PythiloLesson 4 of 12 · progress kept in this browser

Practice in Lab: Reserve output space before adding retrieval context →

Reserve output space before adding retrieval context

A context window is shared by instructions, user input, retrieved evidence and the generated output. Filling the whole budget with documents can leave no room for an answer. Reserve fixed input and output costs first, then admit whole evidence chunks under the remaining allowance.

This exercise accepts measured token counts and a ranked list. It skips a chunk that does not fit, allowing a later smaller chunk to enter, and deduplicates identifiers. It does not tokenize text or claim that character length equals model tokens. The caller must measure counts with the target model's tokenizer and include formatting overhead.

window,fixed,output=100,20,30
remaining=window-fixed-output
print(remaining)
print(40<=remaining)
Your task

Implement pack_context(chunks,window,fixed,output). chunks are (id,token_count), already ranked. Return admitted ids and total used context tokens. Skip oversized chunks, deduplicate admitted ids, require nonnegative integer counts/budgets and fixed+output<=window.