usage

package
v0.18.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func Present

func Present(v gjson.Result) bool

Present reports whether a usage block is there: an object with at least one field. A JSON null or `{}` is absent, not a block in an unknown dialect.

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.

func (*Merger) Add

func (m *Merger) Add(t Tiers, hasInput bool)

Add folds one event's tiers into the merge. hasInput is Parse's second return for this event.

func (*Merger) Result

func (m *Merger) Result() (Tiers, bool)

Result is the merged tiers, and whether any event was added.

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

func Parse(u gjson.Result) (t Tiers, hasInput, ok bool)

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.

func (Tiers) Zero

func (t Tiers) Zero() bool

Zero reports whether every count is zero.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL