Skip to main content
Every piece of text you send to or receive from a model is measured in tokens. Understanding tokens helps you estimate costs, avoid context-window errors, and build efficient pipelines.

What Is a Token?

A token is a chunk of text — roughly 3–4 characters or about 0.75 words in English. Tokenization is not simply splitting on spaces; punctuation, subwords, and whitespace each contribute their own tokens.
Token counts vary slightly by model because each provider uses a different tokenizer. The figures above are approximate. The usage field in every API response gives you the exact counts for that call.

Token Types in a Response

The usage object returned with every completion breaks down token usage:
On reasoning models, completion_tokens includes internal reasoning tokens. Some responses include a completion_tokens_details breakdown.

Context Windows

A model’s context window is the maximum number of tokens it can process in a single request — the sum of prompt_tokens and completion_tokens combined. Sending a request that exceeds the context window returns a 400 error. Always leave headroom for the model’s output — if the window is 128K and your prompt is 127K tokens, the model has almost no room to respond.

Controlling Output Length

Use max_tokens to cap how many tokens the model generates. This prevents runaway costs and enforces response length for your use case.
If the model hits max_tokens before finishing, finish_reason will be "length" instead of "stop". The response is truncated — check finish_reason in production to detect this.

Managing Long Conversations

Because the full messages array is sent on every request, conversation costs grow with each turn. For long sessions, use one of these strategies:
Keep only the last N turns in the messages array, always preserving the system message at the start.
When history gets long, ask the model to summarize it, then replace the old messages with the summary.
If your system prompt or context is large and stable across many requests, enable Prompt Caching. The first request processes and caches the prefix; subsequent requests with the same prefix pay a fraction of the cost.

Estimating Costs Before Sending

You can estimate token usage before making a request by counting tokens locally. The tiktoken library implements OpenAI’s tokenizer:
This is an approximation. For billing purposes, always use the usage values returned in the actual API response.

Token Cost Summary

See the Models page for per-model rates and the Billing page to check your current balance and usage.