Claude tokens: how to count them

Claude tokens explained: a Claude token counter with count_tokens, the usage fields, the Claude context window and how Claude Code tokens add up.

· 5 min read

Claude tokens are the unit everything in the Claude API is measured in: the size of the context window, the length of an answer and the usage you see after each request. Knowing how many tokens a request will take before you send it helps you keep prompts inside the context window, plan long agent sessions and spot a prompt that has grown much larger than you meant.

This post covers what Claude tokens are, a Claude token counter you can call before sending, the usage in every response, how much fits in the Claude context window and how to watch Claude Code tokens.

What Claude tokens are

A token is a piece of text the model reads as one unit. Common English words are often a single token, while longer or rarer words are split into several. Code, numbers and punctuation have their own patterns, and text in other languages, such as Russian, usually takes more tokens per word than English.

Claude token counter: the count_tokens endpoint

The API has an endpoint that counts tokens without running the model: POST /v1/messages/count_tokens. You send the same body you would send to /v1/messages, without max_tokens, and get back the number of input tokens.

curl {BASE}/v1/messages/count_tokens \
  -H "x-api-key: {KEY}" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-5-5",
    "system": "You are a helpful assistant.",
    "messages": [{"role": "user", "content": "Summarize this file."}]
  }'

The answer is short:

{ "input_tokens": 27 }

A few things to know about it:

  • The count covers everything in the request: system prompt, messages, tool definitions, images and documents. Pass the same tools you plan to use, otherwise the number will be too low.
  • It counts for a specific model, so set the model field to the one you will actually use.
  • It does not generate anything, so it is safe to call as often as you need, for example before every turn of a long chat.

The same endpoint is available through your own Base URL, so tools that check the size of a prompt before sending it keep working when you connect them through a custom address.

Read the real numbers: the usage object

Every response from /v1/messages includes a usage object. It is the exact record of what the request used:

FieldWhat it counts
input_tokensInput processed normally
cache_creation_input_tokensInput written to the prompt cache
cache_read_input_tokensInput read from the cache
output_tokensEverything the model wrote, including thinking

The total input of a request is the sum of the first three. When caching works well, most of the input moves from input_tokens to cache_read_input_tokens, which counts about ten times less. The post on prompt caching explains how to get there.

When you stream the answer, the final usage arrives in the last events of the stream. If you only read the text, look for the message_delta event at the end to get the output count.

The parts people forget

Parts of a Claude request that count as tokens
Everything in the request counts as input, not only the new message. Thinking counts as output.

Tool definitions. Every tool you pass has a name, a description and a schema, and all of that counts as input on every request. An agent with thirty tools can spend thousands of tokens before the first message.

The system prompt. It is sent with every request, not just the first one. A long system prompt is the first thing worth caching.

Conversation history. Each turn sends the whole conversation again. A chat that started small can reach hundreds of thousands of tokens after a long session.

Thinking. With adaptive thinking on, the model may reason before it answers. Those tokens count as output, even when your app does not show them.

Images and files. Pictures and PDF pages are converted into tokens too. A large screenshot can take more tokens than a page of text.

Input and output are not equal

Output tokens weigh several times more than input tokens, because writing is harder work for the model than reading. In practice, this means that a long answer often matters more than a long prompt. If you need short answers, say so in the prompt and set a sensible max_tokens.

Which model you choose also changes the picture: heavier models think longer and write more on hard tasks. See Claude Opus vs Sonnet vs Haiku for when each one is worth it.

Claude context window: how many tokens fit

The Claude 5.5 models and Fable 5.2 have a context window of up to one million tokens and can write up to 128,000 tokens in one answer. Everything shares that window: the system prompt, tools, history and the answer itself. A long conversation reaches the limit sooner than it seems, so check its size with count_tokens from time to time.

Claude Code tokens

Claude Code keeps track of tokens for you. The /context command shows how much of the window is used and by what: system prompt, tools, memory files such as CLAUDE.md and messages. When a conversation nears the limit, Claude Code compacts the history automatically, and /compact does it on demand. When you switch to unrelated work, /clear starts a fresh session.

Check it in your dashboard

You do not have to log anything yourself. The history in your dashboard lists every request with its model and the same four token counts, so you can see which session, tool or prompt used the most. It is the quickest way to find a runaway agent loop or a system prompt that grew too large.

Daily token chart in the dashboard, demo data
The daily chart shows how many tokens each day used (demo data).
Request history in the dashboard with token counts, demo data
The request history lists every request with its model and token counts (demo data).

Summary

  1. Use /v1/messages/count_tokens to check the size of a request before sending it.
  2. Read usage in every response for the exact numbers.
  3. Count tools, system prompt, history, thinking and images, not only the visible message.
  4. Cache the parts that repeat.

The API docs list every endpoint. To try it with your own key, create an account and follow the step-by-step guide.

Try it with your own tools

Create an account, get a key and connect Claude Code or any other app in a few minutes.

More posts

All posts