Tiktoken explained · Interactive Python guide

What Is Tiktoken?

Tiktoken is a tokenizer library for converting text into token IDs with OpenAI encodings. Try the process interactively, then reproduce it with a short Python example.

Accuracy note

This page demonstrates raw tiktoken encodings. Complete model requests may include structured input and provider-side accounting beyond the text shown here.

Official counting docs →
Input
Unicode text
Output
Integer token IDs
Common encodings
o200k_base · cl100k_base
Python package
tiktoken

Facts checked against official sources on .

What Tiktoken Does

Tiktoken converts text into a sequence of integer IDs from a selected encoding vocabulary. Language models process those IDs rather than raw words. A familiar word may map to one token, while an unusual name, long number, emoji, or piece of source code may be split into several tokens.

The conversion is reversible for complete token sequences: encoded IDs can be decoded back into text. The interactive example above makes the mapping visible by pairing colored text segments with token IDs, which is useful for learning, debugging, and prompt-size analysis.

How Tiktoken Tokenization Works

A tiktoken encoding uses a fixed vocabulary and merge rules to represent frequent byte patterns efficiently while retaining the ability to encode arbitrary text. Token boundaries therefore do not match words consistently. Leading spaces, punctuation, capitalization, and adjacent characters can change the selected token sequence.

Encoding names identify different vocabularies. The same text can produce different IDs and counts under o200k_base, cl100k_base, p50k_base, r50k_base, or GPT-2. Token IDs should only be interpreted together with the encoding that produced them.

Tiktoken Python Example

In Python, load a named encoding and call encode to obtain token IDs. Use len on the returned list for the raw-text count, and decode the complete list when you want to confirm that it reconstructs the original input.

Match the encoding name and exact text when comparing Python with an online tokenizer. Newlines, Unicode punctuation, and invisible spaces are part of the input and can explain a disagreement.

pythonOfficial counting example
import tiktoken

encoding = tiktoken.get_encoding("o200k_base")
text = "Tiktoken converts text into token IDs."
ids = encoding.encode(text)

print(ids)
print(len(ids))
print(encoding.decode(ids))

What Tiktoken Is Used For

  • Inspect raw token boundaries and IDs while developing or debugging prompts.
  • Compare how the same text behaves under different OpenAI encoding vocabularies.
  • Estimate the text portion of a prompt before making an API request.
  • Find costly repetition, whitespace, code, or multilingual segments in long inputs.
  • Create reproducible tokenizer tests in Python or compare them with browser output.

When Tiktoken Is Not the Final Count

A local tokenizer explains the text layer, but an API request can include instructions, conversation history, tools, images, files, cached input, and generated output. Those components may not be represented by encoding one visible string.

For an OpenAI Responses request, use the request-level input token counting operation when you need the total for supported structured input, then read the response usage after generation. Keep tiktoken for boundary inspection and repeatable raw-encoding work.

Tiktokenizer Reference Sources

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

Frequently asked questions

Is tiktoken a tokenizer?

Yes. It converts text to token IDs using a selected OpenAI encoding and can decode token sequences back to text.

Is tiktoken the same as ChatGPT?

No. Tiktoken is a tokenizer library; it does not generate answers or run a language model.

How does tiktoken count tokens?

It encodes text with a selected vocabulary and returns a list of token IDs. The number of IDs is the raw-text token count.

Which tiktoken encoding should I use?

Use the encoding associated with the model or comparison you are testing. Do not assume one encoding matches every model.

Can tiktoken count a complete API request?

It can count an encoded text representation, but structured inputs and provider-side components may require the API's request-level token count or final usage data.