tokenize

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package tokenize owns gateway-side token estimation. When a provider stream ends without reporting usage, the estimator counts prompt and completion tokens with the closest known tokenizer so accounting, budgets, and clients still receive token totals. Codecs initialize at composition time and are shared across requests; counting is read-only and safe for concurrent use.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Estimator

type Estimator struct {
	// contains filtered or unexported fields
}

Estimator counts tokens with shared pre-built codecs. Create one with NewEstimator at composition time; the zero value has no codecs and falls back to a bytes-per-token heuristic.

func NewEstimator

func NewEstimator() *Estimator

NewEstimator builds the estimator and initializes every codec it can select, so request paths never pay first-use construction and never touch the codecs' lazy decode state.

func (*Estimator) CountMessages

func (e *Estimator) CountMessages(hint Hint, messages []inference.Message) int

CountMessages estimates the prompt token count of a chat request, including per-message framing overhead and the reply preamble.

func (*Estimator) CountText

func (e *Estimator) CountText(hint Hint, text string) int

CountText estimates the token count of one text with the hinted codec.

type Hint

type Hint struct {
	// Tokenizer is the catalog tokenizer family, for example "gpt".
	Tokenizer string
	// Model is the exact provider model ID.
	Model string
}

Hint names the routed model so the estimator can pick the closest codec. Both fields are optional; an empty hint selects the default.

Jump to

Keyboard shortcuts

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