The Bill That Surprised Me
When I first started calling AI APIs directly, I had no idea what I was doing cost-wise. I'd written a little script that looped through 500 customer support emails, sent each one to Claude for a summary, and left it running overnight. By morning it had processed maybe 200 emails. By the end of the week I'd racked up a bill I wasn't expecting — not because the per-token price is scary high, but because I had zero idea how much text I was feeding in versus getting back, and I was doing it in the least efficient way possible.
That experience made me actually learn how token billing works. And once I understood it, I was able to cut that same workflow's cost by about 60% without changing the quality of the output. That's what this article is about.
What Even Is a Token?
Before we talk money, let's make sure we're on the same page about tokens. A token is roughly 3-4 characters of text, or about ¾ of a word. The word "hamburger" is two tokens. "AI" is one token. Code and special characters can tokenize differently — JSON curly braces, for example, are often their own tokens.
Every time you send a request to an AI API, you're spending tokens in two directions: the stuff going in (your prompt, system instructions, conversation history) and the stuff coming out (the model's reply). These are billed separately, and the rates are different.
The Three Token Categories You're Actually Billed For
Most providers break your usage into three buckets:
Input tokens — Everything you send to the model. This includes your system prompt, the user message, any conversation history you're replaying, and any documents or context you've stuffed into the request. Input tokens are cheaper than output tokens, usually by a factor of 3-5x.
Output tokens — Everything the model writes back. This is the expensive bucket. If you ask for a 2,000-word essay, those 2,000 words cost more per token than the prompt that asked for them. This is why "be concise" isn't just good UX advice — it's a cost-reduction strategy.
Cache read tokens — This is the one people miss. When you send the same system prompt or document over and over (like in a loop), providers like Anthropic and OpenAI can cache that prefix and charge you a reduced rate for it on subsequent calls. Cache reads can cost 80-90% less than fresh input tokens. If you're not using prompt caching and you're running batch jobs, you're leaving real money on the table.
Input vs Output Pricing
As a rough rule of thumb: input tokens cost about 1/5 of output tokens. On Claude 3.5 Sonnet, it's $3 per million input tokens vs $15 per million output tokens. This means a long prompt that gets a short answer is almost always cheaper than a short prompt that gets a long answer.
How to Read a Usage Object
Every API response includes a usage object. It's easy to skip past it, but it's your receipt — and it tells you exactly what happened. Here's what it looks like for a Claude call:
// From the API response object
{
"usage": {
"input_tokens": 1842,
"output_tokens": 312,
"cache_creation_input_tokens": 1500,
"cache_read_input_tokens": 342
}
}Let me break that down. input_tokens: 1842 is the total tokens going in. output_tokens: 312 is what came back. The two cache fields are interesting: cache_creation_input_tokens means the model saved 1,500 of those input tokens into a cache for the first time (slight cost premium to create the cache), and cache_read_input_tokens means 342 tokens were served from cache at the discounted rate.
For OpenAI the structure is slightly different but the idea is the same:
// OpenAI usage object
{
"usage": {
"prompt_tokens": 1842,
"completion_tokens": 312,
"total_tokens": 2154,
"prompt_tokens_details": {
"cached_tokens": 1500,
"audio_tokens": 0
}
}
}Get in the habit of logging this object on every call. Even a simple console.log(response.usage) will help you spot patterns — like a system prompt that's ballooned to 3,000 tokens without you noticing.
Estimating Cost Before You Run
The worst way to discover you have a cost problem is after the fact. Here's a simple way to estimate before you fire off a batch job. You need two things: the token count and the per-token price from the provider's pricing page.
To get a rough token count, divide your character count by 4 (conservative estimate). Or use a tokenizer library. Here's a quick Node snippet that estimates cost before sending:
// Rough cost estimator before running a batch
const INPUT_PRICE_PER_M = 3.00; // Claude 3.5 Sonnet
const OUTPUT_PRICE_PER_M = 15.00;
function estimateCost(promptText, expectedOutputWords) {
const inputTokens = promptText.length / 4;
const outputTokens = expectedOutputWords * 1.33;
const inputCost = (inputTokens / 1_000_000) * INPUT_PRICE_PER_M;
const outputCost = (outputTokens / 1_000_000) * OUTPUT_PRICE_PER_M;
return { inputCost, outputCost, total: inputCost + outputCost };
}
// For 500 emails, each ~300 words prompt, ~150 word summary
const singleCall = estimateCost(emailPrompt, 150);
console.log(`Per call: $${(singleCall.total).toFixed(5)}`);
console.log(`500 calls: $${(singleCall.total * 500).toFixed(2)}`);
→ Per call: $0.00089
→ 500 calls: $0.44That math check takes two minutes and can save you from unexpected charges. Always do it before looping through large datasets.
Five Ways to Cut Your Bill Without Losing Quality
Once you can read the numbers, here's what to actually do about them.
1. Trim your system prompt. System prompts repeat on every single call. A 2,000-token system prompt across 1,000 calls is 2 million tokens — before the model writes a single word back. Audit yours. Remove examples that aren't earning their keep. I cut mine from 1,800 tokens to 600 by converting prose explanations into bullet points, and the output quality didn't change at all.
2. Enable prompt caching. If you're on Claude or GPT-4o and you have a consistent system prompt or document that gets sent repeatedly, enable caching. For Anthropic you add "cache_control": {"type": "ephemeral"} to the content block. Cache reads cost 10 cents per million on Claude vs $3 for fresh input. That's a 97% discount on repeated content.
3. Set a max_tokens limit. If you need a summary, don't let the model write 800 words when 200 will do. Set max_tokens to cap the output. The model stops generating when it hits the limit. This is the single most direct lever on output cost.
4. Use a smaller model for simpler tasks. You don't need Sonnet to classify an email as "refund request" or "shipping question". Claude Haiku or GPT-4o Mini cost a fraction of the flagship models. I use a tiered approach: cheap model for routing/classification, expensive model only for the tasks that actually need it.
5. Batch similar requests. Some providers offer batch APIs at 50% discount for non-real-time jobs. If you don't need an instant response — like processing last month's support tickets overnight — batch mode is free money.
Set Spending Limits
Both Anthropic and OpenAI let you set monthly spend limits and notification alerts in their dashboards. Set a limit that's slightly above your expected spend and you'll never get surprised. I set mine to 2x my expected usage so I get a warning email before I hit anything painful.
The Pattern I Use Every Time Now
These days before I write any loop or batch job, I do three things automatically: I estimate the cost per call and multiply by my count, I check whether my system prompt could be trimmed or cached, and I ask myself whether I actually need the big model or whether a smaller one would handle this fine.
It's become second nature, like checking the gas before a road trip. The API costs are genuinely low for small-scale work — that 500-email job I mentioned works out to under a dollar if you do it right. But they scale with volume, and the fastest way to understand where your money goes is to stare at that usage object for a few minutes and do the math.
Once you know the levers, you'll stop worrying about the bill and start thinking about what you can build instead.
More tutorials in this category, or explore the full field guide.