Documentation
¶
Overview ¶
Package tokencount keeps the bytes-to-tokens ratio relay observed on real traffic, so a caller asking "how many input tokens is this prompt" can be answered from measurement when the upstream exposes no counting endpoint.
Every completed generation yields a true pair — the request body's byte length and the input-token count the upstream reported — and the last pair written wins: one ratio per session and one per model, no history, no averaging. A session ratio is the sharper answer (one conversation, one prompt shape); the model ratio is the fallback for a request that carries no session.
A pod writes a ratio only when it moved more than 5% from the one that pod last wrote to the key, or that write is over 5 minutes old, so the stored ratio may lag the latest sample by up to that much. Pods do not share what they wrote: until its refresh, a pod skips a sample close to its own last write even when another pod has since stored a different ratio.
Deliberately out of scope: tokenisation (relay never tokenises — counts come from the provider), per-request accounting (that is usage logging), and any knowledge of which client header carries a session id (the caller passes the key it resolved).
Expected kv ops: Observe = at most 1 Set per key it can write (≤2 per completed request, from the detached post-flight goroutine), and none while the ratio holds steady; Ratio = 1 Get, plus a second Get only when a session key was given and missed.
Index ¶
Constants ¶
const MetadataKeySession = "session_id"
MetadataKeySession is the lifecycle Metadata key carrying the caller's conversation id. The request entry resolves it through the client profile — only the profile knows which header its client marks a session with — and records it under this profile-neutral name so observers need no such knowledge.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Calibrator ¶
type Calibrator struct {
// contains filtered or unexported fields
}
Calibrator records and reads the observed tokens-per-byte ratio. Construct once at boot over the shared kv store; safe for concurrent use.
func NewCalibrator ¶
func NewCalibrator(s kv.Store) *Calibrator
NewCalibrator constructs a Calibrator over s.
func (*Calibrator) Observe ¶
func (c *Calibrator) Observe(ctx context.Context, sessionID, modelID string, bodyBytes, inputTokens int)
Observe records one true measurement: inputTokens is what the upstream reported for a request whose body was bodyBytes long. Both ids are optional — an empty or unusable one is skipped, so a request with no session still teaches the model ratio. Non-positive inputs are ignored rather than stored as a zero ratio. A key is written only when the ratio moved or its last write is due for a refresh (see claimWrite).
Errors are dropped on purpose: a lost sample costs the next estimate some accuracy and nothing else, and this runs in post-flight where there is no caller to tell.
The writes together are bounded by writeTimeout within ctx. The deadline is taken only once a write is claimed: most observations write nothing.
type Observer ¶
type Observer struct {
// contains filtered or unexported fields
}
Observer feeds completed generations back into the Calibrator. It is a lifecycle.Collector, so it runs after the usage producer filled its Event — which is where the upstream's true input-token count already sits, parsed once, for streamed and buffered responses alike.
func NewObserver ¶
func NewObserver(cal *Calibrator) *Observer
NewObserver constructs the collector recording into cal.