Packing passages into a token budget
The 4-characters-per-token rule is a planning estimate. Use your provider's tokenizer for the final check.
Everything in the prompt competes for one fixed window, so sources get a token budget. You count tokens with a stated rough rule, pack passages best-first, decide what to do with one that does not fit, and order the rest. More context is not better.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write pack(hits, budget, overhead) where hits is a list of (doc_id, text) pairs in rank order. Keep a passage only when its cost (estimate plus overhead) still fits; skip passages that do not fit and carry on. Return the kept ids. Print the result for the example hits with a budget of 80.
Write at_the_edges(ranked) that puts rank 1 first and rank 2 last, rank 3 second and rank 4 second to last, and so on. Print the order for ranks ["a", "b", "c", "d", "e", "f"].
Explain it without notes
How do you estimate tokens without a tokenizer, and when is that not good enough?
Describe the 'stop' and 'skip' packing policies and the trade-off between them.
Why is sending more passages not always better?
What can the order of passages change?
Expected output
D1 31 tokens + 8 overhead
D3 31 tokens + 8 overhead
D12 30 tokens + 8 overhead
D9 16 tokens + 8 overhead
stop at the first miss: D1, D3 (78 of 105 tokens)
skip what does not fit: D1, D3, D9 (102 of 105 tokens)