Search + K

Command Palette

Search for a command to run...

Sign In

Count tokens in a Message

POST /v1/messages/count_tokens?beta=true
Copy endpoint
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-beta

Optional header to specify the beta version(s) you want to use.

To use multiple betas, use a comma separated list like beta1,beta2 or specify the header multiple times for each beta.

string
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 BetaCountMessageTokensParams {
context_management?: BetaContextManagementConfig
interface BetaContextManagementConfig {
edits?: (BetaClearToolUses20250919 | BetaClearThinking20251015)[];
}
| null
;
mcp_servers?: BetaRequestMCPServerURLDefinition
interface BetaRequestMCPServerURLDefinition {
authorization_token?: string | null;
name: string;
tool_configuration?: BetaRequestMCPServerToolConfiguration | null;
type: "url";
url: string;
}
[]
;
messages: BetaInputMessage
interface BetaInputMessage {
content: BetaInputContentBlock[] | string;
role: "user" | "assistant";
}
[]
;
model: Model

The model that will complete your prompt.\n\nSee models for additional details and options.

type Model = string | "claude-opus-4-5-20251101" | "claude-opus-4-5" | "claude-3-7-sonnet-latest" | "claude-3-7-sonnet-20250219" | "claude-3-5-haiku-latest" | "claude-3-5-haiku-20241022" | "claude-haiku-4-5" | "claude-haiku-4-5-20251001" | "claude-sonnet-4-20250514" | "claude-sonnet-4-0" | "claude-4-sonnet-20250514" | "claude-sonnet-4-5" | "claude-sonnet-4-5-20250929" | "claude-opus-4-0" | "claude-opus-4-20250514" | "claude-4-opus-20250514" | "claude-opus-4-1-20250805" | "claude-3-opus-latest" | "claude-3-opus-20240229" | "claude-3-haiku-20240307"
;
output_config?: BetaOutputConfig
interface BetaOutputConfig {
effort?: BetaEffortLevel | null;
}
;
output_format?: BetaJsonOutputFormat
interface BetaJsonOutputFormat {
schema: { };
type: "json_schema";
}
| null
;
system?: BetaRequestTextBlock
interface BetaRequestTextBlock {
cache_control?: BetaCacheControlEphemeral | null;
citations?: (BetaRequestCharLocationCitation | BetaRequestPageLocationCitation | BetaRequestContentBlockLocationCitation | BetaRequestWebSearchResultLocationCitation | BetaRequestSearchResultLocationCitation)[] | null;
text: string;
type: "text";
}
[]
| string
;
thinking?: BetaThinkingConfigParam

Configuration for enabling Claude's extended thinking.

When enabled, responses include thinking content blocks showing Claude's thinking process before the final answer. Requires a minimum budget of 1,024 tokens and counts towards your max_tokens limit.

See extended thinking for details.

type BetaThinkingConfigParam = BetaThinkingConfigEnabled | BetaThinkingConfigDisabled
;
tool_choice?: BetaToolChoice

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.

type BetaToolChoice = BetaToolChoiceAuto | BetaToolChoiceAny | BetaToolChoiceTool | BetaToolChoiceNone
;
tools?: (BetaTool
interface BetaTool {
type?: "custom" | null;
description?: string;
name: string;
input_schema: BetaInputSchema;
cache_control?: BetaCacheControlEphemeral | null;
strict?: boolean;
allowed_callers?: ("direct" | "code_execution_20250825")[];
defer_loading?: boolean;
input_examples?: { }[];
}
| BetaBashTool20241022
interface BetaBashTool20241022 {
allowed_callers?: ("direct" | "code_execution_20250825")[];
cache_control?: BetaCacheControlEphemeral | null;
defer_loading?: boolean;
input_examples?: { }[];
name: "bash";
strict?: boolean;
type: "bash_20241022";
}
| BetaBashTool20250124
interface BetaBashTool20250124 {
allowed_callers?: ("direct" | "code_execution_20250825")[];
cache_control?: BetaCacheControlEphemeral | null;
defer_loading?: boolean;
input_examples?: { }[];
name: "bash";
strict?: boolean;
type: "bash_20250124";
}
| BetaCodeExecutionTool20250522
interface BetaCodeExecutionTool20250522 {
allowed_callers?: ("direct" | "code_execution_20250825")[];
cache_control?: BetaCacheControlEphemeral | null;
defer_loading?: boolean;
name: "code_execution";
strict?: boolean;
type: "code_execution_20250522";
}
| BetaCodeExecutionTool20250825
interface BetaCodeExecutionTool20250825 {
allowed_callers?: ("direct" | "code_execution_20250825")[];
cache_control?: BetaCacheControlEphemeral | null;
defer_loading?: boolean;
name: "code_execution";
strict?: boolean;
type: "code_execution_20250825";
}
| BetaComputerUseTool20241022
interface BetaComputerUseTool20241022 {
allowed_callers?: ("direct" | "code_execution_20250825")[];
cache_control?: BetaCacheControlEphemeral | null;
defer_loading?: boolean;
display_height_px: number;
display_number?: number | null;
display_width_px: number;
input_examples?: { }[];
name: "computer";
strict?: boolean;
type: "computer_20241022";
}
| BetaMemoryTool20250818
interface BetaMemoryTool20250818 {
allowed_callers?: ("direct" | "code_execution_20250825")[];
cache_control?: BetaCacheControlEphemeral | null;
defer_loading?: boolean;
input_examples?: { }[];
name: "memory";
strict?: boolean;
type: "memory_20250818";
}
| BetaComputerUseTool20250124
interface BetaComputerUseTool20250124 {
allowed_callers?: ("direct" | "code_execution_20250825")[];
cache_control?: BetaCacheControlEphemeral | null;
defer_loading?: boolean;
display_height_px: number;
display_number?: number | null;
display_width_px: number;
input_examples?: { }[];
name: "computer";
strict?: boolean;
type: "computer_20250124";
}
| BetaTextEditor20241022
interface BetaTextEditor20241022 {
allowed_callers?: ("direct" | "code_execution_20250825")[];
cache_control?: BetaCacheControlEphemeral | null;
defer_loading?: boolean;
input_examples?: { }[];
name: "str_replace_editor";
strict?: boolean;
type: "text_editor_20241022";
}
| BetaComputerUseTool20251124
interface BetaComputerUseTool20251124 {
allowed_callers?: ("direct" | "code_execution_20250825")[];
cache_control?: BetaCacheControlEphemeral | null;
defer_loading?: boolean;
display_height_px: number;
display_number?: number | null;
display_width_px: number;
enable_zoom?: boolean;
input_examples?: { }[];
name: "computer";
strict?: boolean;
type: "computer_20251124";
}
| BetaTextEditor20250124
interface BetaTextEditor20250124 {
allowed_callers?: ("direct" | "code_execution_20250825")[];
cache_control?: BetaCacheControlEphemeral | null;
defer_loading?: boolean;
input_examples?: { }[];
name: "str_replace_editor";
strict?: boolean;
type: "text_editor_20250124";
}
| BetaTextEditor20250429
interface BetaTextEditor20250429 {
allowed_callers?: ("direct" | "code_execution_20250825")[];
cache_control?: BetaCacheControlEphemeral | null;
defer_loading?: boolean;
input_examples?: { }[];
name: "str_replace_based_edit_tool";
strict?: boolean;
type: "text_editor_20250429";
}
| BetaTextEditor20250728
interface BetaTextEditor20250728 {
allowed_callers?: ("direct" | "code_execution_20250825")[];
cache_control?: BetaCacheControlEphemeral | null;
defer_loading?: boolean;
input_examples?: { }[];
max_characters?: number | null;
name: "str_replace_based_edit_tool";
strict?: boolean;
type: "text_editor_20250728";
}
| BetaWebSearchTool20250305
interface BetaWebSearchTool20250305 {
allowed_callers?: ("direct" | "code_execution_20250825")[];
allowed_domains?: string[] | null;
blocked_domains?: string[] | null;
cache_control?: BetaCacheControlEphemeral | null;
defer_loading?: boolean;
max_uses?: number | null;
name: "web_search";
strict?: boolean;
type: "web_search_20250305";
user_location?: BetaUserLocation | null;
}
| BetaWebFetchTool20250910
interface BetaWebFetchTool20250910 {
allowed_callers?: ("direct" | "code_execution_20250825")[];
allowed_domains?: string[] | null;
blocked_domains?: string[] | null;
cache_control?: BetaCacheControlEphemeral | null;
citations?: BetaRequestCitationsConfig | null;
defer_loading?: boolean;
max_content_tokens?: number | null;
max_uses?: number | null;
name: "web_fetch";
strict?: boolean;
type: "web_fetch_20250910";
}
| BetaToolSearchToolBM2520251119
interface BetaToolSearchToolBM2520251119 {
allowed_callers?: ("direct" | "code_execution_20250825")[];
cache_control?: BetaCacheControlEphemeral | null;
defer_loading?: boolean;
name: "tool_search_tool_bm25";
strict?: boolean;
type: "tool_search_tool_bm25_20251119" | "tool_search_tool_bm25";
}
| BetaToolSearchToolRegex20251119
interface BetaToolSearchToolRegex20251119 {
allowed_callers?: ("direct" | "code_execution_20250825")[];
cache_control?: BetaCacheControlEphemeral | null;
defer_loading?: boolean;
name: "tool_search_tool_regex";
strict?: boolean;
type: "tool_search_tool_regex_20251119" | "tool_search_tool_regex";
}
| BetaMCPToolset

Configuration for a group of tools from an MCP server.

Allows configuring enabled status and defer_loading for all tools from an MCP server, with optional per-tool overrides.

interface BetaMCPToolset {
cache_control?: BetaCacheControlEphemeral | null;
configs?: { } | null;
default_config?: BetaMCPToolDefaultConfig;
mcp_server_name: string;
type: "mcp_toolset";
}
)[]
;
}

