count_llm_tokens
Count tokens in text for any major LLM model to manage context windows, avoid overflows, and estimate input costs. Returns consumption, remaining tokens, and cost.
Instructions
Count the tokens in text for any major LLM model.
Returns the exact token count (or a close approximation for non-OpenAI models), how much of the model's context window is consumed, tokens remaining, and the estimated input cost. Essential for prompt engineering, RAG chunk sizing, and context window management.
Supported models: OpenAI: gpt-5.6, gpt-5, gpt-4o, gpt-4.1, o3, o4-mini Anthropic: claude-opus-5, claude-sonnet-5, claude-haiku-4-5 (plus aliases: claude-opus-4, claude-sonnet-4) Google: gemini-3.5-flash, gemini-3.1-pro, gemini-2.0-flash Meta: llama-4-maverick, llama-4-scout, llama-3.3-70b Mistral: mistral-large-3, mistral-small-4 DeepSeek: deepseek-v4-flash, deepseek-v4-pro, deepseek-v3, deepseek-r1
OpenAI models use tiktoken counts. All other models are approximated with cl100k_base (accurate to ±10%). Input prices are estimates; see as_of.
Args: text: The text to count tokens for (prompt, document, message, etc.) model: LLM model name (default "gpt-4o")
Returns: dict with keys: model, model_family, token_count, context_window, context_used_pct, tokens_remaining, fits_in_context, estimated_input_cost_usd, approximate, as_of, note, cost_usd
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | ||
| model | No | gpt-4o |