Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:24kchengYe MemoMind Token Budgeting

From Leeroopedia
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.

Related Pages

Implemented By

Uses Heuristics

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment