Skip to main content
POST
Messages Count Tokens

Overview

Use POST /api/v1/messages/count_tokens to estimate the number of input tokens for an Anthropic Messages-format request. This endpoint is useful for request planning, context budgeting, and cost control. The result is an estimate and may not exactly match a provider tokenizer.

Endpoint

Authentication

Use either header:
  • Authorization: Bearer YOUR_API_KEY
  • x-api-key: YOUR_API_KEY

Request Body

Supported Content

The request uses Anthropic message format and supports:
  • text blocks
  • image and document-like blocks
  • tool use blocks
  • tool result blocks
  • system
  • tools
  • tool_choice

Limits

  • tools can hold up to 1,024 tools and 1 MiB (1,048,576 bytes) of JSON. Requests over a limit return a 400 invalid_request_error.
  • A request body over 1.5 MiB returns 413.

Response

Example

Notes

  • This is an estimate for planning and cost control.
  • It may not exactly match a provider tokenizer.
  • For generation, use POST /api/v1/messages.