Count message tokens
Estimates the input-token count of a Messages request without generating anything (and without cost). Accepts the same body as /anthropic/v1/messages, with max_tokens optional.
Unlike Anthropic’s native endpoint, this never calls the underlying provider’s tokenizer: the count is a local, model-aware approximation — for every model, Claude included. It exists so Anthropic SDK clients that rely on count_tokens (such as Claude Code’s remaining-context display) keep working; do not use it for exact billing math. It enforces the same per-key model permissions as message creation.
https://api.deutschlandgpt.de/anthropic/v1/messages/count_tokensExample request
curl https://api.deutschlandgpt.de/anthropic/v1/messages/count_tokens \
-X POST \
-H "Authorization: Bearer $DGPT_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-4.5-sonnet",
"max_tokens": 0,
"messages": [
{
"role": "user",
"content": "string"
}
],
"system": "string",
"tools": [
{
"name": "string"
}
],
"tool_choice": {
"type": "auto"
},
"temperature": 0,
"top_p": 0,
"top_k": 0,
"stop_sequences": [
"string"
],
"stream": false,
"thinking": {
"type": "enabled",
"budget_tokens": 0
},
"metadata": {},
"service_tier": "string"
}'curl https://api.deutschlandgpt.de/anthropic/v1/messages/count_tokens \
-X POST \
-H "Authorization: Bearer $DGPT_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-4.5-sonnet",
"max_tokens": 0,
"messages": [
{
"role": "user",
"content": "string"
}
],
"system": "string",
"tools": [
{
"name": "string"
}
],
"tool_choice": {
"type": "auto"
},
"temperature": 0,
"top_p": 0,
"top_k": 0,
"stop_sequences": [
"string"
],
"stream": false,
"thinking": {
"type": "enabled",
"budget_tokens": 0
},
"metadata": {},
"service_tier": "string"
}'import os, requests
response = requests.post(
"https://api.deutschlandgpt.de/anthropic/v1/messages/count_tokens",
headers={"Authorization": f"Bearer {os.environ['DGPT_API_KEY']}"},
json={
"model": "claude-4.5-sonnet",
"max_tokens": 0,
"messages": [
{
"role": "user",
"content": "string"
}
],
"system": "string",
"tools": [
{
"name": "string"
}
],
"tool_choice": {
"type": "auto"
},
"temperature": 0,
"top_p": 0,
"top_k": 0,
"stop_sequences": [
"string"
],
"stream": False,
"thinking": {
"type": "enabled",
"budget_tokens": 0
},
"metadata": {},
"service_tier": "string"
},
)
print(response.json())import os, requests
response = requests.post(
"https://api.deutschlandgpt.de/anthropic/v1/messages/count_tokens",
headers={"Authorization": f"Bearer {os.environ['DGPT_API_KEY']}"},
json={
"model": "claude-4.5-sonnet",
"max_tokens": 0,
"messages": [
{
"role": "user",
"content": "string"
}
],
"system": "string",
"tools": [
{
"name": "string"
}
],
"tool_choice": {
"type": "auto"
},
"temperature": 0,
"top_p": 0,
"top_k": 0,
"stop_sequences": [
"string"
],
"stream": False,
"thinking": {
"type": "enabled",
"budget_tokens": 0
},
"metadata": {},
"service_tier": "string"
},
)
print(response.json())const response = await fetch('https://api.deutschlandgpt.de/anthropic/v1/messages/count_tokens', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.DGPT_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
"model": "claude-4.5-sonnet",
"max_tokens": 0,
"messages": [
{
"role": "user",
"content": "string"
}
],
"system": "string",
"tools": [
{
"name": "string"
}
],
"tool_choice": {
"type": "auto"
},
"temperature": 0,
"top_p": 0,
"top_k": 0,
"stop_sequences": [
"string"
],
"stream": false,
"thinking": {
"type": "enabled",
"budget_tokens": 0
},
"metadata": {},
"service_tier": "string"
}),
});
console.log(await response.json());const response = await fetch('https://api.deutschlandgpt.de/anthropic/v1/messages/count_tokens', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.DGPT_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
"model": "claude-4.5-sonnet",
"max_tokens": 0,
"messages": [
{
"role": "user",
"content": "string"
}
],
"system": "string",
"tools": [
{
"name": "string"
}
],
"tool_choice": {
"type": "auto"
},
"temperature": 0,
"top_p": 0,
"top_k": 0,
"stop_sequences": [
"string"
],
"stream": false,
"thinking": {
"type": "enabled",
"budget_tokens": 0
},
"metadata": {},
"service_tier": "string"
}),
});
console.log(await response.json());Request body
modelstringrequiredRegistry model name (e.g. claude-4.5-sonnet, gpt-4o, mistral-large). Every hosted model is reachable through this endpoint — not just Claude. Use /anthropic/v1/models to enumerate.
max_tokensintegeroptionalMaximum number of tokens to generate. Required (unlike the OpenAI-compatible endpoints).
messagesAnthropicMessage[]requiredAlternating user / assistant turns. System instructions go in the top-level system field, not here.
rolestringrequiredcontentstring | AnthropicContentBlock[]requiredA bare string (shorthand for one text block) or a list of content blocks.
systemstring | AnthropicTextBlock[]optionalSystem prompt: a bare string or a list of text blocks (each block can carry a cache_control breakpoint).
toolsAnthropicTool[]optionalCustom tools the model may call. Return each tool_use result in a following user message as a tool_result block.
typestringoptionalOmit, or custom. Other values mark unsupported server tools.
namestringrequireddescriptionstringoptionalWhat the tool does — the model uses this to decide when to call it.
input_schemaobjectoptionalJSON Schema of the tool’s arguments.
cache_controlAnthropicCacheControloptionalPrompt-caching breakpoint. Honoured when the request is served by an Anthropic-family host; a no-op for other providers.
typestringrequiredttlstringoptionalCache time-to-live.
tool_choiceAnthropicToolChoiceoptionalHow the model should use the provided tools.
temperaturenumberoptionalSampling temperature. Higher = more random output.
top_pnumberoptionalNucleus sampling.
top_knumberoptionalOnly sample from the top K options per token.
stop_sequencesstring[]optionalCustom sequences that stop generation.
streambooleanoptionalIf true, respond with server-sent events in Anthropic’s streaming format: message_start, content_block_start, content_block_delta, content_block_stop, message_delta, message_stop.
thinkingAnthropicThinkingConfigoptionalExtended-thinking configuration. Mapped to the equivalent reasoning options of whichever provider hosts the selected model.
metadataobjectoptionalRequest metadata.
user_idstring | nulloptionalOpaque end-user identifier.
service_tierstringoptionalAccepted but ignored — routing is decided server-side.
Response
{
"input_tokens": 2095
}{
"input_tokens": 2095
}The estimated input-token count.
input_tokensintegerrequiredResponse codes
The estimated input-token count.
objectInvalid request body.
AnthropicErrorMissing or invalid API key.
AnthropicErrorModel not found, or the API key has no access to it.
AnthropicError