---
title: "Count Message Tokens"
method: POST
path: "/v1/messages/count_tokens"
tags: ["messages"]
---

# Count Message Tokens

`POST /v1/messages/count_tokens`

Anthropic ``/v1/messages/count_tokens``-compatible endpoint.

Returns ``{"input_tokens": N}`` without contacting an upstream provider:
counting is local, so there is no budget reservation, pricing, or usage
logging. Authentication mirrors :func:`create_message` — hybrid mode
resolves the caller's token against the platform, standalone mode validates
the API key — so the endpoint is not an open token-counting oracle.

## Request body

- CountTokensRequest — Anthropic ``/v1/messages/count_tokens`` request. A subset of :class:`MessagesRequest`: the input fields that affect the token count, minus ``max_tokens`` and the streaming/sampling controls, since the endpoint only counts input tokens. ``context_management`` and ``betas`` are accepted for wire compatibility, but the local estimate does not apply provider-side context edits. Clients such as Claude Code call this on every turn to keep their prompt within the model's context window.
  - `betas` string[], nullable
  - `cache_control` object, nullable
  - `context_management` object, nullable
  - `messages` object[], required
  - `metadata` object, nullable
  - `model` string, required
  - `system` union
    - string
    - object[]
  - `thinking` object, nullable
  - `tool_choice` object, nullable
  - `tools` object[], nullable

## Response `200`

Successful Response

- CountTokensResponse — Anthropic ``/v1/messages/count_tokens`` response.
  - `input_tokens` integer, required

## Other responses

- `422` — Validation Error

## Changes

- **2026-08-06** `ff1546601235` — 2 info
  - added the new optional request property `betas`
  - added the new optional request property `context_management`
- **2026-07-20** `d9a87177ce4a` — 1 info
  - the endpoint scheme security `XApiKeyAuth` was added to the API
- **2026-07-14** `0ad4e5f1cc3e` — 1 info
  - the endpoint scheme security `ApiKeyAuth` was added to the API
- **2026-06-09** `6a65636f8e3d` — 1 breaking, 3 info
  - the response's body type changed from no type to `object` for status `200`
  - added the new optional request property `cache_control`
  - added the new optional request property `metadata`
  - added the required property `input_tokens` to the response with the `200` status
- **2026-06-09** `a7ed55e344b0` — 1 info
  - endpoint added

[Full history](https://skmtc.dev/mozilla-ai/apis/otari/changes/v1/messages/count_tokens/post.md)

---

[API](https://skmtc.dev/mozilla-ai/apis/otari.md) · [All operations](https://skmtc.dev/mozilla-ai/apis/otari/llms.txt) · [OpenAPI document](https://skmtc.dev/mozilla-ai/apis/otari/revisions/c05befed4db4?raw)
