Claude API Token Counting for Budgeting: A Practical Guide
If you're building on the Claude API, token counting is how you turn an unpredictable bill into a forecastable line item. Tokens are the unit Anthropic uses to measure both input (prompt, system message, tool definitions, conversation history) and output (the model's response), and every dollar you spend maps directly to a token count. Budgeting without counting tokens first is like estimating a cloud bill without knowing your compute hours.
This guide covers how Claude tokenizes text, how to count tokens before and after a call, how to translate that into a monthly budget, and how to cap spend per customer or feature so nothing surprises you at the end of the month.
How Claude Counts Tokens
Claude's tokenizer is not word-based or character-based — it splits text into subword units. Rough rule of thumb for English text:
- ~4 characters per token on average
- ~0.75 tokens per word
- Code, JSON, and non-English text often tokenize less efficiently (more tokens per character)
This means a 1,000-word blog post is roughly 1,300–1,400 tokens, but a 1,000-line JSON payload could easily be 3,000+ tokens because of punctuation, quotes, and repeated keys.
For budgeting purposes, never guess. Use the actual counting mechanisms available to you:
- Pre-flight estimation — count tokens in your prompt before sending the request, so you can reject or truncate oversized inputs before paying for them.
- Post-response accounting — every Claude API response includes a
usageobject withinput_tokensandoutput_tokens. This is your ground truth for billing.
Counting Tokens Before You Send
Anthropic's API doesn't bill you for counting — a dedicated token-counting endpoint lets you check size before committing to a full request. If you're using a proxy like SubToAPI, the same request/response shape applies, so your existing token-counting logic doesn't need to change when you switch where the request is routed.
curl https://api.anthropic.com/v1/messages/count_tokens \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4-5",
"messages": [{"role": "user", "content": "Summarize this contract in 3 bullet points."}]
}'
The response gives you input_tokens directly, before you spend a cent on generation. This is the key move for budgeting: estimate cost before the call, not just after.
Counting Tokens After the Response
Every completed request returns usage data alongside the content:
{
"usage": {
"input_tokens": 512,
"output_tokens": 184
}
}
Log this on every single call. If you're not persisting usage per request, you have no way to audit where your budget actually went — you only know the total invoice, not which feature, customer, or prompt caused it.
Turning Token Counts Into a Budget
Once you can count tokens reliably, budgeting is arithmetic:
- Price per token — check current per-model pricing for input and output tokens (output is typically priced higher than input).
- Average tokens per request — measure this from real usage, not assumptions. System prompts, tool definitions, and conversation history all count as input tokens and add up fast in multi-turn chats.
- Expected request volume — per day, per customer, per feature.
- Multiply and add margin —
tokens × price × volume, then add 20–30% headroom for retries, longer-than-expected outputs, and traffic spikes.
A practical budgeting formula:
daily_cost = (avg_input_tokens × input_price + avg_output_tokens × output_price) × requests_per_day
Run this per feature if you have several (chat, summarization, classification) because token profiles differ wildly — a classifier might use 50 output tokens per call while a long-form writer uses 2,000.
Where Token Costs Hide
Common budget leaks that don't show up until the invoice arrives:
- System prompts repeated on every call — a 500-token system prompt sent 100,000 times a month is 50M tokens before a single user message is processed.
- Tool definitions — every tool schema you pass counts as input tokens on every request, even if the model doesn't call the tool.
- Conversation history — multi-turn chat apps resend the entire history each turn unless you implement summarization or truncation.
- Streaming vs. non-streaming — token counts are identical either way, but streaming lets you cut off a response early if it's clearly going off-track, saving output tokens.
Capping Spend in Practice
Token counting is only useful if it feeds into an actual cap. A few patterns that work:
- Per-request ceiling: set
max_tokensconservatively on every call so a single runaway generation can't blow a daily budget. - Per-customer metering: log
usage.input_tokensandusage.output_tokenstagged by customer ID or API key, then roll up daily/monthly totals. - Pre-flight rejection: if a pre-flight count shows a request exceeds your per-call token budget, truncate the input or reject it before sending.
If you're exposing Claude to multiple team members or customers through your own product, this is exactly the kind of accounting that gets painful fast with a shared raw API key. SubToAPI issues separate sub_live_... keys per application or team member, and every response includes the same usage metadata you'd get from the underlying model — so you can attribute token spend to a specific key without building your own metering layer. See /docs/messages for the response shape and /pricing for plan details if you want seat-based access control on top of your own budget caps.
A Minimal Logging Setup
const response = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"content-type": "application/json"
},
body: JSON.stringify({
model: "claude-sonnet-4-5",
max_tokens: 1024,
messages: [{ role: "user", content: userPrompt }]
})
});
const data = await response.json();
await logUsage({
customerId: user.id,
inputTokens: data.usage.input_tokens,
outputTokens: data.usage.output_tokens,
model: data.model
});
This single logUsage call, run on every request, is enough to build accurate per-customer or per-feature dashboards without any third-party analytics tool. Start with /docs/quickstart if you're setting this up for the first time, and review /docs/streaming if your app streams responses — usage totals still arrive at the end of the stream.
Getting Started
Token counting isn't optional overhead — it's the only reliable way to forecast and control Claude API costs. Count before you send, log after every response, and roll usage up by customer or feature so budget overruns are visible before they become a surprise invoice. Try it out with a free trial at /signup.
FAQ
Does counting tokens before sending cost anything? No. Anthropic's token-counting endpoint is free to call and doesn't trigger generation, so you can estimate cost before committing to a full request.
Are input and output tokens priced the same? No, output tokens are typically priced higher than input tokens. Factor this into your budget formula separately rather than averaging the two.
How do I budget for multi-turn conversations? Track cumulative input tokens across the whole conversation history, not just the latest message — each turn resends prior turns unless you truncate or summarize, so costs grow with conversation length.