OpenAI tokenizer · Browser and Python guide

OpenAI Tokenizer

Use this OpenAI tokenizer to count GPT prompt tokens online, inspect token IDs, and reproduce your results in Python. Compare model encodings and estimate multi-turn conversation input.

Accuracy note

Raw encoding selections are exact for the selected encoding. Model selections are compatible views and may not include complete API request overhead.

Official counting docs →
Browser encodings
o200k_base · cl100k_base · p50k_base · r50k_base · GPT-2
Python package
tiktoken
Local result
Raw text boundaries and token IDs
Request-level count
OpenAI Responses input_tokens endpoint

Facts checked against official sources on .

Use Tiktoken Online in the Browser

Choose a GPT model for a compatible view or select a raw encoding such as o200k_base or cl100k_base. Paste text into the editor and the page updates the token count, colored boundaries, and IDs immediately. Show whitespace is useful when indentation, line endings, or leading spaces explain an unexpected result.

A raw-encoding selection measures exactly what that encoding does to the pasted text. A model selection is labeled compatible because a complete API request can contain message structure, tool definitions, images, and provider-managed tokens that are not present in a plain text field.

OpenAI Tokenizer Counts by Model

Compare the exact same text without quotation marks or a trailing newline: “Hello world! 你好,世界!”. The table counts only that raw text. Select the named raw encoding in the browser to reproduce the result; a chat-model selection may add message formatting.

Current tiktoken maps gpt-5-mini, gpt-4o, and gpt-4.1 to o200k_base, so their base-text counts match here. Its gpt-5 prefix rule also resolves gpt-5.6 to o200k_base; that is a compatibility mapping, not independent verification of every GPT-5.6 request format. The cl100k_base row is a raw-encoding comparison and is not assigned to any of these four models.

Model / comparisonEncodingSample tokensAccuracy boundary
gpt-5.6o200k_base8Compatible view via the gpt-5 prefix; confirm full API usage
gpt-5-minio200k_base8Base text only; excludes request formatting
gpt-4oo200k_base8Base text only; excludes request formatting
gpt-4.1o200k_base8Base text only; excludes request formatting
Raw encoding comparisoncl100k_base11Exact for this encoding; not a model assignment

Count Tokens with Tiktoken in Python

Install the tiktoken package in your Python environment, load the same raw encoding selected in the browser, and encode the same string. The length of the returned ID list is the raw-text token count, and the list itself can be compared with the IDs displayed by Tiktokenizer.

Choose an encoding deliberately when you need a reproducible comparison. Do not assume that one encoding reproduces every model or every request wrapper. For production limits, keep the model ID and complete input structure tied to the provider's current documentation.

pythonOfficial counting example
import tiktoken

encoding = tiktoken.get_encoding("o200k_base")
text = "Count this prompt with tiktoken."
token_ids = encoding.encode(text)

print(len(token_ids))
print(token_ids)
print(encoding.decode(token_ids))

Choose the OpenAI Tokenizer by Model Name in Python

Use tiktoken.encoding_for_model when your application knows the model ID. For gpt-4o, the lookup returns o200k_base. This example encodes the same text as the model table and prints its encoding name, eight raw-text tokens, and token IDs.

Model lookup selects a vocabulary; it does not add chat roles or message boundaries. Keep tiktoken up to date, handle an unrecognized model explicitly, and check whether a result comes from a named mapping or a broad prefix rule before treating it as verified model support.

pythonOfficial counting example
import tiktoken

text = "Hello world! 你好,世界!"
encoding = tiktoken.encoding_for_model("gpt-4o")
token_ids = encoding.encode(text)

print(encoding.name)  # o200k_base
print(len(token_ids))  # 8 raw-text tokens
print(token_ids)

Estimate Tokens for a Multi-Turn Conversation in Python

A multi-turn prompt includes the earlier user and assistant messages you send again, plus message framing. This text-only example follows the OpenAI Cookbook estimate for the pinned gpt-4o-2024-08-06 Chat Completions snapshot: three tokens per message, one extra token for an optional name field, and three tokens to prime the next assistant reply.

