Counting Tokens and Estimating LLM API Costs
LLM APIs bill by the token, a chunk of text that is often a word or part of a word. Input and output tokens are priced separately, and output tokens cost several times more than input tokens.
Count tokens before you send
Token counts depend on the model's tokenizer, so use the provider's counting endpoint rather than a generic tokenizer library.
const count = await client.messages.countTokens({
model: 'claude-opus-5-5',
system: systemPrompt,
messages: [{ role: 'user', content: longDocument }],
});
console.log(count.input_tokens);
count = client.messages.count_tokens(
model="claude-opus-5-5",
system=system_prompt,
messages=[{"role": "user", "content": long_document}],
)
print(count.input_tokens)
Read actual usage from every response
const { input_tokens, output_tokens, cache_read_input_tokens } = response.usage;
Log these numbers per feature or per customer. It is the only reliable way to find out where the money goes.
A simple cost formula
// Prices are per million tokens; check the provider's pricing page for current values
const PRICE = { input: 4.0, output: 20.0, cacheRead: 0.2 };
function costUSD(u) {
return (
(u.input_tokens * PRICE.input +
u.output_tokens * PRICE.output +
(u.cache_read_input_tokens ?? 0) * PRICE.cacheRead) / 1_000_000
);
}
Ways to spend less
- Cache repeated prefixes (system prompts, documents, tool definitions).
- Trim what you send: summaries of old chat turns instead of the full history, only the relevant document chunks.
- Ask for shorter output when you don't need long prose. Output tokens are the expensive ones.
- Use a lower effort setting or a smaller model for simple routes such as classification.
- Use the Batches API for work that doesn't need an instant answer; it is billed at a discount.
Was this article helpful?
Comments
No comments yet. Have a question or a tip? Be the first to share it.