Count tokens in a Message changedapi/beta/messages/count_tokens
Nearest release: v2.1.232, published 7 hours before this site recorded the change. Shown because the two are within 24 hours of each other. Nothing here says the release caused the edit.
Recorded here
Lines+3,074added
Lines−0removed
From line
—
no hunk to open at
First seen
14 Aug 2026
this site's first read of the page
Recorded edits25to this page, all time
## Count tokens in a Message ### Header Parameters ### Body Parameters ### Returns ### Example #### Response
The whole hunk
3074 lines, first recorded
/
lines
The first capture of this source. The page was already there, and this is what it said.
---
title: Count tokens in a Message
url: https://platform.claude.com/docs/en/api/beta/messages/count_tokens
---
## Count tokens in a Message
**post** `/v1/messages/count_tokens`
Count the number of tokens in a Message.
The Token Count API can be used to count the number of tokens in a Message, including tools, images, and documents, without creating it.
Learn more about token counting in our [user guide](https://platform.claude.com/docs/en/build-with-claude/token-counting)
### Header Parameters
- `"anthropic-beta": optional array of AnthropicBeta`
Optional header to specify the beta version(s) you want to use.
- `string`
- `"message-batches-2024-09-24" or "prompt-caching-2024-07-31" or "computer-use-2024-10-22" or 30 more`
- `"message-batches-2024-09-24"`
- `"prompt-caching-2024-07-31"`
- `"computer-use-2024-10-22"`
- `"computer-use-2025-01-24"`
- `"pdfs-2024-09-25"`
- `"token-counting-2024-11-01"`
- `"token-efficient-tools-2025-02-19"`
- `"output-128k-2025-02-19"`
- `"files-api-2025-04-14"`
- `"mcp-client-2025-04-04"`
- `"mcp-client-2025-11-20"`
- `"dev-full-thinking-2025-05-14"`
- `"interleaved-thinking-2025-05-14"`
- `"code-execution-2025-05-22"`
- `"extended-cache-ttl-2025-04-11"`
- `"context-1m-2025-08-07"`
- `"context-management-2025-06-27"`
- `"model-context-window-exceeded-2025-08-26"`
- `"skills-2025-10-02"`
- `"fast-mode-2026-02-01"`
- `"output-300k-2026-03-24"`
- `"user-profiles-2026-03-24"`
- `"advisor-tool-2026-03-01"`
- `"managed-agents-2026-04-01"`
- `"cache-diagnosis-2026-04-07"`
- `"dreaming-2026-04-21"`
- `"thinking-token-count-2026-05-13"`
- `"server-side-fallback-2026-06-01"`
- `"server-side-fallback-2026-07-01"`
- `"fallback-credit-2026-06-01"`
- `"fallback-credit-2026-07-01"`
- `"agent-memory-2026-07-22"`
- `"mid-conversation-tool-changes-2026-07-01"`
- `"anthropic-user-profile-id": optional string`
The user profile ID to attribute this request to. Use when acting on behalf of a party other than your organization. Requires the `user-profiles` beta header.
### Body Parameters
- `messages: array of BetaMessageParam`
Input messages.
Our models are trained to operate on alternating `user` and `assistant` conversational turns. When creating a new `Message`, you specify the prior conversational turns with the `messages` parameter, and the model then generates the next `Message` in the conversation. Consecutive `user` or `assistant` turns in your request will be combined into a single turn.
Each input message must be an object with a `role` and `content`. You can specify a single `user`-role message, or you can include multiple `user` and `assistant` messages.
If the final message uses the `assistant` role, the response content will continue immediately from the content in that message. This can be used to constrain part of the model's response.
Example with a single `user` message:
```json
[{"role": "user", "content": "Hello, Claude"}]
```
Example with multiple conversational turns:
```json
[
{"role": "user", "content": "Hello there."},
{"role": "assistant", "content": "Hi, I'm Claude. How can I help you?"},
{"role": "user", "content": "Can you explain LLMs in plain English?"},
]
```
Example with a partially-filled response from Claude:
```json
[
{"role": "user", "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"},
{"role": "assistant", "content": "The best answer is ("},
]
```
Each input message `content` may be either a single `string` or an array of content blocks, where each block has a specific `type`. Using a `string` for `content` is shorthand for an array of one content block of type `"text"`. The following input messages are equivalent:
```json
{"role": "user", "content": "Hello, Claude"}
```
```json
{"role": "user", "content": [{"type": "text", "text": "Hello, Claude"}]}
```
See [input examples](https://platform.claude.com/docs/en/build-with-claude/working-with-messages).
Note that if you want to include a [system prompt](https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/claude-prompting-best-practices#give-claude-a-role), you can use the top-level `system` parameter — there is no `"system"` role for input messages in the Messages API.
There is a limit of 100,000 messages in a single request.
- `content: string or array of BetaContentBlockParam`
- `string`
- `array of BetaContentBlockParam`
- `BetaTextBlockParam object { text, type, cache_control, citations }`
- `text: string`
- `type: "text"`
- `"text"`
- `cache_control: optional BetaCacheControlEphemeral or null`
Create a cache control breakpoint at this content block.
- `type: "ephemeral"`
- `"ephemeral"`
- `ttl: optional "5m" or "1h"`
The time-to-live for the cache control breakpoint.
This may be one the following values:
- `5m`: 5 minutes
- `1h`: 1 hour
Defaults to `5m`. See [prompt caching pricing](https://platform.claude.com/docs/en/build-with-claude/prompt-caching) for details.
- `"5m"`
- `"1h"`
- `citations: optional array of BetaTextCitationParam or null`
- `BetaCitationCharLocationParam object { cited_text, document_index, document_title, 3 more }`
- `cited_text: string`
- `document_index: number`
- `document_title: string or null`
- `end_char_index: number`
- `start_char_index: number`
- `type: "char_location"`
- `"char_location"`
- `BetaCitationPageLocationParam object { cited_text, document_index, document_title, 3 more }`
- `cited_text: string`
- `document_index: number`
- `document_title: string or null`
- `end_page_number: number`
- `start_page_number: number`
- `type: "page_location"`
- `"page_location"`
- `BetaCitationContentBlockLocationParam object { cited_text, document_index, document_title, 3 more }`
- `cited_text: string`
The full text of the cited block range, concatenated.
Always equals the contents of `content[start_block_index:end_block_index]` joined together. The text block is the minimal citable unit; this field is never a substring of a single block. Not counted toward output tokens, and not counted toward input tokens when sent back in subsequent turns.
- `document_index: number`
- `document_title: string or null`
- `end_block_index: number`
Exclusive 0-based end index of the cited block range in the source's `content` array.
Always greater than `start_block_index`; a single-block citation has `end_block_index = start_block_index + 1`.
- `start_block_index: number`
0-based index of the first cited block in the source's `content` array.
- `type: "content_block_location"`
- `"content_block_location"`
- `BetaCitationWebSearchResultLocationParam object { cited_text, encrypted_index, title, 2 more }`
- `cited_text: string`
- `encrypted_index: string`
- `title: string or null`
- `type: "web_search_result_location"`
- `"web_search_result_location"`
- `url: string`
- `BetaCitationSearchResultLocationParam object { cited_text, end_block_index, search_result_index, 4 more }`
- `cited_text: string`
The full text of the cited block range, concatenated.
Always equals the contents of `content[start_block_index:end_block_index]` joined together. The text block is the minimal citable unit; this field is never a substring of a single block. Not counted toward output tokens, and not counted toward input tokens when sent back in subsequent turns.
- `end_block_index: number`
Exclusive 0-based end index of the cited block range in the source's `content` array.
Always greater than `start_block_index`; a single-block citation has `end_block_index = start_block_index + 1`.
- `search_result_index: number`
0-based index of the cited search result among all `search_result` content blocks in the request, in the order they appear across messages and tool results.
Counted separately from `document_index`; server-side web search results are not included in this count.
- `source: string`
- `start_block_index: number`
0-based index of the first cited block in the source's `content` array.
- `title: string or null`
- `type: "search_result_location"`
- `"search_result_location"`
- `BetaImageBlockParam object { source, type, cache_control }`
- `source: BetaBase64ImageSource or BetaURLImageSource or BetaFileImageSource`
- `BetaBase64ImageSource object { data, media_type, type }`
- `data: string`
- `media_type: "image/jpeg" or "image/png" or "image/gif" or "image/webp"`
Cut at 300 lines. The page has the rest.
No line in this hunk matches that.