tokencount

package
v0.9.22 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: Apache-2.0 Imports: 7 Imported by: 1 module, 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 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

View Source
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.

func (*Calibrator) Ratio

func (c *Calibrator) Ratio(ctx context.Context, sessionID, modelID string) (float64, bool)

Ratio returns the tokens-per-byte ratio to estimate with: the session's own when one was recorded, else the model's. ok is false when neither is known and the caller must fall back to its own heuristic.

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

func (o *Observer) Collect(lc *lifecycle.Context)

Collect records this request's (body bytes, input 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.

Jump to

Keyboard shortcuts

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