AI APIs are rarely priced as a fixed amount per question. A request includes input and output tokens, while some models also distinguish cache reads, cache writes, images, or tool usage. Cost control starts by calculating your real request shape, not by comparing one headline price.
The Basic Formula
The common calculation is:
request cost = input tokens / 1,000,000 x input price
+ output tokens / 1,000,000 x output priceFor a request with 20,000 input tokens and 2,000 output tokens, multiply each number by its own current rate. Do not apply the input rate to all tokens because output often has a different price.
Rates change over time. Use Model Hub as the current source instead of keeping old article numbers in a spreadsheet forever.
What Counts as Input
Input is more than the final sentence a user types. It can include:
- System instructions
- Conversation history
- Uploaded or retrieved documents
- Repository files read by Claude Code
- Tool results
- Context resent during retries
That is why the word “continue” can cost much more late in a long session than it does in a new one. The model still needs enough prior context to continue correctly.
What Counts as Output
Output includes generated prose, code, and some tool-call arguments. Asking for full files, long explanations, or many alternatives increases output tokens directly.
Useful controls are specific rather than simply “be short”:
- Return a patch instead of repeating the full file
- Limit table columns
- Give the conclusion first and expand only when requested
- Return structured JSON for batch work
How Prompt Cache Changes Cost
Some models and protocols support prompt caching. Repeated system prompts or documents can receive a lower cache-read rate after the initial write, which may have its own price.
Caching is most useful when:
- The same prefix is sent repeatedly
- The prefix is substantial
- It stays unchanged during the cache lifetime
- There are enough calls to justify the first write
If the prefix changes every time or the request runs once, caching may not help. Current support and billing rules are documented in the billing guide.
Why Long Context Can Exceed a Budget
A large context window means the model can accept more material, not that every request should include all available material. Common waste includes resending a whole repository, never trimming history, duplicating retrieval results, and uploading unfiltered logs.
Use a lower-cost model to classify or summarize first, then send relevant sections to the target model. The Gemini long-context guide covers this workflow in more detail.
Estimate a Monthly Budget
Use real samples rather than one optimistic request:
- Collect 20 to 50 representative calls from Usage
- Calculate average input and output tokens separately
- Multiply by expected daily requests
- Multiply by working days and reserve room for retries and growth
monthly tokens = average tokens per call x calls per day x usage daysFor teams, split estimates by member, project, and key. A runaway script should hit its own limit rather than consume the entire balance. Teams supports separate keys and spending limits.
Replace Estimates with Usage Data
Once a workload runs, review:
- Which model consumes the most
- Input-to-output ratio
- Fastest-growing key or member
- Unexpected retries
- Expensive models handling simple work
Optimize the largest source first. If repeated context dominates, focus on retrieval and caching. If output dominates, tighten response formats.
A Practical Control Order
- Confirm current rates in Model Hub
- Calculate average cost from real calls
- Route simple tasks away from premium models
- Reduce repeated context and unnecessary output
- Set a tolerable limit for every key
- Review usage weekly instead of discovering problems at month end
There is no universal token cost for every user. The reliable method combines current prices with actual usage, then continuously adjusts model choice, context, and limits. Start with the AI API quickstart, and use the billing documentation for the current accounting rules.