Anthropic Claude Sonnet 5 · New tokenizer

Claude Sonnet 5 Token Counter

Claude Sonnet 5 uses Anthropic's newer tokenizer. Count the complete request with Messages count_tokens instead of reusing estimates from Sonnet 4.6.

Accuracy note

API counting required. Anthropic Messages count_tokens with model claude-sonnet-5 is authoritative; the local cl100k_base explorer is not a Claude tokenizer.

Model ID
claude-sonnet-5
Context
1,000,000 tokens
Max output
128,000 tokens
Exact method
Messages count_tokens

Facts checked against official sources on .

How to Count Claude Sonnet 5 Tokens

Call Anthropic's Messages token counting endpoint with model claude-sonnet-5 and the same system prompt, messages, tools, images, or documents you plan to send. The response returns an input_tokens value before message generation.

Anthropic describes the result as an estimate because the final message request can differ slightly and may include system-added optimization tokens. The endpoint is nevertheless the authoritative preflight method for Claude Sonnet 5 and is more reliable than an OpenAI or character-ratio proxy.

pythonOfficial counting example
import anthropic

client = anthropic.Anthropic()
result = client.messages.count_tokens(
    model="claude-sonnet-5",
    system="You are a concise assistant.",
    messages=[{"role": "user", "content": "Count this prompt."}],
)
print(result.input_tokens)

Claude Sonnet 5 Uses a Newer Tokenizer

Anthropic states that Claude Sonnet 5 uses the newer tokenizer also used by Claude Opus 4.7 and later Opus models, Claude Fable 5, and related current models. The same text produces approximately 30 percent more tokens than on earlier Claude models, although the exact change depends on the content.

That does not mean Sonnet 5 is always more expensive or less efficient; it means token-based limits and prices must be evaluated with Sonnet 5 counts. Recount real prompts rather than copying a Sonnet 4.6 token total into a new budget.

What count_tokens Includes

Request componentSupported by preflight countCaveat
System and messagesYesUse the exact conversation structure
Tool definitionsYesServer tool counts can depend on the sampling stage
Images and PDFsYesUse the same media payload structure
Generated answerNoRead output usage after generation
System-added optimization tokensMay appearAnthropic says these are not billed to you

Claude Sonnet 5 Context Planning

Claude Sonnet 5 supports a one-million-token context window and up to 128,000 output tokens. Because the newer tokenizer represents the same source text with more tokens on average than Sonnet 4.6, the effective amount of text that fits in a fixed token budget can be lower.

Leave headroom for the answer and for request components that are easy to overlook, especially tool schemas, documents, and long multi-turn histories. Run count_tokens again whenever the model ID, system prompt, tool set, or document set changes.

Tiktokenizer Reference Sources

Tokenizer behavior and model limits change. Verify production decisions with current provider documentation:

Frequently asked questions

How do I count Claude Sonnet 5 tokens?

Use Anthropic Messages count_tokens with model claude-sonnet-5 and the complete structured request.

Did Claude Sonnet 5 change tokenizers?

Yes. Anthropic says it uses a newer tokenizer that produces approximately 30 percent more tokens than earlier Claude models for the same text, depending on content.

Is the Claude token counting endpoint free?

Anthropic currently documents token counting as free and subject to separate rate limits.

Is the local cl100k_base count exact for Claude?

No. cl100k_base is an OpenAI encoding and must not be treated as Claude Sonnet 5 usage.