Counting tokens offline: tiktoken encodings and Hugging Face tokenizers
Answer: Count tokens by counting the length of the list returned by .encode(). Knowing the count tells you whether a string is too long for a model to process and how much a call costs, as usage is priced by token.
tiktoken
- Use
get_encoding()to load an encoding by name, orencoding_for_model()to retrieve the encoding for a model. The first run needs an internet connection to download. o200k_baseis used bygpt-4oandgpt-4o-mini;cl100k_basebygpt-4andgpt-3.5-turbo..decode()can be lossy for single tokens that aren't on utf-8 boundaries; use.decode_single_token_bytes().- Chat messages add per-message overhead, and requests that use functions consume extra tokens on top of the estimates.
Open models
The 🤗 Tokenizers library gives output.tokens and output.ids, with offsets into the original sentence.
It's best to process your texts by batches by using the encode_batch method.
See also
Claims
- id: C1 claim: Count is the length of encode(). source: S1 evidence: "Count tokens by counting the length of the list returned by
.encode()."
- id: C2 claim: Single-token decode can be lossy. source: S1 evidence: "it can be lossy for tokens that aren't on utf-8 boundaries"
- id: C3 claim: encode_batch handles batches. source: S2 evidence: "process your texts by batches by using the
Tokenizer.encode_batchmethod"
Sources
- [S1] How to count tokens with tiktoken. openai/openai-cookbook (GitHub). https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb . License: MIT. Accessed 2026-11-16.
- [S2] Tokenizers quicktour. huggingface/tokenizers (GitHub). https://github.com/huggingface/tokenizers/blob/main/docs/source-doc-builder/quicktour.mdx . License: Apache-2.0. Accessed 2026-11-16.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: openai/openai-cookbook (GitHub), How to count tokens with tiktoken, MIT (https://raw.githubusercontent.com/openai/openai-cookbook/main/LICENSE). Copyright (c) 2025 OpenAI; huggingface/tokenizers (GitHub), Tokenizers quicktour, Apache-2.0 (https://raw.githubusercontent.com/huggingface/tokenizers/main/LICENSE). Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: openai/openai-cookbook (GitHub), How to count tokens with tiktoken, MIT (https://raw.githubusercontent.com/openai/openai-cookbook/main/LICENSE). Copyright (c) 2025 OpenAI; huggingface/tokenizers (GitHub), Tokenizers quicktour, Apache-2.0 (https://raw.githubusercontent.com/huggingface/tokenizers/main/LICENSE).
Cite this
iinventedlight. 2026. "Counting tokens offline: tiktoken encodings and Hugging Face tokenizers." iinventedlight Library, iil:note:bnv5mych, version of 2026-11-16. https://iinventedlight.com/library/notes/ao-tokens-tiktoken-counting/. License: CC-BY-4.0.
@misc{iil_note_bnv5mych,
author = {{iinventedlight}},
title = {{Counting tokens offline: tiktoken encodings and Hugging Face tokenizers}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-16},
url = {https://iinventedlight.com/library/notes/ao-tokens-tiktoken-counting/},
note = {Permanent ID iil:note:bnv5mych. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/ao-tokens-tiktoken-counting.md.