Count tokens in a Message
POST
/v1/messages/count_tokens https://api.anthropic.com
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
header Header Parameters
| Name | Type |
|---|---|
anthropic-version The version of the Claude API you want to use. Read more about versioning and our version history here. | string |
Request Body
application/json
required
interface CountMessageTokensParams {
messages:InputMessage [];
model:Model ;
system?:RequestTextBlock [] | string;
thinking?:ThinkingConfigParam ;
tool_choice?:ToolChoice ;
tools?: (Tool | BashTool20250124 | TextEditor20250124 | TextEditor20250429 | TextEditor20250728 | WebSearchTool20250305 )[];
}
messages:
model:
system?:
thinking?:
tool_choice?:
tools?: (
}
Responses
200 application/json
Successful Response
interface CountMessageTokensResponse {
input_tokens: number;
}
input_tokens: number;
}
Client Errors
4XX application/json
Error response.
See our errors documentation for more details.
interface ErrorResponse {
error:InvalidRequestError | AuthenticationError | BillingError | PermissionError | NotFoundError | RateLimitError | GatewayTimeoutError | APIError | OverloadedError ;
request_id: string | null;
type: "error";
}
error:
request_id: string | null;
type: "error";
}