Count Tokens (Claude)
Text Series
Count Tokens (Claude)
POST
Count Tokens (Claude)
Introduction
Calculate the token count of Claude messages to estimate costs before sending requests. This endpoint does not consume quota and only performs local calculations.Authentication
string
required
Bearer Token, e.g.,
Bearer sk-xxxxxxxxxxRequest Parameters
string
required
Claude model identifier. Supported models include:
claude-opus-4-5-20251101(Recommended replacement for claude-3-opus)claude-haiku-4-5-20251001claude-sonnet-4-5-20250929claude-sonnet-4-20250514- Other Claude series models
array
required
List of conversation messages, each containing
role (user/assistant) and content. content can be a string or an array of media content.Supported content types:- Plain text messages
- Multimodal messages (including images)
- Tool call results
string|array
System prompt (optional), can be a string or an array of media content. Used to set the model’s behavior and role.
array
Tool definitions list (optional), used to calculate tokens related to tool calls.
Response Parameters
number
Total token count of input messages, including:
- System prompt tokens
- All messages tokens
- Tools definition tokens (if any)
Basic Examples
- Simple Text Message
- With System Prompt
- Multi-turn Conversation
- Python Example
Advanced Use Cases
With Tool Definitions
Multimodal Content
Use Cases
1. Cost Estimation
Calculate token counts before sending bulk requests to estimate costs:2. Context Window Management
Check if messages exceed the model’s context window limit:3. Prompt Optimization
Compare token consumption of different prompts:Important Notes
Error Handling
Missing Required Parameters
Invalid API Key
Related Resources
- Create Message Request (Claude) - Send actual Claude requests
- Model List - View supported Claude models
- Pricing - Learn about token billing standards
