Count tokens

Count the number of input tokens an Anthropic-format Messages request would consume, without generating a message.

POST /messages/count_tokens

Count input tokens for an Anthropic-compatible Messages request.

Parameters

Header parameters

FieldTypeRequiredDescription
anthropic-versionstringNoAnthropic API version header. Accepted for SDK compatibility.
x-api-keystringNoAPI key header used by Anthropic SDKs. Authorization: Bearer is also accepted.

Request body

Content Type: application/json

AnthropicCountTokensRequest

Response

HTTP 200 — Success

Content Type: application/json

AnthropicCountTokensResponse

python
import os
from anthropic import Anthropic
client = Anthropic(
base_url="https://api.meta.ai",
auth_token=os.environ["MODEL_API_KEY"],
)
message = client.messages.count_tokens(
model="muse-spark-1.3",
messages=[
{
"role": "user",
"content": "What is the capital of France?",
},
],
)
print(message.model_dump_json(indent=2))

For usage examples and rate-limit guidance that applies to the shared inference pipeline, see the Responses feature page.