Counting Tokens Before You Send: Claude, OpenAI, and Gemini
Compare pre-request token counting APIs across Anthropic, OpenAI, and Google Gemini: endpoints, schemas, multimodal rules, limits, and cost controls.
- Anthropic, OpenAI, and Google provide dedicated pre-call endpoints that calculate exact input tokens before running inference.
- Anthropic requires images and documents as base64 and rejects server tools such as web search and code execution on its counting endpoint.
- OpenAI Responses API token counting incorporates request structure and tool schemas that client-side tokenizers miss.
- Gemini exposes fixed tile formulas for images, per-second rates for audio and video, and distinct usage metrics for agentic search and thinking.
Why Inspecting Token Totals Prior to Transmission Matters
Every major frontier model vendor bills input and output based on token volume. Sending unvalidated payloads directly to inference endpoints introduces financial unpredictability, exposes workflows to context overflow failures, and complicates traffic routing across model tiers. Pre-request token counting allows systems to determine input size before any inference execution begins.
Historically, developers relied on client-side tokenizers such as tiktoken or heuristic formulas like character divisions. While local text tokenizers process plain text strings rapidly, they fail when requests combine structured system prompts, tool function schemas, conversation turn metadata, and binary media assets. In production pipelines, an image or structured schema can inject hundreds or thousands of tokens that local text splitters cannot detect. For teams balancing budgets or building model fallbacks, inspecting token consumption upfront enables deterministic routing, prompt shortening, and rate-limit safeguards. To evaluate broader price differences across tiers, consult /posts/choosing-an-llm-api-model-by-cost-tier/ and /tools/llm-api-cost-calculator/.
Anthropic Claude: The Messages Count Tokens Endpoint
Anthropic provides pre-call token estimation via the token counting endpoint. This interface mirrors the standard Messages API structure, accepting system instructions, conversation turns, client-defined tools, and base64-encoded media blocks.
The endpoint is accessible across the Claude API, Claude Platform on AWS, Amazon Bedrock, Google Cloud, and Microsoft Foundry. Supported models include active catalog models such as claude-opus-5-5. Anthropic notes that Claude 4.7 and later releases alongside Claude Mythos Preview utilize an updated tokenizer. Under this newer tokenizer, identical raw text produces approximately 30 percent more tokens than on previous architectures, requiring developers to test counts against their targeted model identifier rather than historical baselines.
curl https://api.anthropic.com/v1/messages/count_tokens \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "content-type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-opus-5-5",
"system": "You are a scientist",
"messages": [{"role": "user", "content": "Hello, Claude"}]
}'
The count returned represents an estimate. In certain instances, actual input tokens recorded upon message generation can diverge by minor margins due to system-level optimizations added automatically by Anthropic. However, Anthropic does not bill for system-added tokens; invoices reflect content supplied in the request. Pricing for claude-opus-5-5 stands at $4 per 1M input tokens, $0.2 per 1M cached input tokens, and $20 per 1M output tokens, as documented on /tools/llm-api-pricing/.
Crucial payload constraints apply to the Anthropic token counting service. An invalid_request_error occurs when submitting server tools—including web search, web fetch, hosted code execution, and tool search. The only server tool permitted during token counting is the advisor tool, whose count covers the first sampling call of the executor. Furthermore, the Model Context Protocol (MCP) connector is prohibited on this endpoint. For binary attachments such as images or PDFs, the endpoint rejects url or file sources; developers must supply data as base64 content blocks.
OpenAI: Responses API Input Token Counting
OpenAI provides request-time counting through its input token count endpoint. This endpoint accepts the exact input format used by responses.create, allowing multi-turn messages, system instructions, function schemas, images, and documents to be evaluated in a single round trip.
A key advantage over client-side libraries like tiktoken is the inclusion of structural tokens. OpenAI notes that request envelopes, boundary markers, and message roles consume tokens that do not appear when tokenizing plain strings locally. The counting API factors these formatting tokens into the response, delivering the precise figure the model encounters.
curl https://api.openai.com/v1/responses/input_tokens \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-6-astra",
"instructions": "You are a helpful assistant that explains concepts simply.",
"input": "Explain quantum computing in one sentence."
}'
On models such as gpt-6-astra, pricing is structured at $10 per 1M input tokens, $1 per 1M cached input tokens, and $50 per 1M output tokens. When handling multimodal content, OpenAI allows input images through URLs or base64 data URLs, with an optional detail property set to values such as auto. PDFs and file objects can be submitted directly via file_id references from the Files API, avoiding manual byte extraction.
When planning downstream costs, developers should recognize differences between input estimates and generated output tokens. The Responses API reports total generated tokens under output_tokens, whereas Chat Completions reports completion_tokens. In models such as GPT-5 architectures, formatting markers delimiting response channels or tool calls add tokens to generation totals that do not appear in raw text or logprobs, even when reasoning tokens report a value of 0.
Google Gemini: SDK Methods and Multimodal Formulas
The Gemini API evaluates tokens across both text and multimodal assets. According to Google documentation, one Gemini token equals approximately 4 characters, while 100 tokens represent roughly 60-80 English words. Developers using SDK versions newer than 2.0.0 can invoke count_tokens to inspect input sizes prior to message creation.
curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.8-flash:countTokens" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"contents": [{"parts": [{"text": "The quick brown fox."}]}]}'
Gemini models like gemini-3.8-flash incur rates of $0.75 per 1M input tokens, $0.075 per 1M cached input tokens, and $3.75 per 1M output tokens. Multi-turn interactions can be counted and continued across context histories using previous_interaction_id, linking preceding turns without requiring developers to manually concatenate transcript payloads.
- Image Assets: Pictures measuring ≤384 pixels in both dimensions consume 258 tokens. Dimensions exceeding 384 pixels are broken into 768x768 pixel tiles, each tile charging 258 tokens.
- Static Video Processing: Standard processing at the default rate of 1 FPS uses approximately 100 tokens/second for low resolution and approximately 300 tokens/second for high resolution. A 60-second clip yields approximately 6,000 tokens.
- Agentic Video Processing: In agentic mode, the model dynamically fetches only transcript portions, specific frames, or audio tracks necessary for the prompt, reducing consumption by up to 88% on long-form content. A 1-hour recording that consumes ~1.08M tokens statically can drop to ~108K tokens.
- Audio Assets: Audio processing consumes a predictable rate of 32 tokens per second, resulting in approximately 1,920 tokens for a 60-second audio stream.
When inspecting responses, Gemini separates consumption within the usage object across total_input_tokens, total_output_tokens, total_thought_tokens, total_cached_tokens, total_tool_use_tokens, and aggregate total_tokens. Tool definitions and system instructions are counted alongside regular input.
Provider Comparison: Capabilities, Endpoints, and Limits
While all three vendors offer dedicated pre-call counting, their endpoints diverge significantly in schema compatibility, media referencing, and tool support. Selecting an architectural pattern requires knowing these boundary constraints before deployment.
| Feature / Dimension | Anthropic Claude | OpenAI Responses API | Google Gemini |
|---|---|---|---|
| Primary Counting Endpoint | /v1/messages/count_tokens | /v1/responses/input_tokens | :countTokens REST / models.count_tokens |
| Representative Models | claude-opus-5-5 | gpt-6-astra | gemini-3.8-flash |
| Tool Schema Counting | Supported for client tools and advisor tool; other server tools error | Supported for functions, schemas, and MCP servers | Supported for function definitions, code execution, and search |
| Media Input Requirements | Base64 encoded blocks only; url and file sources trigger error | Image URLs, base64 data URLs, and file_id from Files API | Uploaded File API URIs, base64 data, and raw parts |
| Structural Token Visibility | Estimates input tokens; excludes unbilled system tokens | Exact input count including role and boundary tokens | Reports input tokens; post-run breakdown separates thought and tool use |
Teams managing complex multi-provider routing can leverage these differences. OpenAI allows unified validation of uploaded documents via file_id, whereas Anthropic mandates base64 conversion prior to hitting the counting endpoint. For deeper implementation details regarding functions and tooling, review /posts/function-and-tool-calling-across-claude-gpt-and-gemini/ and caching mechanics at /posts/prompt-caching-explained-claude-gpt-gemini/.
Production Implementation Checklist for Token Counting
Integrating pre-flight token counters into real-time applications requires defensive design to prevent latency bottlenecks and unexpected exceptions. Follow this structured checklist before activating automated routing or enforcement rules:
- Avoid local estimation for multimodal prompts: Do not use division formulas or plain-text tokenizers when inputs contain images, PDFs, or audio. Query provider counting endpoints to capture true tiling and encoding overhead.
- Strip server tools on Anthropic requests: Ensure web search, web fetch, code execution, and MCP connector configurations are excluded from
/v1/messages/count_tokenspayloads to avoidinvalid_request_errorexceptions. - Encode binary assets appropriately: Convert media files to base64 when verifying payloads against Anthropic, or utilize
file_idpointers when calling OpenAI. - Recalculate across model generations: Account for tokenizer shifts, such as the Claude 4.7 update that yields roughly 30 percent higher token counts for identical text compared to earlier architectures.
- Differentiate static versus agentic video in Gemini: Budget 100 or 300 tokens/second for standard 1 FPS video, or monitor
total_tool_use_tokenswhen deploying agentic workflows that reduce usage up to 88%. - Monitor structural formatting tokens: Recognize that role envelopes in OpenAI and response formatting delimiters in models like GPT-5 add tokens beyond visible content.
- Audit tier quotas and usage limits: Couple pre-call verification with account limits detailed in /posts/rate-limits-and-usage-tiers-at-anthropic-openai-and-google/ to maintain uptime under tight rate limits.