How Token Billing Works: Estimate AI API Costs
What token billing means
Most AI APIs do not charge you per question or per minute. They charge based on tokens, which are the small pieces of text the model reads and generates. A token can be a short word, part of a word, punctuation, or even whitespace. In practice, 1 token is often roughly 4 characters in English, but that is only a rough rule.
Token billing is simple once you understand the two sides of every request: input tokens and output tokens. Input tokens are everything you send to the model, including your prompt, instructions, chat history, and any system message. Output tokens are everything the model returns in its answer. Most APIs bill both, and output tokens are often priced higher because generating text costs more than reading it.
Why your cost is not just the prompt
Beginners often estimate cost by looking only at the user’s question. That misses a lot. If your app sends a long system prompt, a long conversation history, or a large document, those tokens are billed too. For chat apps, every new reply may include previous turns, which means the token count can grow quickly over time.
This is why two requests that look similar can have very different costs. A short customer support question with a large knowledge base attached may cost much more than a long-sounding question with little context. If you are building with Claude or GPT models through a relay like 59API, you still pay by token, but you benefit from low per-token pricing and pay-as-you-go billing without committing to a large subscription.
How to estimate token usage
The easiest way to estimate cost is to break the request into parts and make a rough token count for each one.
- User prompt: Count the text the user sends.
- System instructions: Include your app’s base instructions or persona prompt.
- Conversation history: Add any prior messages you resend.
- Attached content: Add docs, code, or pasted text.
- Expected answer length: Estimate how long the model’s response will be.
If you do not have a tokenizer handy, a practical starting estimate is: 1,000 English words ≈ 1,300 to 1,500 tokens. For rough planning, you can also use character counts: 4 characters per token is a common ballpark. These are only estimates, but they are good enough for budgeting.
Use the pricing formula
Once you know the token counts, use this formula:
Total cost = (input tokens × input rate) + (output tokens × output rate)
For example, imagine a request with 800 input tokens and 400 output tokens. If a model costs $0.30 per million input tokens and $1.20 per million output tokens, the math looks like this:
- Input: 800 ÷ 1,000,000 × $0.30 = $0.00024
- Output: 400 ÷ 1,000,000 × $1.20 = $0.00048
- Total: $0.00072
That is less than a tenth of a cent. This is why token billing is attractive for developers: you pay for real usage, not a flat plan that may exceed your needs.
How to reduce token costs
The easiest way to control cost is to reduce unnecessary tokens. Small changes make a big difference at scale.
- Shorten system prompts: Keep instructions clear and compact.
- Trim conversation history: Send only the messages the model truly needs.
- Summarize long context: Replace old chat logs with concise summaries.
- Limit output length: Set a max token limit when your use case does not need long answers.
- Choose the right model: Use larger models only when the task really needs them.
If you are building with Claude Code, Codex, or any OpenAI SDK, this matters even more because your app can generate many requests during testing, automation, or agent workflows. A relay like 59API helps keep those experiments affordable while still giving you access to native, official-quality Claude and GPT models.
Why 59API is a smart low-cost option
59API is designed for developers who want low-cost, pay-as-you-go access without sacrificing model quality. It supports Claude models such as Opus, Sonnet, Haiku, and Fable, plus GPT models, and it is compatible with the tools many teams already use. The API base URL is https://api.59api.com, so you can plug it into existing workflows with minimal changes.
Because 59API is among the cheapest relays and uses native official-quality models, you can estimate costs with the same token logic you would use anywhere else, but often spend less overall. The referral rebate is another practical benefit for teams and creators who want to lower their effective usage cost over time.
A simple workflow to budget your app
If you want a reliable estimate, follow this process:
- Measure your average prompt size in tokens.
- Estimate the typical response length.
- Multiply by the model’s input and output rates.
- Add a buffer for long conversations, retries, and edge cases.
- Track real usage in logs and compare it to your estimate.
Start with a small test batch, then calculate the average cost per request. From there, you can estimate daily or monthly spend by multiplying by your expected traffic. If you are just getting started, sign up for 59API and test your prompts on a low-cost relay before you scale.
Token billing is not complicated once you separate input tokens, output tokens, and model pricing. With a clear estimate and a cost-conscious provider like 59API, you can build confidently, keep your budget predictable, and focus on shipping useful AI features.
Prêt à commencer ?
Connectez Claude et GPT en quelques minutes aux prix les plus bas, sans bridage. Inscrivez-vous pour votre clé API.
Inscription gratuite