59API

← Voltar aos guias

Token Billing Explained: Estimate AI API Costs

Preços · EN · 2026-09-08

Token billing looks simple until a production request includes a long system prompt, conversation history, tool definitions, and a large generated answer. The key principle is that most AI APIs charge separately for tokens sent to the model and tokens generated by it. Once you measure both sides, estimating costs becomes a repeatable engineering task rather than guesswork.

What counts as a token?

A token is a small piece of text, not always a complete word. Common words may use one token, while unusual terms, code, JSON, URLs, and non-English text can use several. Whitespace and punctuation are also represented during tokenization. As a rough planning rule, English text often averages about four characters per token, but real prompts vary substantially.

Input tokens include the system instruction, user message, previous conversation turns, attached text that is sent inline, tool definitions, and sometimes tool results. Output tokens are the model's response, including generated JSON, code, reasoning exposed by the API, and other returned content. A long chat can therefore become more expensive even when the newest user message is short, because the application may resend the entire conversation on every request.

The core cost formula

For a model with separate input and output rates, use this formula:

Total cost = (input tokens ÷ 1,000,000 × input price) + (output tokens ÷ 1,000,000 × output price)

Use the model's published rates and billing unit exactly as displayed. Some providers also publish discounted cached-input rates, batch rates, or different prices for particular context sizes. Do not assume that a model's input and output prices are identical, or that all model families use the same unit.

For example, suppose an illustrative model rate is $3 per million input tokens and $15 per million output tokens. A request using 8,000 input tokens and generating 2,000 output tokens costs (8,000 ÷ 1,000,000 × $3) plus (2,000 ÷ 1,000,000 × $15), or $0.054. This is a calculation example, not a current price quote; always verify the selected model's live rate.

A practical estimation workflow

Advanced ways to reduce the bill

Trim duplicated instructions and avoid resending irrelevant history. Summarize old turns, retrieve only the documents needed for the current question, and place strict output requirements in a compact schema. Limit maximum output tokens, but do not set the limit so low that useful responses fail and require retries.

Tool calling has two cost layers: the model sees the tool schema and arguments, while your application may send the tool result back in a second request. Keep schemas concise, return only necessary fields, and avoid placing full database records into the conversation. Streaming improves user experience but normally does not reduce token usage.

For repeated prefixes, investigate whether your chosen provider supports cached input and whether the cache rules fit your traffic pattern. Also compare model quality against task requirements: routing simple classification or extraction to a smaller model can reduce cost without weakening a complex reasoning workflow.

Estimating costs with 59API

59API provides pay-as-you-go access to native official-quality Claude models, including Opus, Sonnet, Haiku, and Fable, along with GPT models. Because it works with Claude Code, Codex, and any OpenAI SDK, you can often test a lower-cost routing strategy without rebuilding your application. Configure the API base URL as https://api.59api.com, select the model you intend to use, and calculate with that model's current displayed rates rather than a generic average.

Its low-cost relay model can be useful for developers who want to control spend while keeping access to the original model quality. A referral rebate may further reduce effective costs for eligible users. If the rates and compatibility match your workload, you can sign up for 59API and begin with a small usage budget, then increase it after comparing logged token totals with your forecast.

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