Documentation
¶
Overview ¶
Package tokenkit estimates LLM token counts for text and images without calling an upstream API.
Text is counted exactly with tiktoken for OpenAI GPT and o-series models and estimated with per-tokenizer weights for Claude, Gemini, and other models. Image costs follow each provider's published sizing rules. The weights were calibrated against upstream usage on real agent traffic (Claude Code and Codex sessions).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Count ¶
Count returns the token count of text for model. GPT and o-series models are counted with their tiktoken encoding; other models use Estimate.
func Estimate ¶
Estimate returns the estimated token count of text for model, using the weights of the model's family. It never runs a tokenizer; use Count for an exact count where one is available.
func EstimateFamily ¶
EstimateFamily returns the estimated token count of text for a tokenizer family. Unrecognized families use the FamilyUnknown weights.
func ImageNeedsSize ¶
ImageNeedsSize reports whether ImageTokens depends on the image dimensions for model and detail. When it returns false, callers can skip reading the image.
func ImageTokens ¶
ImageTokens returns the prompt tokens one image costs on model.
Types ¶
type Family ¶
type Family string
Family is a tokenizer family. Models in one family tokenize text alike.
const ( FamilyOpenAI Family = "openai" // GPT and o-series models FamilyClaude Family = "claude" // Claude models before Claude Opus 4.7 FamilyClaude47 Family = "claude4.7" // Claude Opus 4.7 and later, including Claude 5, Fable, and Mythos FamilyGemini Family = "gemini" // Gemini models FamilyUnknown Family = "unknown" // everything else )
func ModelFamily ¶
ModelFamily classifies a model name, case-insensitively. Vendor prefixes and date suffixes are allowed (for example us.anthropic.claude-opus-4-7-v1:0).
type Image ¶
type Image struct {
// Width and Height are the pixel dimensions. Zero or negative means
// unknown; where size matters, a 1024×1024 image is assumed.
Width, Height int
// Detail is the OpenAI detail level (low, high, auto, original) or the
// Gemini media resolution (MEDIA_RESOLUTION_LOW, _MEDIUM, _HIGH,
// _ULTRA_HIGH). Empty means the provider default. Each family ignores
// values meant for the other.
Detail string
}
Image describes one input image.