These overhead constants are specific to the documented estimate. Do not reuse them as an exact GPT-5.6 count or for tool calls, images, files, or Responses API inputs. Count the complete supported Responses input with the official input_tokens endpoint, or compare the estimate with usage.prompt_tokens after a Chat Completions request. The next generated reply is output usage and is not included below.

pythonOfficial counting example
import tiktoken

# Text-only estimate for this pinned Chat Completions snapshot.
model = "gpt-4o-2024-08-06"
encoding = tiktoken.encoding_for_model(model)
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Hello!"},
    {"role": "assistant", "content": "Hi! How can I help?"},
    {"role": "user", "content": "Explain tokens in one sentence."},
]

estimated_tokens = 3  # Assistant reply priming.
for message in messages:
    estimated_tokens += 3  # Message framing for this snapshot.
    for field in ("role", "content", "name"):
        if field in message:
            estimated_tokens += len(encoding.encode(message[field]))
            if field == "name":
                estimated_tokens += 1

print("Estimated input tokens:", estimated_tokens)
# Excludes tools, images, files, and the next generated reply.
# Confirm actual input usage with response.usage.prompt_tokens.

Match Python and Browser Token IDs

Use the exact same Unicode text and encoding name on both sides. Invisible differences such as a trailing newline, non-breaking space, smart quote, or normalization change can produce different token IDs even when two strings look similar on screen.

If the totals differ, turn on whitespace visualization, copy the raw text again, and compare the first token position where the ID lists diverge. This approach narrows the problem to the input or encoding instead of treating tokenization as a black box.

Tiktoken Count vs OpenAI API Input Tokens

Tiktoken is useful for local raw-text inspection. OpenAI's Responses API also exposes a request-level input-token counting operation at POST /responses/input_tokens. It accepts request components such as model, instructions, conversation context, text, image, or file inputs and returns an input_tokens total.

Use the API operation when you need a count for the structured request you plan to send. Use the browser or Python tokenizer when you need to understand token boundaries, inspect IDs, compare encodings, or debug why a particular string is expensive.

MethodBest forImportant boundary
Tiktokenizer browserInteractive token visualizationCounts the selected local representation
Python tiktokenRepeatable raw-encoding checksRequires an explicit matching encoding
Responses input_tokensComplete supported API inputRequires an OpenAI API request

Tiktokenizer Reference Sources

Tokenizer behavior and model limits change. Verify production decisions with current provider documentation:

Frequently asked questions

What is the OpenAI tokenizer?

An OpenAI tokenizer splits text into tokens and maps those pieces to numeric IDs for GPT models. Tiktoken is OpenAI's open-source tokenizer library. This independent browser tool helps you inspect supported encodings and compare raw-text counts; it is not an official OpenAI service.

Is the OpenAI tokenizer free?

This browser tokenizer is free to use, and running the open-source tiktoken package locally does not require an API key or a paid API request. Calling OpenAI models is a separate service subject to its API pricing.

How many tokens is 1000 words?

For ordinary English, OpenAI's rough guide of about 0.75 words per token suggests approximately 1,333 tokens for 1,000 words. This is a planning estimate, not a fixed conversion. Code, punctuation, unusual words, other languages, and the chosen encoding can change the result; paste the actual text for a count.

Does the OpenAI tokenizer work for GPT-5.6?

You can inspect GPT-5.6 prompts with this tool's compatible o200k_base view. Current tiktoken also maps the gpt-5.6 name through its gpt-5 prefix rule. That lookup alone does not verify a model's complete request accounting; use the official input-token counting endpoint or API usage for structured requests.

Which tiktoken encodings are available?

The tool includes o200k_base, cl100k_base, p50k_base, p50k_edit, r50k_base, and GPT-2.

How do I count tokens with tiktoken in Python?

Load an encoding with tiktoken.get_encoding, call encode on the text, and use the length of the returned token ID list.

Can I see individual token IDs?

Yes. Token segments and IDs are linked by hover highlighting.

Will a tiktoken count equal OpenAI API usage?

Not always. Structured inputs, tools, images, files, cached content, and model-side processing can affect request or response usage.

Does the browser upload my text?

No. Supported tiktoken encodings run locally in the browser.