Responses

200 application/json

Successful Response

interface BetaCountMessageTokensResponse {
context_management: BetaContextManagementResponse
interface BetaContextManagementResponse {
original_input_tokens: number;
}
| null
;
input_tokens: number;
}

Client Errors

4XX application/json

Error response.

See our errors documentation for more details.

interface BetaErrorResponse {
error: BetaInvalidRequestError
interface BetaInvalidRequestError {
message: string;
type: "invalid_request_error";
}
| BetaAuthenticationError
interface BetaAuthenticationError {
message: string;
type: "authentication_error";
}
| BetaBillingError
interface BetaBillingError {
message: string;
type: "billing_error";
}
| BetaPermissionError
interface BetaPermissionError {
message: string;
type: "permission_error";
}
| BetaNotFoundError
interface BetaNotFoundError {
message: string;
type: "not_found_error";
}
| BetaRateLimitError
interface BetaRateLimitError {
message: string;
type: "rate_limit_error";
}
| BetaGatewayTimeoutError
interface BetaGatewayTimeoutError {
message: string;
type: "timeout_error";
}
| BetaAPIError
interface BetaAPIError {
message: string;
type: "api_error";
}
| BetaOverloadedError
interface BetaOverloadedError {
message: string;
type: "overloaded_error";
}
;
request_id: string | null;
type: "error";
}