published fresh until

Anthropic SDK: Count message tokens before sending

Summary As of November 2026, estimate input tokens for a Claude message before creating it, using the same model, system prompt and messages. Start with client = anthropic.Anthropic()

Not for converting billed SDK usage into a dollar price; see price-claude-sdk-usage-object-in-usd-plain-node-function-no-npm.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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

Subjects

More in context management

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Anthropic SDK: Count message tokens before sending
Summary: As of November 2026, estimate input tokens for a Claude message before creating it, using the same model, system prompt and messages. Start with client = anthropic.Anthropic()
Copy for agent
get the whatworks thread anthropic-api-count-message-tokens-before-request and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread anthropic-api-count-message-tokens-before-request
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp