Why Is My AI API Bill Higher Than Expected? Hidden Tokens and How to Cut API Costs (2026)
Tool prompts, thinking tokens, resent history, a new tokenizer and retries add tokens you never typed. Find each one in usage, then cut it.
1. Compare the estimate with the statement, line by line
Before changing anything, find out which line of the bill is larger than you expected. Every response from Anthropic's Messages API carries a usage object with input_tokens, output_tokens, cache_creation_input_tokens and cache_read_input_tokens, and server tools such as web search report their own usage. Log that object for every request, together with the model, a request identifier and your own task identifier. After a day, total each field per model and per task type, then set those totals next to your estimate. Look for the one or two lines with the largest gap. Input higher than expected points to sections 2 and 3; output higher than expected points to section 4; everything higher by a similar proportion after a model change points to section 5; more requests than user actions points to section 6. Without this split, people tend to switch models or vendors first, which changes the price without removing the tokens that caused the surprise.
2. Hidden input: the tool use system prompt and tool definitions
Anthropic's pricing page states that when a request includes tools, the API automatically adds a special system prompt that enables tool use, and those tokens are added to your normal input. The page lists the count per model: for example 286 tokens on Claude Opus 5.5 and Claude Sonnet 5.5, and 497 tokens on Claude Sonnet 4.6 when tool_choice is auto or none, rising to 589 when it is any or tool. Your own tool names, descriptions and JSON schemas are input tokens on top of that, on every call. Larger built-in toolsets weigh more: the same page puts the computer use toolset at about 4,500 input tokens and the browser use toolset at about 6,600 per request. None of this appears in the prompt you wrote. To cut it, send only the tools a given step can use, keep descriptions short and precise, and avoid forcing tool_choice when auto is enough. Because the overhead repeats on every turn, it multiplies with agent length.
3. Hidden input: history, search results and long agent loops
The Messages API is stateless, so each turn resends the conversation you want the model to see, and the input line grows with every turn. Tool results are part of that history: a large file read or database dump returned by a tool is carried into every later call unless you remove it. Web search adds a second layer. The official pricing page lists web search at $10 per 1,000 searches plus standard token costs, and states that search results retrieved during a conversation count as input tokens both within the turn and in later turns. It also says a search that errors is not billed, and that web fetch has no charge beyond the tokens of the fetched content. Practical changes: summarize older turns once a conversation passes a length you choose, replace bulky tool results with a short extract once they have been used, and cap the number of searches per task. If the same long prefix repeats, prompt caching changes its price; see /hub/prompt-caching-guide.
No overseas card needed: cocodot's OpenAI-compatible API — switch your base_url to call the models on sale.
Top up via Alipay / WeChat Pay (RMB), USDT / USDC · billed per token · switch your base_url to connect
4. Hidden output: thinking tokens are billed in full
With extended thinking, Anthropic's documentation is explicit that the billed output token count does not match the visible token count in the response: you are billed for the full thinking process, not the summarized thinking text you receive. The response reports how many of the billed output tokens were internal reasoning in usage.output_tokens_details.thinking_tokens; when streaming, that breakdown appears only on the final message_delta event. Subtracting it from output_tokens approximates the non-reasoning part of the answer. If your estimate counted only the visible text, check how much of the gap this field accounts for. Check the field per task type. For extraction, formatting or classification, test a lower supported thinking setting and compare accepted results before changing production; do not assume a task category determines the required effort. Keep thinking for the steps where it changes the outcome, and record the setting alongside the usage so later changes are traceable.
5. A model upgrade can change the token count for the same text
Token counts are not a property of your text alone; they depend on the model's tokenizer. Anthropic's pricing page states that Claude 4.7 and later models use a newer tokenizer that produces approximately 30 percent more tokens for the same text than earlier models, and the token counting documentation adds that usage and billing on these models reflect the new counts. So a team that moves from an older model to a newer one can see every token line rise by a similar proportion with no change to prompts. Do not reuse counts measured on the old model. Count with the model ID you plan to use: the official token counting endpoint is free to use, subject to its own requests-per-minute limit, which is separate from message creation. Its result is an estimate that can differ slightly from the actual count, and it does not apply caching logic. Run your test set through it before a migration and adjust estimates accordingly.
6. Retries and truncation: paying for the same work twice
A user action can turn into several billed attempts. Anthropic's official SDKs retry certain errors twice by default, Claude Code retries up to 10 times by default, and many applications add their own retry loop on top, so one failure can multiply. Streaming adds a subtlety documented on the errors page: a response can return HTTP 200 and still report an error inside the stream. Log every attempt with its request identifier, then compare attempts with user actions; if the ratio is well above one, keep a single retry layer with backoff and a total attempt limit. Truncation is the other loop. When a response stops with stop_reason max_tokens, the official guidance is to raise max_tokens or continue the response. Resending the whole request from scratch pays for the input again and regenerates what you already had. Set max_tokens per task type, check stop_reason on every response, and continue rather than restart when an answer is cut off.
7. Cut in the order your usage data points to
With the split from section 1, change the largest line first and measure again after each change. If tool overhead dominates, load tools per step. If history dominates, summarize and trim. If thinking dominates, lower it for simple calls. If input repeats a long stable prefix, enable prompt caching: on cocodot, Claude cache hits at half the input price; Claude cache writes at 1.25x the input price (2x for the portion written to the 1-hour cache), the same structure as the official pricing, and the official cache-hit multiplier is lower, which /hub/ai-api-official-vs-relay-when works through. If simple tasks run on a large model, test a smaller one on the same acceptance rules; /hub/llm-cost-optimization-2026 covers routing and evaluation. Change one thing at a time, otherwise you cannot tell which change moved the bill. Keep the before and after usage totals with the date, the model and the prompt version, so the next review starts from evidence rather than memory.
8. Keep the bill readable, and where cocodot fits
Two habits keep surprises small. First, review usage weekly by model and by task, not just the monthly total; the largest line this week is next week's target. Second, keep guardrails in your own code: a per-task attempt limit, a per-job token allowance and a deadline. Do not assume a budget alert stops charges. Whether the platform offers a spending cap that stops usage, and which services it covers, is set out in its official documentation. A prepaid balance is not a hard cap: requests already in flight when the balance runs out may still be billed; your statement is the record. cocodot bills per token through one OpenAI-compatible endpoint, https://cocodot.co/api/ai/v1, for Claude, GPT, Gemini and other model families. Listed Claude / GPT / Gemini rates are at or below the official list price; check the pricing page for each model's current rate. For runaway-spend patterns specific to agents, see /hub/api-relay-runaway-risk.
Symptom → where the tokens come from → what to change
| What you see | Likely source of extra tokens | What to change |
|---|---|---|
| Input much higher than your prompt | Tool use system prompt and tool definitions on every call | Send only the tools a step needs |
| Input grows turn after turn | Full history and earlier web search results resent | Summarize or trim older turns |
| Output much longer than the visible answer | Thinking tokens billed in full | Read thinking_tokens; lower thinking effort for simple work |
| Same prompt, more tokens after a model upgrade | Newer tokenizer on Claude 4.7 and later | Recount with the token counting endpoint |
| More requests than user actions | SDK and application retries stacking | Log attempts; keep one retry layer |
| Answers cut off, then asked again | stop_reason max_tokens followed by a full resend | Raise max_tokens or continue the response |