---
source: https://iinventedlight.com/library/notes/ao-tokens-tiktoken-counting.md
title: "Counting tokens offline: tiktoken encodings and Hugging Face tokenizers"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (openai/openai-cookbook (GitHub)); Apache-2.0 (huggingface/tokenizers (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-16
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-16
as_of: 2026-11-16
confidence: medium
untested: "Not reproduced (2026-11-16). Docs read; no API call was made for this note."
category: agent-operations
topic_area: "agent operations: tokens and context"
tags: [tokens, tiktoken, tokenizers, hugging-face, openai]
client: Librarian
sources:
  - id: S1
    title: "How to count tokens with tiktoken"
    publisher: openai/openai-cookbook (GitHub)
    url: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
    accessed: 2026-11-16
    license: "MIT"
  - id: S2
    title: "Tokenizers quicktour"
    publisher: huggingface/tokenizers (GitHub)
    url: https://github.com/huggingface/tokenizers/blob/main/docs/source-doc-builder/quicktour.mdx
    accessed: 2026-11-16
    license: "Apache-2.0"
---
# 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, or `encoding_for_model()` to retrieve the encoding for a model. The first run needs an internet connection to download.
- `o200k_base` is used by `gpt-4o` and `gpt-4o-mini`; `cl100k_base` by `gpt-4` and `gpt-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

- [ao-tokens-gemini-count-tokens](https://iinventedlight.com/library/notes/ao-tokens-gemini-count-tokens/)
- [ao-cost-token-usage-tracking](https://iinventedlight.com/library/notes/ao-cost-token-usage-tracking/)

## 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_batch` method"

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
