Documentation
¶
Overview ¶
Package usage tracks token consumption and dollar cost across API requests.
OpenRouter's reported cost is authoritative: RequestUsage.Cost is the dollar figure the provider billed for that request, and SessionUsage accumulates it with no local rate table or fallback. Token counts are display-only — they drive the context-fill indicator and the per-request log fields, and are never converted into dollars.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RequestUsage ¶
type RequestUsage struct {
InputTokens int64 // uncached input tokens sent to the model
OutputTokens int64 // tokens generated by the model in this response
CacheReadInputTokens int64 // input tokens served from the prompt cache this request
Cost float64 // dollar cost OpenRouter reported for this request
}
RequestUsage captures the token buckets reported in a single API response.
InputTokens is the uncached remainder of the prompt. CacheReadInputTokens are tokens served from the prompt cache. OutputTokens are tokens generated in the reply. Cost is the dollar figure OpenRouter reported for the request; it is the sole cost source.
func FromChatUsage ¶ added in v0.2.0
func FromChatUsage(u components.ChatUsage) RequestUsage
FromChatUsage maps the SDK's ChatUsage fields into the harness-internal RequestUsage type, decoupling the rest of the application from the SDK struct.
InputTokens is derived as the uncached remainder of the SDK's total prompt_tokens, matching FromOpenAI's convention: OpenRouter reports CachedTokens as a subset of PromptTokens, not an addition to it.
Cost carries OpenRouter's own dollar figure for the request (Usage.Cost) straight through; it is the sole cost source.
func (RequestUsage) ContextTokens ¶
func (r RequestUsage) ContextTokens() int64
ContextTokens returns the total input-side tokens this request consumed: uncached input plus cache-read. Matches the definition used for SessionUsage.LastContextTokens.
type SessionUsage ¶
type SessionUsage struct {
TotalInputTokens int64 // sum of uncached input tokens across all requests
TotalOutputTokens int64 // sum of output tokens across all requests
TotalCacheReadInputTokens int64 // sum of cache-read tokens across all requests
LastContextTokens int64 // total input-side tokens from the most recent request; used for context-fill display
TotalCost float64 // cumulative dollar cost of all requests
RequestCount int // number of completed API requests
}
SessionUsage accumulates token counts and cost across all requests in a session, and tracks how full the model's context window currently is.
LastContextTokens is not a running total; it is overwritten on each call to Add and reflects only the most recent request. The TUI uses this to display a context-window fill indicator.
func (*SessionUsage) Add ¶
func (s *SessionUsage) Add(r RequestUsage)
Add folds one request's token counts and reported cost into the running session totals and updates the current context window size.
func (*SessionUsage) Merge ¶
func (s *SessionUsage) Merge(other SessionUsage)
Merge folds another SessionUsage's running totals into the receiver. It is used to fold a subagent's fully-accumulated usage (e.g. from research_codebase or review_ticket, each with its own pre-computed TotalCost) into the parent session's totals.
LastContextTokens is deliberately left untouched: a merged subagent runs its own, separate context window, and folding its context-fill snapshot into the parent's would inflate the parent's context-fill indicator with a number that has nothing to do with the parent's own context state. The parent's LastContextTokens must only ever reflect the parent's own most recent request, exactly as documented on Add.
Source Files
¶
- adapter.go
- usage.go