GPT-4 Tokenizer

Token count

0

GPT-4 uses the cl100k_base encoding in OpenAI's tiktoken. Paste text above to see its token count, token ids and how the text is split.

Token counts for sample texts

TextSampleTokens
EnglishThe quick brown fox jumps over the lazy dog.10
Chinese你好,世界!今天天气很好。16
Codedef add(a, b): return a + b11

FAQ

Which tokenizer does GPT-4 use?

cl100k_base, according to the model name table in OpenAI's tiktoken. The encoding page lists the other models that share it.

How do I count GPT-4 tokens in code?

With OpenAI's tiktoken. Use a recent release so that the model name is known:

# Python
import tiktoken
enc = tiktoken.encoding_for_model("gpt-4")
print(len(enc.encode(text)))

// JavaScript
import { encoding_for_model } from "tiktoken";
const enc = encoding_for_model("gpt-4");
console.log(enc.encode(text).length);
enc.free();

Is my text sent to a server?

No. Tokenization runs in your browser and the text never leaves the page. For usage statistics the page only reports which tokenizer you pick and that you started typing, never the text itself.

Why can the count differ from the usage reported by an API?

Chat requests wrap your text in special tokens and per-message formatting that this page does not show. For billing and limits, use the usage field in the API response as the authoritative number.

Related tokenizers