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 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.
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 = 1 Set per key it can write (≤2 per completed request, from the detached post-flight goroutine); 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.
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.
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.
func (*Observer) Collect ¶
Collect records this request's (body bytes, prompt tokens) pair. No-op unless both are known and the request resolved to a model: an unrouted or failed request measures nothing.
The body length is read off the retained inbound reference — never copied, and skipped when the runner only captured a prefix of it, which would read as a request far denser in tokens than it was.