Zum Inhalt springen
DeutschlandGPT

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.

POSThttps://api.deutschlandgpt.de/anthropic/v1/messages/count_tokens

Example 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"
}'

Request body

modelstringrequired

Registry 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_tokensintegeroptional

Maximum number of tokens to generate. Required (unlike the OpenAI-compatible endpoints).

messagesAnthropicMessage[]required

Alternating user / assistant turns. System instructions go in the top-level system field, not here.

rolestringrequired
contentstring | AnthropicContentBlock[]required

A bare string (shorthand for one text block) or a list of content blocks.

systemstring | AnthropicTextBlock[]optional

System prompt: a bare string or a list of text blocks (each block can carry a cache_control breakpoint).

toolsAnthropicTool[]optional

Custom tools the model may call. Return each tool_use result in a following user message as a tool_result block.

typestringoptional

Omit, or custom. Other values mark unsupported server tools.

namestringrequired
descriptionstringoptional

What the tool does — the model uses this to decide when to call it.

input_schemaobjectoptional

JSON Schema of the tool’s arguments.

cache_controlAnthropicCacheControloptional

Prompt-caching breakpoint. Honoured when the request is served by an Anthropic-family host; a no-op for other providers.

typestringrequired
ttlstringoptional

Cache time-to-live.

tool_choiceAnthropicToolChoiceoptional

How the model should use the provided tools.

temperaturenumberoptional

Sampling temperature. Higher = more random output.

top_pnumberoptional

Nucleus sampling.

top_knumberoptional

Only sample from the top K options per token.

stop_sequencesstring[]optional

Custom sequences that stop generation.

streambooleanoptional

If 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.

thinkingAnthropicThinkingConfigoptional

Extended-thinking configuration. Mapped to the equivalent reasoning options of whichever provider hosts the selected model.

metadataobjectoptional

Request metadata.

user_idstring | nulloptional

Opaque end-user identifier.

service_tierstringoptional

Accepted but ignored — routing is decided server-side.

Response

200application/json
{
  "input_tokens": 2095
}

The estimated input-token count.

input_tokensintegerrequired

Response codes

200

The estimated input-token count.

object
400

Invalid request body.

AnthropicError
401

Missing or invalid API key.

AnthropicError
404

Model not found, or the API key has no access to it.

AnthropicError
Was this page helpful?