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
Theusage 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 ofprompt_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
Usemax_tokens to cap how many tokens the model generates. This prevents runaway costs and enforces response length for your use case.
Managing Long Conversations
Because the fullmessages array is sent on every request, conversation costs grow with each turn. For long sessions, use one of these strategies:
Sliding window
Sliding window
Keep only the last N turns in the messages array, always preserving the
system message at the start.Summarization
Summarization
When history gets long, ask the model to summarize it, then replace the old messages with the summary.
Prompt caching
Prompt caching
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. Thetiktoken library implements OpenAI’s tokenizer:
Token Cost Summary
See the Models page for per-model rates and the Billing page to check your current balance and usage.