Principle:24kchengYe MemoMind Token Budgeting
| Knowledge Sources | |
|---|---|
| Domains | Information_Retrieval, LLM_Engineering |
| Last Updated | 2026-10-28 00:00 GMT |
Overview
Technique that limits the total token count of retrieved context to fit within an LLM's context window, packing results greedily until the budget is exhausted.
Description
Retrieval systems produce ranked lists of results, but LLMs have finite context windows. Token budgeting ensures the assembled context fits within a specified token limit. The method counts tokens (using tiktoken's cl100k_base encoding) for each result's text field and greedily includes results until adding the next would exceed the budget.
This is a greedy knapsack approach: results are processed in ranked order, and inclusion stops at the first result that would overflow. This prioritizes high-relevance results over fitting more lower-ranked ones.
Usage
Use this principle whenever retrieved results are fed into an LLM context window with a known size limit. It is the final step in a recall pipeline before results are formatted for LLM consumption.
Theoretical Basis
# Abstract token budgeting (not real implementation)
encoding = tiktoken.get_encoding("cl100k_base")
total = 0
filtered = []
for result in results:
tokens = len(encoding.encode(result["text"]))
if total + tokens <= max_tokens:
filtered.append(result)
total += tokens
else:
break # Stop at first overflow
return filtered, total
The greedy approach is optimal when results are pre-sorted by relevance (which they are, after reranking). Stopping at the first overflow ensures no partial result is included.