usage

package
v0.7.28 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: Apache-2.0 Imports: 0 Imported by: 2 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 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

func WholeOf added in v0.7.18

func WholeOf(part string) (string, bool)

WholeOf returns the key that counts part inside it, or false when part is not counted inside another key.

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

type Tokens map[string]int64

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

func (t Tokens) Add(other Tokens)

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

func (t Tokens) Billable(key string, rated func(part string) bool) int64

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

func (t Tokens) PromptTokens() int64

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

func (t Tokens) Sum() int64

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.

Jump to

Keyboard shortcuts

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