anthropic_count_tokens
POST
/v1/messages/count_tokens
const url = 'https://example.com/v1/messages/count_tokens';const options = { method: 'POST', headers: {'Content-Type': 'application/json'}, body: '{"agent_permission":null,"code_execution_permission":null,"dry_allowed_length":1,"dry_base":1,"dry_multiplier":1,"dry_sequence_breakers":["example"],"enable_code_execution":true,"enable_thinking":true,"files":["example"],"frequency_penalty":1,"grammar":{"type":"regex","value":"example"},"logit_bias":"example","logprobs":true,"max_tokens":1,"max_tool_rounds":1,"messages":[{"content":[{"cache_control":"example","citations":"example","content":"example","id":"example","input":"example","is_error":true,"name":"example","signature":"example","source":{"data":"example","media_type":"example","type":"example","url":"example"},"text":"example","thinking":"example","tool_use_id":"example","type":"example"}],"role":"example"}],"metadata":"example","min_p":1,"model":"example","presence_penalty":1,"reasoning_effort":"example","repetition_penalty":1,"response_format":{"type":"text"},"session_id":"example","stop_sequences":["example"],"stream":true,"system":"example","temperature":1,"thinking":{"budget_tokens":1,"type":"example"},"tool_choice":{"name":"example","type":"example"},"tools":[{"allowed_domains":["example"],"blocked_domains":["example"],"description":"example","input_schema":"example","max_uses":1,"name":"example","type":"example","user_location":{"city":"example","country":"example","region":"example","timezone":"example","type":"example"}}],"top_k":1,"top_logprobs":1,"top_p":1,"truncate_sequence":true,"web_search_options":{"extract_description":"example","search_context_size":"low","search_description":"example","user_location":{"approximate":{"city":"example","country":"example","region":"example","timezone":"example"},"type":"approximate"}}}'};
try { const response = await fetch(url, options); const data = await response.json(); console.log(data);} catch (error) { console.error(error);}curl --request POST \ --url https://example.com/v1/messages/count_tokens \ --header 'Content-Type: application/json' \ --data '{ "agent_permission": null, "code_execution_permission": null, "dry_allowed_length": 1, "dry_base": 1, "dry_multiplier": 1, "dry_sequence_breakers": [ "example" ], "enable_code_execution": true, "enable_thinking": true, "files": [ "example" ], "frequency_penalty": 1, "grammar": { "type": "regex", "value": "example" }, "logit_bias": "example", "logprobs": true, "max_tokens": 1, "max_tool_rounds": 1, "messages": [ { "content": [ { "cache_control": "example", "citations": "example", "content": "example", "id": "example", "input": "example", "is_error": true, "name": "example", "signature": "example", "source": { "data": "example", "media_type": "example", "type": "example", "url": "example" }, "text": "example", "thinking": "example", "tool_use_id": "example", "type": "example" } ], "role": "example" } ], "metadata": "example", "min_p": 1, "model": "example", "presence_penalty": 1, "reasoning_effort": "example", "repetition_penalty": 1, "response_format": { "type": "text" }, "session_id": "example", "stop_sequences": [ "example" ], "stream": true, "system": "example", "temperature": 1, "thinking": { "budget_tokens": 1, "type": "example" }, "tool_choice": { "name": "example", "type": "example" }, "tools": [ { "allowed_domains": [ "example" ], "blocked_domains": [ "example" ], "description": "example", "input_schema": "example", "max_uses": 1, "name": "example", "type": "example", "user_location": { "city": "example", "country": "example", "region": "example", "timezone": "example", "type": "example" } } ], "top_k": 1, "top_logprobs": 1, "top_p": 1, "truncate_sequence": true, "web_search_options": { "extract_description": "example", "search_context_size": "low", "search_description": "example", "user_location": { "approximate": { "city": "example", "country": "example", "region": "example", "timezone": "example" }, "type": "approximate" } } }'Request Body required
Section titled “Request Body required ” Media type application/json
object
agent_permission
string | null
Example
null code_execution_permission
string | null
Example
null dry_allowed_length
integer | null
dry_base
number | null format: float
dry_multiplier
number | null format: float
dry_sequence_breakers
Array<string> | null
enable_code_execution
boolean
enable_thinking
boolean | null
files
Array | null
frequency_penalty
number | null format: float
grammar
One of:
null
One of:
object
type
required
string
value
required
string
object
type
required
string
value
required
object
object
type
required
string
value
required
Top-level grammar configuration for LLGuidance
object
grammars
required
List of grammar configurations
Array<object>
Grammar configuration with lexer settings
object
json_schema
The JSON schema that the grammar should generate
object | null
lark_grammar
The Lark grammar that the grammar should generate
string | null
name
The name of this grammar, can be used in GenGrammar nodes
string | null
max_tokens
Maximum number of tokens to generate
integer | null
object
type
required
string
value
required
string
logit_bias
object | null
logprobs
boolean
max_tokens
integer | null
max_tool_rounds
integer | null
messages
required
Array<object>
object
content
required
One of:
string
Array<object>
object
cache_control
object | null
citations
object | null
content
object | null
id
string | null
input
object | null
is_error
boolean | null
name
string | null
signature
string | null
source
text
string | null
thinking
string | null
tool_use_id
string | null
type
required
string
role
required
string
metadata
object | null
min_p
number | null format: double
model
string
presence_penalty
number | null format: float
reasoning_effort
string | null
repetition_penalty
number | null format: float
response_format
session_id
string | null
stop_sequences
Array<string> | null
stream
boolean | null
system
One of:
null
One of:
string
Array<object>
object
cache_control
object | null
citations
object | null
content
object | null
id
string | null
input
object | null
is_error
boolean | null
name
string | null
signature
string | null
source
text
string | null
thinking
string | null
tool_use_id
string | null
type
required
string
temperature
number | null format: double
tools
Array<object> | null
object
allowed_domains
Array<string> | null
blocked_domains
Array<string> | null
description
string | null
input_schema
object | null
max_uses
integer | null
name
string | null
type
string | null
top_k
integer | null
top_logprobs
integer | null
top_p
number | null format: double
truncate_sequence
boolean | null
web_search_options
One of:
null
object
extract_description
Override the description for the extraction tool.
string | null
search_description
Override the description for the search tool.
string | null
Responses
Section titled “ Responses ”Anthropic message token count
Media type application/json
object
input_tokens
required
integer
Example generated
{ "input_tokens": 1}