Count tokens in a Message
$ ant beta:messages count-tokensPOST/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
Parameters
--tool-choice: optional BetaToolChoiceAuto { type, disable_parallel_tool_use } or BetaToolChoiceAny { type, disable_parallel_tool_use } or BetaToolChoiceTool { name, type, disable_parallel_tool_use } or BetaToolChoiceNone { type }
Body param: How the model should use the provided tools. The model can use a specific tool, any available tool, decide by itself, or not use tools at all.
Header param: Optional header to specify the beta version(s) you want to use.
Returns
Count tokens in a Message
ant beta:messages count-tokens \
--api-key my-anthropic-api-key \
--message '{content: [{text: x, type: text}], role: user}' \
--model claude-opus-5{
"context_management": {
"original_input_tokens": 0
},
"input_tokens": 2095
}Returns Examples
{
"context_management": {
"original_input_tokens": 0
},
"input_tokens": 2095
}