Pack Retrieved Chunks into a Token Budget

medium
Unit 17 · RAG II0/3 to milestone

After retrieval and reranking you have more relevant chunks than the context window can hold. Choose what goes in.

Each chunk is a dict with "id", "score" (relevance) and "tokens" (its length). Implement pack_context(chunks, budget, reserve=0):

  • The available budget is budget - reserve (leave room for the question and the answer).
  • Greedily consider chunks by score, highest first (ties by id), adding each one that still fits. Skip chunks that don't fit and keep going, since smaller ones later may still fit.
  • Don't include two chunks with the same "source" and overlapping "span" (both optional keys, where span is a (start, end) tuple). Keep the higher-scored one.
  • Return the chosen ids in their original document order (by position in chunks), plus the total tokens used: (ids, used).
#rag#context-window#greedy2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Pack Retrieved Chunks into a Token Budget · TechGradify