Skip to main content
POST
Count Tokens

Request

Body

Response

Use Cases

  • Cost estimation: Calculate the cost of a request before sending it
  • Context window management: Ensure your messages fit within the model’s context window
  • Token budgeting: Allocate token budgets across multiple requests

Example

Authorizations

x-api-key
string
header
required

API key authentication via x-api-key header

Headers

anthropic-version
string
default:2023-06-01

Anthropic API version

Body

application/json
model
string
required

Model ID to count tokens for

messages
object[]
required

Messages to count tokens for

system
string

System prompt to include in token count

Response

200 - application/json

Token count response

input_tokens
integer

Number of input tokens