Skip to main content
POST
Count Tokens (Claude)

Introduction

Calculate the token count of Claude messages to estimate costs before sending requests. This endpoint does not consume quota and only performs local calculations.

Authentication

string
required
Bearer Token, e.g., Bearer sk-xxxxxxxxxx

Request Parameters

string
required
Claude model identifier. Supported models include:
  • claude-opus-4-5-20251101 (Recommended replacement for claude-3-opus)
  • claude-haiku-4-5-20251001
  • claude-sonnet-4-5-20250929
  • claude-sonnet-4-20250514
  • Other Claude series models
array
required
List of conversation messages, each containing role (user/assistant) and content. content can be a string or an array of media content.Supported content types:
  • Plain text messages
  • Multimodal messages (including images)
  • Tool call results
string|array
System prompt (optional), can be a string or an array of media content. Used to set the model’s behavior and role.
array
Tool definitions list (optional), used to calculate tokens related to tool calls.

Response Parameters

number
Total token count of input messages, including:
  • System prompt tokens
  • All messages tokens
  • Tools definition tokens (if any)

Basic Examples

Advanced Use Cases

With Tool Definitions

Multimodal Content

Use Cases

1. Cost Estimation

Calculate token counts before sending bulk requests to estimate costs:

2. Context Window Management

Check if messages exceed the model’s context window limit:

3. Prompt Optimization

Compare token consumption of different prompts:

Important Notes

  • Image tokens use a fixed estimate (~1000 tokens), actual count may vary based on resolution
  • Does not include output-related parameters like max_tokens, only counts input tokens
  • This endpoint does not make actual AI requests and does not consume quota

Error Handling

Missing Required Parameters

Invalid API Key