Count Tokens for a Claude Request

The Anthropic API provides a dedicated token counting endpoint that returns the exact token count for a request payload before you send it, without consuming your quota or generating a response. This is particularly valuable for long-context applications where you need to verify a document fits within the context window before committing to the API call. This example shows how to use the token counting endpoint and interpret the results. Claude uses a different tokenizer than GPT models — the same text may produce significantly different token counts. Technical documentation, code, and non-English text are areas where the difference is most pronounced. Claude's tokenizer tends to tokenize common English words and phrases more efficiently, while technical symbols and code may use slightly more tokens compared to GPT's tiktoken. Always count tokens with the actual model you plan to use rather than using a count from a different provider's tokenizer. For the Claude 3.5 Sonnet model, the input context window is 200,000 tokens — one of the largest among production LLM APIs. At 200K tokens, you can fit approximately 150,000 words, or about 600 pages of a standard novel, in a single request. The token counter helps you plan how much of a large document you can include alongside system instructions and conversation history.

Example
Count tokens for the following Claude API request:

System prompt: You are a helpful research assistant specializing in scientific literature. Provide accurate, well-cited information.

User message: Please analyze the methodology section of this research paper and identify potential limitations or biases in the experimental design. Focus on sample size, control groups, and statistical methods used.

Expected response length: medium (300-500 tokens)
[ open in Claude Token Counter → ]

FAQ

Does Anthropic have a token counting API endpoint?
Yes. POST to /v1/messages/count_tokens with the same payload as a normal messages request. It returns the total input token count without generating a response or consuming quota beyond the counting operation itself.
How does Claude's tokenizer compare to GPT's tiktoken?
The tokenizers produce different counts for the same text. For plain English, the counts are similar. For code, technical terms, and non-English text, Claude's tokenizer may produce noticeably different counts. Always verify with the specific model you are deploying.
What is Claude 3.5 Haiku's context window?
Claude 3.5 Haiku has a 200,000-token context window, the same as Claude 3.5 Sonnet. This makes it excellent for long-document tasks where speed and cost matter more than maximum capability.

Related Examples