Assemble, generate, cite·5 min
Context assembly
0 / 1
You have ranked, de-duplicated chunks; now pack them into the prompt under a token budget. The rules, pinned: dedup by chunk id, order by descending score (ties → ascending id), greedily add chunks while countTokens(formatted) <= budget, stop at the first that won't fit (don't skip ahead — that keeps the output deterministic), and format each block as [doc i] (sourceId) text.
Greedy packing (budget 200)
chunks by score: doc1(90 tok), doc2(80 tok), doc3(70 tok)
+doc1 -> 90 (fits)
+doc2 -> ~170 (fits)
+doc3 -> ~240 (> 200) -> STOP
output: [doc 1] ... \n\n [doc 2] ... (two blocks)