Token Billing: Estimate AI API Costs Accurately
How Token Billing Actually Works
Most AI APIs bill by tokens rather than requests. A token is a small unit of text: in English, a practical planning estimate is roughly four characters or about three-quarters of a word per token. The exact count depends on the model tokenizer, language, punctuation, code, and formatting. A 1,000-word English document may therefore contain roughly 1,300 to 1,700 tokens, while dense source code, JSON, and non-English text can tokenize differently.
Your bill usually has at least two components: input tokens, which include the system prompt, conversation history, user message, retrieved context, and tool schemas; and output tokens, which are the model's generated response. Model providers typically publish separate per-million-token prices for each. Output tokens often cost more, so a workflow that permits long reasoning or verbose answers can cost materially more even when the user prompt is short.
Some models also bill for cached or reused prompt content at a discounted rate. When available, prompt caching is especially valuable for stable material such as long system instructions, product documentation, codebase conventions, or large tool definitions. Do not assume every repeated prompt is automatically cached: confirm the model's cache behavior and billing rules before forecasting savings.
The Cost Formula to Use
Estimate one request with this formula: request cost = (input tokens / 1,000,000 x input rate) + (output tokens / 1,000,000 x output rate) + any cache, tool, or service charges. Then multiply by expected request volume. Keep input and output separate because using one blended token price hides the part of the workflow that needs optimization.
- A request sends 12,000 input tokens and generates 1,500 output tokens.
- The selected model costs $3 per million input tokens and $15 per million output tokens.
- Input cost: 12,000 / 1,000,000 x $3 = $0.036.
- Output cost: 1,500 / 1,000,000 x $15 = $0.0225.
- Total estimated cost per request: $0.0585, before applicable extras.
At 20,000 similar requests per month, that is about $1,170. This is why a modest reduction in repeated context can have a larger impact than shaving a sentence from the final answer.
Forecast With Realistic Percentiles
Do not budget from an average prompt alone. Production traffic has a long tail: a minority of users may paste a large document, run a repository-scale coding task, or hold a lengthy multi-turn conversation. Build three scenarios: a typical request, a p95 request, and a maximum allowed request. For each scenario, track input tokens, output tokens, calls per task, retries, and tool-loop iterations.
For agentic workflows, calculate cost per completed task, not simply cost per model call. A coding agent might make several calls to inspect files, plan edits, apply changes, run tests, and repair failures. Multiply the expected token use per call by the expected number of calls, then add a retry allowance. This creates a forecast that reflects user outcomes rather than an unrealistically clean demo path.
Practical Ways to Reduce Token Spend
- Set explicit output limits. Ask for a concise answer, structured patch, or fixed number of alternatives instead of allowing unbounded generation.
- Trim conversation history. Preserve decisions, requirements, and unresolved issues in a compact summary rather than replaying every prior message.
- Retrieve only relevant chunks. Sending an entire knowledge base or repository on every call is expensive and often reduces answer quality.
- Use the smallest capable model for classification, routing, extraction, and simple transformations; reserve premium models for difficult reasoning or high-stakes output.
- Measure tokens by endpoint, model, customer, and feature. A daily cost dashboard exposes accidental loops, oversized prompts, and abusive traffic quickly.
- Apply per-request token caps and monthly customer budgets so one malformed workflow cannot create an uncontrolled bill.
Choosing a Relay Without Losing Model Quality
Cost optimization should not mean accepting a downgraded or imitation model. 59API is a low-cost, pay-as-you-go AI API relay that provides access to native official-quality Claude models, including Opus, Sonnet, Haiku, and Fable, as well as GPT models. Its API base URL is https://api.59api.com, and its compatibility with Claude Code, Codex, and OpenAI SDK integrations can reduce migration work when testing model and pricing combinations.
Start with a small production-like token sample, log actual input and output usage, and compare cost per successful task across models. Because 59API is positioned among the cheapest relay options and includes a referral rebate, it can be a practical choice for teams that need predictable pay-as-you-go access without compromising on model quality. Sign up for 59API, run a controlled benchmark, and base your budget on measured token usage rather than assumptions.
Pronto para começar?
Conecte Claude e GPT em minutos pelos menores preços, sem cortes. Cadastre-se e obtenha sua chave API.
Cadastro grátis