What Are Tokens? Count Them and Cut Your API Bill
AI APIs usually charge for tokens: small units of text processed by a model. Understanding them helps you predict costs, set useful limits, and avoid paying repeatedly for unnecessary context. Here is how token counting works, plus a concrete example that reduces a sample API bill from $35 to $21.
A token can be a word, part of a word, punctuation, or whitespace. In ordinary English, one token averages roughly four characters, so 1,000 tokens often represent about 750 words. These are planning estimates. Code, numbers, unfamiliar names, and languages other than English can produce very different counts.
Each model uses a tokenizer with its own vocabulary and rules. The same sentence can therefore have different token counts across GPT and Claude models. Counting words or dividing characters by four is useful for an initial budget; use the model's tokenizer or reported usage for accurate measurement.
Separate input tokens from output tokens. Input includes your instructions, the user's message, conversation history, retrieved documents, and tool definitions or results sent to the model. Output includes generated content. Some reasoning models also bill internal reasoning tokens within output usage, so a short visible answer can still consume a larger output budget.
The context window is the model's working capacity, not a fixed charge. You pay according to billable usage and applicable rates. If your application resends previous messages on each turn, that history becomes input again. Cached input may receive different pricing where supported, but a repeated prompt does not automatically qualify for a discount.
Calculate a request before scaling it. For standard uncached text usage, cost equals input tokens divided by 1,000,000 multiplied by the input rate, plus output tokens divided by 1,000,000 multiplied by the output rate. Account separately for cached tokens, tools, images, audio, or other charges when applicable.
Suppose a model costs $0.50 per million input tokens and $2.00 per million output tokens. These are illustrative rates, not a 59API price quote. A request containing 3,000 input tokens and generating 1,000 output tokens costs $0.0015 + $0.0020 = $0.0035. At 10,000 requests, that becomes $35.
Reduce the input to 1,800 tokens and the output to 600 tokens, and each request costs $0.0009 + $0.0012 = $0.0021. The same 10,000 requests now cost $21, saving $14, or 40%, assuming the shorter responses still meet your quality requirements.
Use this workflow to measure and reduce consumption:
- Measure representative requests. Test short, average, and long examples. Use a tokenizer appropriate to the exact model or a supported token-counting endpoint before sending large payloads. Treat local estimates as approximate when the service adds message formatting.
- Record actual usage. Capture the model, input tokens, output tokens, cached-token details where available, and request count. OpenAI-style responses commonly expose prompt_tokens and completion_tokens; Claude responses use input_tokens and output_tokens. Check your endpoint's schema.
- Trim repeated context. Remove duplicate instructions, send relevant document excerpts, and summarize older conversation turns. Compare answer quality before adopting the smaller prompt.
- Control response length. Ask for a specific deliverable, such as five troubleshooting steps. Set the endpoint's supported output-token limit, allowing enough room to finish. A limit is a ceiling; reaching it can truncate the answer.
- Track retries. In the original example, 500 additional full-size requests add $1.75. Fix retry loops and route simpler tasks to a cheaper model after testing accuracy.
Once usage is efficient, compare providers using the same model and workload. 59API offers low-cost, pay-as-you-go access to native, official-quality Claude and GPT models without downgrading them. Its compatibility with Claude Code, Codex, and OpenAI SDKs makes it practical to integrate through https://api.59api.com. Compare current model rates and caching terms against your measured usage; its referral rebate can provide additional savings under the applicable terms.
Sign up for 59API and run a small representative workload to compare cost per successful task before increasing traffic.
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