count_tokens
Estimate token counts for text or chat messages to fit into a model's context window. Supports multiple model families with per-message overhead.
Instructions
Estimate tokens in a string or chat-message array. Fast, dependency-free, within ~10-20% of true tokenizer counts on English prose. Pass a model name to pick the right per-family estimator (openai, anthropic, google, llama, default).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input | Yes | String or array of chat messages. | |
| model | No | Optional model name (e.g. "gpt-5", "claude-sonnet-4-6"). Picks the closest estimator family. | |
| overhead | No | Per-message overhead in tokens (default depends on model family, usually 4-6). |