inference.eu

POST /v1/messages/count_tokens

Count tokens API: measure a prompt before you send it

The count tokens endpoint tells you how many input tokens a request would use, without running the model. It takes the same body as a Messages request and returns a single number. Use it to check that long documents fit the context window, to split work into batches of the right size, and to estimate spend before a large job.

  1. 01API format: Anthropic Messages
  2. 02EU data centres
  3. 03Covered by one DPA
POST /v1/messages/count_tokens
curl https://api.inference.eu/v1/messages/count_tokens \
  -H "x-api-key: $INFERENCE_EU_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-oss-120b",
    "system": "You summarise supplier contracts for a procurement team.",
    "messages": [
      {"role": "user", "content": "Summarise the termination and liability clauses in the contract below.\n\n..."}
    ]
  }'

# {"input_tokens": 2147}

What the endpoint does

POST /v1/messages/count_tokens follows the Anthropic token counting format. You send model, messages and, if you use them, system and tools. The API tokenises everything exactly as it would for a real request, using the tokenizer of the model you name, and returns {"input_tokens": N}.

Nothing is generated and no output tokens are produced, so the call is fast and you do not need max_tokens. The count includes the system prompt, every message, tool definitions and the formatting the model's chat template adds around them, which is why it is more accurate than counting words or running a generic tokenizer yourself.

Why the tokenizer matters

Different open models split text into tokens in different ways. The same contract can be noticeably more tokens for one model than another, especially for German, French and other non-English text, code, or tables. Context windows and token-based billing are both counted in the tokens of the specific model.

That is why the model field is required and why you should count with the model you will actually call. If you switch models, recount. A rough rule of thumb from one tokenizer can be wrong by enough to push a long prompt over the limit, and the API will then reject the request with a 400 error.

A practical pattern for long documents

Before sending a long document, count the tokens of your system prompt, the document and the question together. If the total plus the answer you expect fits within the model's context window, send it as one request. If not, split the document into chunks that each fit, summarise or extract from each chunk, and combine the results in a final call.

The same check is useful for batch jobs. Count a sample of inputs, multiply by the number of rows, and you have a realistic estimate of input volume before you submit the batch, instead of finding out from the invoice.

Errors and edge cases

Errors use the Anthropic format. An unknown model returns not_found_error (404); malformed messages, for example two user turns in a row where the model's template does not allow it, return invalid_request_error (400). A bad key returns 401.

The count covers input only. Output tokens, including reasoning on thinking models, are added when the model generates, so leave room for them. Images in content blocks are counted according to the vision model's own rules, which depend on resolution. Counts reflect the current tokenizer and template for each model, so do not cache them across model versions.

Count tokens: common questions

The questions buyers, developers and legal teams ask us most. Something missing? Ask us directly.

Ask a question

Move your AI workloads to EU infrastructure

Tell us which models you use and roughly how much traffic you have. We reply within one working day with pricing and next steps.

  • Reply within one working day
  • Pricing for your volume
  • DPA ready to sign