P

Tiktoken: Definition and Examples

Tiktoken is the open-source tokenization library developed by OpenAI, used to split text into tokens before sending it to language models like GPT-4.

Full definition

Tiktoken is a fast tokenization library developed and maintained by OpenAI. Its main role is to convert raw text into a sequence of tokens — the basic units that language models actually process. Unlike simple word or character splitting, Tiktoken uses the Byte Pair Encoding (BPE) algorithm to produce an optimal split that balances efficiency and language coverage.

In practice, Tiktoken allows developers to precisely count the number of tokens in a text before sending it to the OpenAI API. This feature is essential because GPT models have context limits expressed in tokens (e.g., 128,000 tokens for GPT-4 Turbo), and billing is also calculated per token. Without a reliable counting tool, it is impossible to optimize costs or avoid context window overflow errors.

The library is written in Rust for performance reasons, with Python bindings that make it easy to use. It supports several encodings corresponding to different OpenAI models: cl100k_base for GPT-4 and GPT-3.5 Turbo, o200k_base for GPT-4o, and p50k_base for older models. Each encoding has its own vocabulary and splitting rules.

Tiktoken has become a reference tool in the prompt engineering ecosystem because it allows designing prompts that fully exploit the available context window without exceeding it. It is also used to estimate API usage costs before execution, making it an indispensable component of any production pipeline using OpenAI models.

Etymology

The name "Tiktoken" is a wordplay combining "tik" (evoking counting or the ticking of a clock) and "token" (the fundamental processing unit of LLMs). It reflects the library's primary function: counting tokens accurately.

Concrete examples

Counting tokens in a prompt before sending to the API

import tiktoken
enc = tiktoken.encoding_for_model('gpt-4')
tokens = enc.encode('Explain prompt engineering to me')
print(len(tokens)) # Displays the number of tokens

Truncating a long text to respect the context window

Use tiktoken to split your document into chunks of up to 4,000 tokens before sending them one by one to the model with an intermediate summary.

Estimating the cost of an API call before execution

Before sending this corpus of 500 articles to GPT-4, use tiktoken to calculate the total number of tokens and estimate the cost at $0.03/1K input tokens.

Practical usage

In prompt engineering, Tiktoken is used to verify that your prompts and documents fit within the context window of the target model. Install it via pip (pip install tiktoken), then use encoding_for_model() to get the encoder for your model and encode() to count tokens. It is an essential habit when working with long texts or trying to optimize API costs.

Related concepts

TokenizationByte Pair Encoding (BPE)Context windowToken

FAQ

Does Tiktoken work with Anthropic's Claude models?
No, Tiktoken is specific to OpenAI models. Claude uses its own tokenizer. To estimate tokens with Claude, you can use Anthropic's token counting API or third-party tools. Vocabularies and splitting rules differ between providers.
Why is the number of tokens different from the number of words?
A token does not correspond to a word. Depending on the language and model vocabulary, a common English word may be a single token, whereas a rare word or a French word may be split into 2 to 4 tokens. Numbers, punctuation, and spaces also consume tokens. On average, one token represents about 0.75 words in English and 0.5 words in French.
Which Tiktoken encoding should I use for which model?
Use o200k_base for GPT-4o and its variants, cl100k_base for GPT-4, GPT-4 Turbo, and GPT-3.5 Turbo, and p50k_base for older models like text-davinci-003. The tiktoken.encoding_for_model() method automatically selects the correct encoding based on the model name.

See also

How to use this prompt

  1. Copy the prompt with the button above.
  2. Paste it into ChatGPT, Claude or your favorite AI assistant.
  3. Replace the bracketed variables with your details, then refine the result.

About Prompt Guide

Prompt Guide is a free library of 2500+ ready-to-use prompts for ChatGPT, Claude and other AIs, with guides to learn prompting and tools to build and optimize your own prompts.

More definitions

Get new prompts every week

Join our newsletter.