Count a message's tokens

Count the input tokens of an Anthropic Messages request without running it. It's free.

POSThttps://inference.trymorse.ai/v1/messages/count_tokens

Counts the input tokens of an Anthropic Messages request without running it, as Anthropic's token count endpoint does. Claude Code and the Anthropic SDKs receive a count from Morse this way. It is free: it sends nothing to a model's provider, holds and charges no credit, and needs no balance. It does not count toward the account's requests per minute. Each account may count 60 times a minute, on every plan.

On a model whose provider publishes its tokenizer, Morse counts the system prompt, the messages, and the tool definitions with that tokenizer. It adds 32 tokens for each message and 64 for the chat template, which is what Morse bills a prompt it counts. On every other model, and on a request with an image, the count is an upper bound estimated from the request's size. A provider's own count can fall well below it. The model's response may report a different input_tokens: the provider's template and its cache decide the billed count.

A count body is read under the same limits as a Messages body, and no more than 8 MiB. A larger one is refused with 413 request_too_large. The key goes in Authorization: Bearer or in x-api-key. Refusals come in Anthropic's envelope, as POST /v1/messages returns them.

curl -X POST https://inference.trymorse.ai/v1/messages/count_tokens \
  -H "x-api-key: $MORSE_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "glm-5.3",
    "system": "Answer in one sentence.",
    "messages": [
      {
        "role": "user",
        "content": "What is a TEE?"
      }
    ]
  }'
Response
{
  "input_tokens": 142
}

Request body

An Anthropic Messages request to count, as POST /v1/messages takes it with max_tokens left out. Morse reads it as POST /v1/messages does and refuses what that route refuses. stream, max_tokens, and the sampling parameters are read and change nothing. Parameters Morse does not use are ignored.

modelstringRequired

A model id from GET /v1/models. Anthropic's own id for a Claude model, such as claude-opus-5-5 or a dated id like claude-opus-5-5-20261001, also names the Morse model anthropic/claude-opus-5.5.

max_tokensinteger

The output limit. Morse clamps it to the model's limit, as it clamps max_completion_tokens.

messagesarray of objectRequired

The conversation. A user turn takes text, image (base64 source only), and tool_result blocks. A tool_result with is_error true reaches the model with its text prefixed "Error: ". An assistant turn takes text and tool_use blocks. Morse drops thinking and redacted_thinking blocks. A message with role system is sent as a system message in its place. Morse refuses these with a 400 that names the type: document blocks, images linked by URL, images inside a tool_result, and every other block type.

Show its 2 properties
rolestringRequired

One of user, assistant, or system.

contentstring | array of objectRequired
systemstring | array of object

The system prompt, a string or text blocks. Blocks are joined with a blank line. cache_control is ignored: Morse places its own cache breakpoints.

streamboolean

Stream the response as Anthropic's named server-sent events.

toolsarray of object

Custom tools (name, description, input_schema). Anthropic's server tools, such as web_search or code_execution, are refused with a 400 that names the type.

tool_choiceobject

auto, any, tool (with name), or none. disable_parallel_tool_use asks for at most one tool call.

thinkingobject

With enabled and budget_tokens, Morse sets a reasoning effort from the budget. With disabled, it asks for no reasoning, which a model that cannot turn it off runs at its lowest effort. With enabled or adaptive, it also streams the model's reasoning as thinking blocks where the model shows it. Thinking blocks carry no signature.

output_configobject

effort sets the reasoning effort (low, medium, high, xhigh, or max). format takes type json_schema with a schema, on a model whose providers honour json_schema. It needs an API key that lets its settings reach the provider.

stop_sequencesarray of string

Sent as stop. An API key needs forward_settings, as for a chat completion's stop.

temperaturenumber

1, Anthropic's default, is ignored. Any other value needs forward_settings, as for a chat completion.

top_pnumber

1 is ignored. Any other value needs forward_settings, as for a chat completion.

Response

The count.

input_tokensintegerRequired

The request's input tokens: the model's tokenizer count plus Morse's template allowance. Where the model has no published tokenizer, it is an upper bound from the request's size.

Response headers

HeaderWhat it says
X-Morse-VersionThe running build. Every response the server makes carries it. A refusal the inference door makes itself does not, such as its 404 not_found or its 401 for a request with no key.

Errors

Branch on the status: an error returns Anthropic's error shape, whose error.type follows the status. The one exception is 403 service_unavailable_in_region, which returns Morse's error shape with that error.code.

JSON
{
  "type": "error",
  "error": {
    "type": "authentication_error",
    "message": "Incorrect API key provided."
  }
}
Statuserror.type
400invalid_request_error
401authentication_error
402billing_error
403permission_error
404not_found_error
413request_too_large
429rate_limit_error
503overloaded_error
504timeout_error
Any other 5xxapi_error

When to retry applies to these statuses too.

400The request is malformed, or asks for something POST /v1/messages does not serve. The message names the parameter or block. A min_privacy of e2ee, on the key or on the request, is refused here too.
401The key is missing or wrong.
403The key may not use this model or route. A request with E2EE headers or a sealed model name is refused here. Its error type is permission_error. Its message reads: "End-to-end encryption is not offered to API keys for now. Send the request without E2EE headers, to a model id from GET /v1/models."1 code
service_unavailable_in_region

Morse doesn't serve the place the request comes from, because of US sanctions rules and its providers' terms. Every request from there returns this, whatever the key. It is the one refusal here that comes in Morse's own error envelope, {"error":{"message","type":"region","param","code"}}, so its code stays readable; every other refusal comes in Anthropic's.

404The model does not exist, or the key may not see it.
413The request body is too large.
429The account counted too often. Retry after the time in the Retry-After header.
503The service is restarting or could not start the count. Retry.