OpenAI

Understanding and counting tokens

Learn how input, output, cached, and reasoning tokens affect API usage, model limits, and costs.

Updated: 8 hours ago

Overview

Tokens are the units that OpenAI models use to process text. A token can represent a character, part of a word, a whole word, or punctuation. Spaces also affect how text is divided into tokens.

A token count is not the same as a word count. The same text can produce different token counts depending on the model, its encoding, and the language.

Understand how text becomes tokens

When you send text to a model:

  1. The text is divided into tokens.

  2. The model processes those tokens.

  3. The model generates output tokens. These can include the text you receive and, for reasoning models, internal reasoning tokens that aren’t shown as answer text.

Use rough estimates for English text

These estimates can help you judge the size of English text:

  • 1 token is approximately 4 characters.

  • 1 token is approximately three-quarters of a word.

  • 100 tokens are approximately 75 words.

These are estimates, not exact counts. Sentence and paragraph lengths vary, and other languages can have different relationships between characters, words, and tokens.

Account for spaces and capitalization

A word can be divided into different tokens depending on its spelling, capitalization, and surrounding text.

For example, red, Red, and red do not contain identical text: the last example includes a leading space. An encoding can represent them differently.

Token IDs also depend on the encoding. Do not assume that an example token ID applies to every model.

Distinguish input and output tokens

CategoryWhat it describes
Input tokensTokens supplied to the model in a request. These are also called prompt tokens.
Output tokensTokens generated by the model. Chat Completions calls these completion tokens.
Cached input tokensInput tokens reused through prompt caching. Their pricing can differ from uncached input tokens.
Reasoning tokensTokens a reasoning model uses internally before producing its visible answer.

Reasoning tokens are not visible as answer text, but they count toward output usage and are billed as output tokens.

A short visible answer can therefore use more tokens than its displayed text suggests.

Count tokens before sending a request

Count plain text

Use the Tokenizer to see how text is divided into tokens.

For programmatic plain-text tokenization, use tiktoken. Select the encoding for your target model, such as with tiktoken.encoding_for_model(model).

A plain-text token count does not necessarily include all tokens in an API request. Message structure, tools, schemas, images, and files can affect the full input count.

Count a complete Responses input

For a complete Responses API input, use the input-token counting API.

It accepts Responses input formats, including messages, images, files, tools, and conversations. Its count includes formatting tokens used for request structure, such as message roles and boundaries.

An input count does not predict how many output tokens the model will generate.

Check actual token usage

After a request, inspect its usage information. Field names vary by endpoint:

  • Chat Completions reports prompt_tokens, completion_tokens, and total_tokens.

  • Responses reports input_tokens, output_tokens, and total_tokens.

You can also review activity over time in the Usage Dashboard. For instructions, including streaming usage, see: Reviewing API usage and costs.

Stay within model limits

Check your model’s documentation for its context window and maximum output. These limits can differ between models.

The context window limits the tokens a model can work with in a request. Models also have an output limit. For reasoning models, allow space for reasoning tokens as well as the visible answer.

If your input is too large, you can:

  • Shorten or rephrase the prompt.

  • Remove unnecessary or repeated context.

  • Divide large inputs into smaller parts.

  • Summarize or preprocess text before sending it.

Use the output-token setting supported by your endpoint and model. Chat Completions uses max_completion_tokens; Responses uses max_output_tokens.

These request-size limits are separate from API rate limits and monthly usage or spend limits. Review the model documentation for the model you use.

Understand token pricing

For token-based API pricing, the rate depends on the model and token category. Input, cached input, and output tokens can have different prices. Other API capabilities can use different billing units.

Check the API pricing page for current rates.

When comparing models, consider the total tokens and cost needed to complete your task. A lower price per million tokens does not necessarily produce a lower total cost: models can tokenize the same text differently and generate different amounts of output or reasoning.

Test representative tasks rather than comparing only the visible response length.

Account for multiple completions

When an endpoint and model support generating multiple completions, those additional completions use tokens too.

For Chat Completions, setting n above 1 generates multiple choices. You are charged for the generated tokens across those choices.

For the legacy Completions API, best_of can generate candidates that are not all returned. For example, best_of = 3 can generate up to 3 × max_tokens completion tokens across the candidates.

These parameters are endpoint-specific. Do not assume that n or best_of is supported by another API or model.

Was this article helpful?