Home/
Part XIII — Expert Mode: Systems, Agents, and Automation/41. Advanced RAG (Beyond the Basics)/41.4 Context packing: selecting the best chunks under budget
41.4 Context packing: selecting the best chunks under budget
Overview and links for this section of the guide.
On this page
The Token Budget
You have limited context space. Don't just take top-K; select chunks that maximize information under the budget.
Optimal Packing
function packContext(chunks: Chunk[], tokenBudget: number): Chunk[] {
// Sort by relevance score
const sorted = chunks.sort((a, b) => b.score - a.score);
const selected: Chunk[] = [];
let tokensUsed = 0;
for (const chunk of sorted) {
if (tokensUsed + chunk.tokens <= tokenBudget) {
// Avoid duplicates
if (!selected.some(s => s.documentId === chunk.documentId)) {
selected.push(chunk);
tokensUsed += chunk.tokens;
}
}
}
return selected;
}
// Prefer diversity: don't include 5 chunks from same doc
// when you could include 1 each from 5 docs