Token Counting
Count tokens in your messages before making API calls
OpenModel provides a token counting endpoint that lets you estimate the number of tokens in your messages before making an API call. This is useful for cost estimation, context window management, and prompt optimization.
Endpoint
POST /v1/messages/count_tokensThis endpoint follows the Anthropic token counting API format. It accepts the same messages and system parameters as the Messages API and returns a token count without actually generating a response.
Request Format
The request body is similar to a regular Messages API call, but without max_tokens:
curl https://api.openmodel.ai/v1/messages/count_tokens \
-H "x-api-key: $OPENMODEL_API_KEY" \
-H "Content-Type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-sonnet-4-6",
"messages": [
{"role": "user", "content": "What is the meaning of life?"}
]
}'With System Prompt
You can include a system prompt to get a more accurate count:
result = client.messages.count_tokens(
model="claude-sonnet-4-6",
system="You are a helpful assistant.",
messages=[
{"role": "user", "content": "Explain quantum computing in simple terms."}
],
)
print(f"Input tokens: {result.input_tokens}")Response Format
The endpoint returns a JSON object with the token count:
{
"input_tokens": 15
}The input_tokens field represents the total number of tokens across all messages and the system prompt (if provided).
Use Cases
Cost Estimation
Calculate the approximate cost of a request before making it:
TOKEN_PRICES = {
"claude-sonnet-4-6": {"input": 3.00, "output": 15.00},
}
result = client.messages.count_tokens(
model="claude-sonnet-4-6",
messages=my_messages,
)
price = TOKEN_PRICES["claude-sonnet-4-6"]
input_cost = (result.input_tokens / 1_000_000) * price["input"]
print(f"Estimated input cost: ${input_cost:.6f}")Context Window Management
Check whether your messages fit within the model's context window before sending:
MAX_CONTEXT = 200_000
result = client.messages.count_tokens(
model="claude-sonnet-4-6",
messages=my_messages,
)
if result.input_tokens > MAX_CONTEXT * 0.9:
print("Warning: approaching context window limit")Prompt Optimization
Compare token counts of different prompt formulations to find the most efficient one:
prompts = [
"Summarize the following text:",
"TL;DR:",
"Give a brief summary:",
]
for prompt in prompts:
result = client.messages.count_tokens(
model="claude-sonnet-4-6",
messages=[{"role": "user", "content": f"{prompt} {my_text}"}],
)
print(f"'{prompt}' → {result.input_tokens} tokens")Estimation Without the API
For rough estimates without an API call, you can use these approximations:
- English text: ~1 token per 4 characters, or ~0.75 tokens per word
- Code: Similar density to English text
- Chinese / Japanese / Korean: ~1–2 tokens per character
These are rough heuristics. For accurate counts, use the token counting endpoint.
Next Steps
- Billing & Pricing — Understand cost calculation and pricing tiers
- Available Models — Browse models and their context windows