liveBook · Manning

Tiktoken

As explained in Essential GraphRAG

tiktoken is a tokenization library developed by OpenAI for encoding text and counting language-model tokens.

tiktoken is a Python package for counting tokens in text. It selects a model encoding and measures the length of encoded strings, helping identify sections that may exceed an LLM context limit. section 3.2

Count tokens in a section

Define num_tokens_from_string(string, model="gpt-4"), obtain the model encoding with tiktoken.encoding_for_model(model), encode the string, and return len(encoding.encode(string)). Applying it to sections produces counts such as 154, 254, 4,186, and 2,703.

How Essential GraphRAG covers it

How deep it goes
Defines and demonstrates model-specific encoding and counting, then stops short of explaining tokenizer internals.
Examples
Counts tokens across extracted sections, including sections with 154, 4,186, and 2,703 tokens.
What you can do after
Apply it via tiktoken to measure text length and decide when retrieval sections require further splitting.
How it connects
Connects tokenization with LLM context limits, document chunking, and parent document retrieval.

Also explained in

Compare how 2 books explain tiktoken →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential GraphRAG. The section links point to the parts of the book it draws on.