Tokens Explained: Count API Usage Step by Step
When an AI API charges by tokens, the bill is based on processed text rather than the number of questions you ask. Understanding token counting helps you estimate costs, set reliable output limits, and diagnose why a request is more expensive than expected.
What a token actually is
A token is a piece of text selected by a model's tokenizer. A short word may be one token, while a long word can be split into several tokens. Spaces, punctuation, numbers, code, and some Unicode characters also affect the count. Tokens are therefore not the same as words.
For rough planning, 1,000 English tokens may represent about 700 to 800 words, but this is only an estimate. Code, JSON, technical terms, and non-English text can use tokens at a different rate. Each model family can also use a different tokenizer.
Step 1: Identify everything you send
Start with the complete request, not just the latest user message. Input tokens can include the system prompt, developer instructions, conversation history, retrieved documents, tool definitions, attached text, and the current prompt. In a chat application, sending the full history on every request means older messages are counted repeatedly.
For example, a request with a 400-token system prompt, 600 tokens of history, and a 200-token question starts with approximately 1,200 input tokens before the model generates anything.
Step 2: Estimate the text before sending it
Use a tokenizer designed for the exact model whenever possible. GPT models should be measured with the tokenizer or counting library recommended for the relevant OpenAI-compatible model. Claude requests should be measured with Anthropic-compatible token counting tools when available. Do not assume that a tokenizer for one model gives an exact result for another.
For a quick first estimate, count characters and divide by four for ordinary English prose. Treat that number as a planning signal only. To get an accurate result, include the actual message structure and any tool schemas, because structured content can add substantial overhead.
Step 3: Measure the response
After the API call, inspect the usage data returned by the service. Common fields include prompt_tokens, completion_tokens, and total_tokens. The total is normally the input tokens plus the generated output tokens. Save these values with your request ID so you can compare prompts, models, and features over time.
A request using 1,200 input tokens and producing 350 output tokens uses about 1,550 total tokens. If your provider prices input and output differently, calculate each portion separately rather than multiplying the total by one rate.
Step 4: Control token consumption
- Set a suitable maximum output limit so a short answer cannot expand into an unexpectedly long one.
- Trim or summarize old conversation history when it is no longer needed.
- Remove duplicated instructions and irrelevant retrieved documents.
- Ask for structured, concise output when your application does not need an essay.
- Cache stable system instructions or repeated context when your provider supports prompt caching.
Do not reduce context blindly. Removing important instructions can cause retries, incorrect answers, or extra tool calls that cost more than the original context.
Step 5: Connect an affordable API relay
If you want to test several models without changing your application architecture, 59API provides access to official-quality Claude Opus, Sonnet, Haiku, and Fable models as well as GPT models. Its OpenAI-compatible API works with standard OpenAI SDK integrations, while Claude Code and Codex can use the relay through the API base URL https://api.59api.com.
Configure the base URL in your SDK, keep your API key in an environment variable, then log token usage from each response. This makes it easy to compare quality and cost while keeping pay-as-you-go control. Because 59API is positioned as a low-cost relay with no model downgrade and also offers a referral rebate, it can be a practical choice for developers who want to start small. Sign up for 59API when you are ready to run your first measured request.
Step 6: Build a simple cost check
For each request, record the model, input tokens, output tokens, price per token, latency, and whether a retry occurred. Multiply input tokens by the input rate and output tokens by the output rate, then add the two results. Reviewing this report weekly reveals which prompts are expensive and whether a smaller model can handle routine tasks.
The key rule is simple: count the entire request, measure the generated response, and use the tokenizer and pricing rules for the specific model you selected. With that habit, token usage becomes a predictable engineering metric rather than a surprise on your API bill.
¿Listo para empezar?
Conecta Claude y GPT en minutos a los precios más bajos, sin recortes. Regístrate para obtener tu clave API.
Registro gratis