Documentation
¶
Overview ¶
Package usage holds the pure wire shapes for usage reporting, token counts and upstream timing, shared by the public client and the server's usage records.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ReasoningTiming ¶
type ReasoningTiming struct {
Start int64 `json:"start"` // start → first reasoning frame
End int64 `json:"end"` // start → last reasoning frame
}
ReasoningTiming is the reasoning span. Microseconds elapsed from the request start, anchored not chained, same as UpstreamTiming:
reasoning span = End - Start
type Tokens ¶
Tokens is the universal token-count shape across providers. Keys are convention-driven, not enforced; every adapter fills the keys its provider reported with the same meaning:
input prompt tokens neither read from nor written to a prompt cache cache_read prompt tokens read from a cache (not in input) cache_creation prompt tokens written to a cache (not in input) cache_creation_1h part of cache_creation: prompt tokens written to a 1-hour cache output every generated token, reasoning included reasoning part of output: reasoning tokens, when the provider reports them apart audio_output part of output: audio tokens accepted_prediction part of output: predicted-output tokens that appeared in the completion rejected_prediction part of output: predicted-output tokens that did not audio_input part of input: audio tokens server_tool_use_input input tokens of server-side tool calls; not treated as part of another key server_tool_use_output output tokens of server-side tool calls; not treated as part of another key
A request's total is input + cache_read + cache_creation + output. A part is never added to its whole; Billable gives the count to charge once a part has a rate of its own.
func (Tokens) Add ¶
Add adds other into t in place. Useful for streaming chunks where each chunk emits a partial usage block.
func (Tokens) Billable ¶ added in v0.7.11
Billable returns the count of key to charge at key's own rate: the stored count less its parts that rated reports as charged at a rate of their own, so no token is charged twice. A part without a rate stays in its whole. The stored counts are not changed.
When the rated parts add up to more than the whole they cannot be inside it (a provider that reports them apart), and nothing is deducted.
func (Tokens) PromptTokens ¶ added in v0.7.17
PromptTokens returns the request's prompt length: input + cache_read + cache_creation. Context-length price tiers are chosen by it, since providers count cache reads and writes toward the threshold.
func (Tokens) Sum ¶
Sum returns the arithmetic total of all values. Used by legacy single-meter rate-limit callers that haven't migrated to typed meters.
NOT a token total for billing/usage reporting: some keys are sub-breakdowns of a coarser one (e.g. reasoning ⊂ output), so Sum double-counts them. See Tokens for the total.
type UpstreamTiming ¶
type UpstreamTiming struct {
Start int64 `json:"start"` // start → handed to upstream
ResponseStart int64 `json:"response_start"` // start → first byte (TTFT)
ResponseEnd int64 `json:"response_end"` // start → upstream done
}
UpstreamTiming is the upstream-leg breakdown. All values are microseconds elapsed from the request start (Event.Timestamp) — the unit lives here, not in the field names. Every mark is anchored to the start, never chained, so derive intervals at query time:
upstream TTFT = ResponseStart - Start stream body time = ResponseEnd - ResponseStart
It is a pure wire shape: the public client reports it as StreamTiming and pkg/usage.Event embeds it. Lives in sdk/usage so both the vendorable client and the server-side event record can name it without a cross-module edge.