---
title: "Count message input tokens (Anthropic-compatible)"
method: POST
path: "/messages/count_tokens"
tags: ["Messages"]
---

# Count message input tokens (Anthropic-compatible)

`POST /messages/count_tokens`

Estimates input tokens for an Anthropic-compatible Messages request without running or billing the model. max_tokens is optional. The per-key rate limit applies. anthropic-beta and Claude Code compatibility headers are accepted and ignored.

## Headers

- `anthropic-version` string
- `anthropic-beta` string

## Request body

- AnthropicMessageRequest — Anthropic-compatible Messages request. POST /messages additionally requires max_tokens. Unknown top-level fields are accepted and ignored.
  - `model` string, required — Model id returned by GET /v1/models.
  - `max_tokens` integer — Required by POST /messages and optional for POST /messages/count_tokens. Reasoning spends this budget. 0 is refused.
  - `messages` AnthropicMessageParam[], required
    - `role` 'user' | 'assistant', required
    - `content` union, required — A string or an array of supported content blocks. Base64 images require a model whose page lists image input. URL and file image sources, document blocks and unknown block types are refused with 400 invalid_request_error.
      - string
      - AnthropicMessageContentBlock[]
        - union
          - AnthropicTextBlock
            - `type` 'text', required
            - `text` string, required
            - `cache_control` unknown
          - AnthropicImageBlock
            - `type` 'image', required
            - `source` AnthropicBase64ImageSource, required
              - …
            - `cache_control` unknown
          - AnthropicToolUseBlock
            - `type` 'tool_use', required
            - `id` string, required
            - `name` string, required
            - `input` object, required
          - AnthropicToolResultBlock
            - `type` 'tool_result', required
            - `tool_use_id` string, required
            - `content` union, required
              - …
            - `is_error` boolean
            - `cache_control` unknown
          - AnthropicThinkingBlock
            - `type` 'thinking', required
            - `thinking` string, required
            - `signature` string, required — Opaque compatibility value. It is not an Anthropic attestation.
          - AnthropicRedactedThinkingBlock
            - `type` 'redacted_thinking', required
            - `data` string, required
  - `system` union — A string or an array of text blocks. Array blocks remain separate prompt parts. Block-level cache_control is accepted and ignored.
    - string
    - AnthropicTextBlock[]
      - `type` 'text', required
      - `text` string, required
      - `cache_control` unknown
  - `temperature` number — Passed through.
  - `top_p` number — Passed through.
  - `top_k` unknown
  - `stop_sequences` string[] — Passed through as stop strings. The matched sequence is not reported.
  - `stream` boolean — true returns text/event-stream in the Anthropic event grammar.
  - `tools` AnthropicToolDefinition[] — Client tools only. Anthropic server tools are refused.
    - `name` string, required
    - `description` string
    - `input_schema` object, required
    - `strict` unknown
    - `defer_loading` unknown
    - `cache_control` unknown
    - `input_examples` unknown
  - `tool_choice` union — auto, none, any, or a named client tool. any requires a tool call. On models whose page says forced tool calling is limited, any with several tools is refused, so use auto or name one tool. disable_parallel_tool_use belongs inside this object. Anthropic server tools are refused.
    - object
      - `type` 'auto', required
      - `disable_parallel_tool_use` boolean
    - object
      - `type` 'none', required
      - `disable_parallel_tool_use` boolean
    - object
      - `type` 'any', required
      - `disable_parallel_tool_use` boolean
    - object
      - `type` 'tool', required
      - `name` string, required
      - `disable_parallel_tool_use` boolean
  - `thinking` AnthropicThinkingConfig — enabled and adaptive keep reasoning on, including enabling it on deepseek-v4-pro. disabled turns reasoning off where supported and is ignored with a hint response header where the model cannot disable reasoning.
    - `type` 'enabled' | 'adaptive' | 'disabled', required
    - `budget_tokens` integer — Accepted and ignored. Reasoning spends max_tokens.
  - `metadata` AnthropicMetadata
    - `user_id` string — Stable routing-affinity hint.
  - `output_config` AnthropicOutputConfig
    - `effort` 'low' | 'medium' | 'high' | 'xhigh' | 'max' — Selects reasoning effort when the model accepts the value.
    - `format` AnthropicOutputFormat
      - `type` 'json_schema', required
      - `schema` object, required
  - `cache_control` unknown
  - `service_tier` unknown
  - `container` unknown
  - `mcp_servers` unknown
  - `context_management` unknown
  - `inference_geo` unknown
  - `speed` unknown

## Response `200`

Estimated input token count

- AnthropicTokenCount
  - `input_tokens` integer, required — Estimated input tokens. The operation does not run or bill the model.

## Other responses

- `400` — Missing or unsupported field, named in the message
- `401` — Missing or invalid API key
- `402` — Prepaid credit balance too low for this request
- `403` — Key scope mismatch or plan limit exceeded
- `404` — Model not available to this key
- `409` — Idempotency conflict
- `413` — Request body exceeds the published size ceiling and returns request_too_large
- `429` — Rate limit or concurrency limit reached
- `500` — 500 or 502 transient failure with error.type api_error
- `502` — 500 or 502 transient failure with error.type api_error
- `503` — 503 temporary unavailability with error.type overloaded_error
- `504` — 504 inference deadline with error.type timeout_error

---

[API](https://skmtc.dev/runinfra/apis/runinfra-api.md) · [All operations](https://skmtc.dev/runinfra/apis/runinfra-api/llms.txt) · [OpenAPI document](https://skmtc-service-production.skmtc.workers.dev/v1/apis/runinfra/runinfra-api/revisions/dbd314122f80/schema)
