Tokens Explained: How AI APIs Count Your Text
Tokens Explained: How AI APIs Count Your Text
If you are starting with AI APIs, the word token appears everywhere: input tokens, output tokens, context windows, token limits, and price per million tokens. Understanding tokens is essential because they directly affect cost, speed, and how much information a model can remember in one request.
A token is a small piece of text that an AI model reads or writes. It is not exactly a word. A token can be a whole short word, part of a longer word, a number, punctuation mark, space, or even part of a code symbol. For example, the sentence “AI is useful.” might be split into tokens like “AI”, “ is”, “ useful”, and “.” depending on the tokenizer used by the model.
As a rough rule for English text, 1 token is about 3 to 4 characters, and 100 tokens is about 75 words. This is only an estimate. Technical text, code, JSON, non-English languages, emojis, and long identifiers can tokenize differently. That is why two prompts with the same word count may have different token counts.
Input Tokens vs Output Tokens
Most AI API bills are based on two numbers: input tokens and output tokens. Input tokens are everything you send to the model. This includes your system instructions, developer instructions, user message, conversation history, tool definitions, retrieved documents, and any structured data in the request. Output tokens are everything the model generates in response.
For example, if you send a 900-token prompt and the model replies with 300 tokens, the request uses 1,200 total tokens. However, pricing is often different for input and output. Many models charge less for input tokens and more for output tokens because generating text requires more computation than reading it.
This is also why long chat histories can become expensive. If you send the entire conversation every time, the previous messages are counted again as input tokens on each new request. The model does not automatically “remember” old messages unless they are included in the current API call or stored through a separate memory or retrieval system.
How Tokens Are Counted in Practice
Token counting is done by a tokenizer, which converts text into the model’s internal units before processing. Different model families may use different tokenizers, so GPT and Claude models can count the same text slightly differently. You do not need to manually split text yourself, but you should learn how to estimate and measure it.
Here are practical ways to manage token counting:
- Use the API response usage fields: Many API responses include token usage details such as prompt tokens, completion tokens, and total tokens. Log these values during development.
- Estimate before sending: For English content, divide character count by 4 for a quick estimate. A 4,000-character prompt is roughly 1,000 tokens.
- Set output limits: Use parameters such as max tokens or max output tokens to prevent unexpectedly long responses.
- Trim conversation history: Keep only the messages that matter, or summarize older turns into a shorter memory note.
- Avoid unnecessary formatting: Large JSON objects, repeated instructions, and verbose examples can consume many tokens.
Why Context Window Matters
The context window is the maximum number of tokens a model can process in a single request. It includes both input and output. If a model has a 200,000-token context window and you send 190,000 input tokens, there may be only about 10,000 tokens left for the answer. If your request exceeds the limit, the API may reject it or require truncation.
Large context windows are useful for analyzing documents, repositories, logs, transcripts, or long conversations. But bigger is not always better. Sending too much irrelevant text increases cost and can reduce answer quality because the model has more material to sift through. A better strategy is often to retrieve only the most relevant sections, include a concise instruction, and ask for a focused output.
Token Costs: A Simple Example
Suppose you are building a support chatbot. Each request sends 1,500 input tokens: system prompt, user question, recent chat history, and a few help center snippets. The model produces a 400-token reply. If your app receives 10,000 requests per month, that is 15 million input tokens and 4 million output tokens monthly. Even small prompts become meaningful at scale.
This is where choosing the right API provider matters. 59API is designed for developers who want low-cost, pay-as-you-go access to Claude models such as Opus, Sonnet, Haiku, and Fable, plus GPT models, through a relay that is compatible with Claude Code, Codex, and OpenAI SDK workflows. Its API base URL is https://api.59api.com, so teams can often switch providers by updating the base URL and model name rather than rewriting their application.
59API focuses on cheap access without downgrading model quality: you still use native, official-quality models through a developer-friendly relay. For teams experimenting with agents, coding assistants, document analysis, or production chatbots, that can make token-heavy workflows much more affordable. The referral rebate is also helpful if you are sharing the platform with teammates or a developer community.
How to Reduce Token Usage Without Hurting Quality
- Write concise system prompts: Keep important rules, remove repeated explanations, and avoid bloated role descriptions.
- Send relevant context only: Use search or embeddings to include the top matching passages instead of entire documents.
- Ask for structured outputs: A clear format reduces rambling and keeps output tokens predictable.
- Use smaller models when appropriate: Haiku-style or lightweight GPT models can be excellent for classification, extraction, and rewriting tasks.
- Cache stable content: If the same long instruction or document is reused often, explore caching options where supported.
- Monitor usage by feature: Track token usage per endpoint, customer, or workflow so you know what actually drives cost.
Getting Started
To work effectively with AI APIs, think in tokens rather than pages or messages. Every instruction, example, document snippet, and generated answer has a token cost. Measure usage, limit outputs, keep context relevant, and choose models based on the job.
If you want a low-cost way to experiment with Claude and GPT models using familiar SDK patterns, consider signing up for 59API and pointing your client to https://api.59api.com. Understanding tokens will help you get better results, avoid surprise bills, and build AI features that scale cleanly from prototype to production.
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