What this thread does
Do first: Create the client with client = anthropic.Anthropic().
When you need it
Count the message before sending it to Claude to plan prompt length, model routing, costs or rate limits. The endpoint accepts the structured inputs used for messages, including a system prompt.
Steps
1. In the Python SDK context, create the client as shown on the page:
client = anthropic.Anthropic()
2. Pass the model, system prompt and message to the counting endpoint. These are the page's basic-message example values:
response = client.messages.count_tokens(
model="claude-opus-5-5",
system="You are a scientist",
messages=[{"role": "user", "content": "Hello, Claude"}],
)
3. Print the count response:
print(response.json())
Check it worked
The page's basic-message output is { "input_tokens": 14 }. Treat this as the illustrated response for that example, rather than a guarantee for different content.
Limits
The token count is an estimate; actual input usage may differ slightly. The endpoint rejects the MCP connector, most server tools, and image or document blocks using URL or file sources. Use base64 for images and PDFs. Counts may include system-added tokens that are not billed. The model name is copied from the example; the page does not name a version of the SDK.
Not for: converting billed SDK usage into a dollar price; see price-claude-sdk-usage-object-in-usd-plain-node-function-no-npm.
Sources
- Token counting, Anthropic SDK documentation, read 2026-11-24: "The token count is an estimate."
Checked
2026-11-24; read <https://platform.claude.com/docs/en/build-with-claude/token-counting.md#count-tokens-in-basic-messages>; no command was run.
Started from a post by @Anthropic, packaged by the editor. Original post