If you have used Claude, ChatGPT, or any large language model API, you have encountered tokens. They show up in pricing pages, rate limit errors, and API documentation. But most explanations of tokens are either too vague ("roughly 4 characters") or too academic (subword tokenization, BPE encoding).
This article explains tokens at the level a developer actually needs — what they are, why they matter for the things you care about, and how to think about them when building with AI APIs.
What a token actually is
A token is the basic unit of text that a language model processes. It is not a character, and it is not a word — it sits somewhere in between.
Modern language models use a technique called Byte Pair Encoding (BPE) to split text into tokens. The tokenizer learns which character sequences appear frequently together and groups them into single tokens. Common words become single tokens. Rare words get split into multiple tokens. Punctuation, spaces, and special characters all become tokens too.
Some examples using GPT-4's tokenizer:
"hello" → 1 token
"tokenization" → 3 tokens (token + ization split further)
"ChatGPT" → 3 tokens
" the" → 1 token (space included)
"2026" → 1 token
"\n" → 1 token
The 4-characters-per-token approximation works because English text averages roughly 4 characters per token across typical writing. Code, which has more symbols and shorter identifiers, tends to be closer to 3 characters per token. Dense technical writing can be 5 or more.
Why tokens matter for developers
1. Cost
Every major AI API charges by token. Claude Sonnet 4 costs $3.00 per million input tokens and $15.00 per million output tokens. GPT-4o costs $2.50 per million input and $10.00 per million output.
A typical back-and-forth debugging session might consume 10,000-50,000 tokens total. At Claude Sonnet pricing, that is $0.03 to $0.15 per session — which sounds trivial until you have a team of 10 developers each running 5 sessions per day.
The output token cost matters more than most developers realize. When you ask Claude to write a 500-line function, you are paying 5x more per token for the output than the input. Prompts that ask for verbose explanations cost significantly more than prompts that ask for concise code.
2. Context window limits
Every model has a context window — the maximum number of tokens it can process in a single request, including both input and output. Claude Sonnet 4 has a 200,000 token context window. GPT-4o has 128,000. Gemini 1.5 Pro has 1,000,000.
This means a 200,000 token context window is approximately:
- 800,000 characters of text
- 150,000 words
- About 600 pages of a typical book
- A large codebase (though not an entire monorepo) In practice, you hit the context limit much faster than these numbers suggest because:
- Your system prompt consumes tokens
- Every message in the conversation accumulates
- Every response the model generates adds to the total
- Large code pastes consume tokens proportional to their length A single paste of a 500-line file is roughly 25,000-30,000 tokens — 12-15% of Claude's context window in one shot.
3. Rate limits
AI providers enforce rate limits in tokens per unit time, not requests per unit time. Claude has a 5-hour session limit and a 7-day weekly limit, both measured in token consumption. OpenAI's API has tokens-per-minute (TPM) and tokens-per-day (TPD) limits.
This means two things developers often get wrong:
Message count is not what matters. Sending 100 short messages consumes far fewer tokens than sending 10 messages with large code pastes. Rate limits are about total token consumption, not request frequency.
Output tokens count too. If you ask a model for a very long, detailed response, you are consuming output tokens against your rate limit just as much as the input.
How to estimate tokens without calling the API
For quick estimates, the 4-characters-per-token approximation is sufficient:
function estimateTokens(text) {
return Math.ceil(text.length / 4)
}
// Examples:
estimateTokens("Hello, world!") // → 4
estimateTokens("function authenticate(") // → 6
estimateTokens(largeCodeFile) // → roughly accurate ±10%
For precise counts, use the official tokenizers:
OpenAI: tiktoken library (Python and JavaScript)
import { encoding_for_model } from 'tiktoken'
const enc = encoding_for_model('gpt-4o')
const tokens = enc.encode(text).length
Anthropic: Claude's API has a count_tokens endpoint:
const response = await anthropic.messages.countTokens({
model: 'claude-sonnet-4-5',
messages: [{ role: 'user', content: text }]
})
console.log(response.input_tokens)
For browser extensions and client-side tools that cannot call the API, client-side estimation with the ±8% approximation is the practical approach — it is accurate enough for showing users whether they are at 20% or 80% of their context window.
Practical token optimization for developers
Be specific, not verbose. "Refactor this function to handle null values" consumes far fewer tokens than a paragraph explaining the same thing. Models understand concise instructions.
Paste targeted context, not entire files. Instead of pasting a 500-line file, paste the specific function plus 10-15 lines of surrounding context. You get equally good answers and use a fraction of the tokens.
Summarize long conversations. After a long debugging session, ask the model to summarize key decisions and findings in under 200 words, start a new conversation with that summary, and continue. You preserve context that matters and reset token overhead.
Use faster models for iteration. If you are iterating on boilerplate, formatting, or simple transformations, use Haiku or GPT-4o mini. They consume the same rate limit quota but cost 10-20x less per token. Reserve the premium models for complex reasoning.
Monitor your actual usage. Most developers significantly underestimate their token consumption because the interface hides it. Tools like TokenPulse show real-time token counts, context window percentage, and estimated cost directly in the browser — no API key required.
The key numbers to remember
| Model | Context Window | Input Cost / 1M | Output Cost / 1M |
|---|---|---|---|
| Claude Sonnet 4 | 200k tokens | $3.00 | $15.00 |
| Claude Opus 4 | 200k tokens | $15.00 | $75.00 |
| GPT-4o | 128k tokens | $2.50 | $10.00 |
| Gemini 2.0 Flash | 1M tokens | $0.10 | $0.40 |
| DeepSeek V3 | 128k tokens | $0.27 | $1.10 |
Understanding tokens is the foundation of working effectively with AI APIs. Once you internalize that everything is measured in tokens — cost, limits, context — the behavior of these systems becomes much more predictable.
Top comments (0)