Documentation
¶
Overview ¶
Package usage reads the billed token tiers out of a provider's `usage` block, and merges them across a stream's events into one set.
One rule, one place. Two hosts need it (a provider-API proxy's own request path and its keep-alive/cheap-model paths), and each used to keep its own copy, in different dialects — a gap showed up as a silent zero, not as an error.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Merger ¶
type Merger struct {
// contains filtered or unexported fields
}
Merger folds a stream's events into one set of tiers.
FreshInput is REPLACED by the last event whose Parse reported hasInput. A LiteLLM gateway streaming an Anthropic-shaped response has been seen to put the TOTAL prompt in one event (no cache fields) and the true split in a later one. Maxing the input field there kept the stale total and double-counted the cache read on top of it, so the latest reported input is the one to trust (a host bug, #2253). An event with no input count leaves FreshInput alone, so a real Anthropic message_delta (output only) does not clobber message_start's value, and a reported zero cannot either.
CacheRead, CacheWrite and CacheWrite1h each keep the MAXIMUM seen, independently. Each starts at 0 (never reported), so a plain max against a later real count is correct on its own with no grouping to FreshInput. This is what fixes a host bug (#1610): the ?beta=true path defers the cache split from message_start to a later event that repeats no input count, and that event's larger cache counts must still raise the running total. CacheWrite1h is maxed on its OWN, not tied to CacheWrite: a later event that raises CacheWrite with no 1h breakdown of its own must not drop an earlier, valid 1h count to zero (a host's "fits in its frame" case).
Output always keeps the maximum seen, since the wire reports it cumulatively.
This matches a host's proven-correct stream merge; see the wiring document in docs/, section W6.6.
type Tiers ¶
type Tiers struct {
FreshInput int64
CacheRead int64
CacheWrite int64
CacheWrite1h int64
Output int64
}
Tiers is one response's billed token counts.
FreshInput, CacheRead and CacheWrite are disjoint. CacheWrite1h is a SUBSET of CacheWrite, never an addition to it.
func Parse ¶
Parse reads a `usage` object. ok is false when the object has no field of a known dialect. hasInput reports whether this block carried a nonzero input or prompt token count — the signal a stream merge uses to decide whether this event's FreshInput replaces the running total (see Merger).
Dialects:
Anthropic input_tokens (fresh only), cache_read_input_tokens,
cache_creation_input_tokens, cache_creation.ephemeral_1h_input_tokens
OpenAI Responses input_tokens (includes cache), input_tokens_details.cached_tokens,
input_tokens_details.cache_write_tokens or a bare cache_write_tokens
(see cacheWriteTokens; the path is not yet confirmed from the wire)
OpenAI chat prompt_tokens (includes cache), prompt_tokens_details.cached_tokens,
prompt_tokens_details.cache_write_tokens or a bare cache_write_tokens
Output only output_tokens alone, or together with the Anthropic cache fields above with
no input_tokens: Anthropic's streaming message_delta carries exactly this on
the plain path (output_tokens alone), and the split cache counts on the
?beta=true path, which defers them from message_start without repeating
input_tokens.
OpenAI counts cached tokens INSIDE its input total, so fresh is the difference. Anthropic reports the tiers apart. Reading one as the other double-counts the whole transcript.