Context Engineering and Compression
You can pack four short spans, 16 tokens, into a 40-token prompt. The whole corpus is seven short spans, 29 tokens, and three of those spans stay out. You keep all three keyed sentences, place each key token outside the middle band, and score a five-query trace at prefix match 5/5 and prefix reuse 0.80.
- Level
- Navigator
- Time
- 20 h
- Prerequisites
- R01-101

Jump to
After this course you can
The student can pack and prune a 200-page document collection to fit a fixed token budget while achieving ≥80% cache-prefix reuse and preserving 100% of keyed answer facts.
Keys first, then page order
In this lesson one token is one piece after you split on spaces. The budget B is 40 tokens. Thirty-two of them form the prefix, the leading run that stays identical from token 1 on every query in the trace. A trace is five questions in a row against the same frozen spans. The other 8 tokens are the tail, and the tail may change when the question changes. 32 plus 8 is 40, so the slack is 0. The prefix holds the rules, two copies of the key index, and the spans you kept. The tail holds one few-shot example and the question. A few-shot example is a 4-token sample of the reply shape, chosen for the question you are answering now.
The rules line is Follow these rules. Cite spans. Keep keys verbatim. Split it into Follow, these, rules., Cite, spans., Keep, keys, verbatim. That is 8 tokens, and those 8 open every packed prompt. The key index is Keys fee=40 hours=20 city=Lund. Split it into Keys, fee=40, hours=20, city=Lund. That is 4 tokens. You write the index twice, once after the rules and once after the spans, so the indexes cost 8. The span budget inside the prefix is 32 minus 8 minus 8, which is 16. Two people who reserve those same blocks and then fill the 16 by the same sort will write the same 32 prefix tokens.
Seven spans are the whole corpus for the hand count. Page 1, keyed, k1: The fee is 40, 4 tokens, key token fee=40. Page 2, keyed, k2: The hours are 20, 4 tokens, key token hours=20. Page 3, filler, f4: Lobby has green plants, 4 tokens. Page 4, filler, f5: Staff meet every Monday, 4 tokens. Page 5, filler, f6: Stairs sit by the door, 5 tokens. Page 6, filler, f7: Coffee is free today, 4 tokens. Page 8, keyed, k3: The city is Lund, 4 tokens, key token city=Lund. A keyed span is one of those three full sentences. A filler span may leave the pack. Six spans have length 4 and one has length 5, so the corpus is 24 plus 5, which is 29 token. 29 is larger than 16, so some spans stay out.
Sort keyed spans before filler, and break remaining ties by ascending page. The order you consider is k1, k2, k3, f4, f5, f6, f7. Take the next span when its length still fits in the 16. k1 takes 4 and leaves 12. k2 takes 4 and leaves 8. k3 takes 4 and leaves 4. f4 takes 4 and leaves 0. f5 needs 4 with 0 left, so f5 stays out. f6 needs 5 and stays out. f7 needs 4 and stays out. The kept spans are k1, k2, k3, and f4, and 4 plus 4 plus 4 plus 4 is 16. Span recall is how many of the three full sentences appear. Here recall is 3 of 3. Page order skips the keyed-first sort and walks pages 1, 2, 3, 4, 5, 6, then 8. k1 leaves 12, k2 leaves 8, f4 leaves 4, f5 leaves 0, and f6, f7, and k3 stay out. That pack keeps k1, k2, f4, and f5. The full sentence The city is Lund is absent, so span recall is 2 of 3.
The middle band and the two indexes
Number the finished prompt from token 1 through token 40. The middle band is tokens 13 through 28, the whole span block. Tokens 1 through 12 and tokens 29 through 40 sit outside the band. The reason for the band is lost in the middle: measured use of a long prompt can be weaker for tokens in the middle than for tokens near the start and near the end. You do not estimate that effect here. You place each key token outside the band and then read the index. The blocks are fixed on an accepted pack. Tokens 1 through 8 are the rules. Tokens 9 through 12 are the first key index. Tokens 13 through 28 are the four kept spans in page order. Tokens 29 through 32 are the second key index. Tokens 33 through 36 are the few-shot example. Tokens 37 through 40 are the question.
Page order among the kept spans puts k1, then k2, then f4, then k3. Tokens 13 through 16 are The fee is 40. Tokens 17 through 20 are The hours are 20. Tokens 21 through 24 are Lobby has green plants. Tokens 25 through 28 are The city is Lund. The hours sentence begins at token 17, inside the band. The city sentence begins at token 25, inside the band. The fee sentence begins at token 13, which is also inside 13 through 28. The first index is four tokens: 9 Keys, 10 fee=40, 11 hours=20, 12 city=Lund. The second index repeats them: 29 Keys, 30 fee=40, 31 hours=20, 32 city=Lund. A Keep token is covered when that exact token appears at least once outside the band. fee=40 is covered at 10 and at 30. hours=20 is covered at 11 and at 31. city=Lund is covered at 12 and at 32. Covered count is 3 of 3.
The full sentence and the Keep token are different pieces. The city is Lund splits into The, city, is, Lund. The Keep token city=Lund is a single piece, and it occurs at 12 and at 32. Finding the sentence is the span-recall check. Finding the single token outside the band is the edge check. A pack is accepted when span recall is 3 of 3, each Keep token sits at least once outside tokens 13 through 28, and the length is 40. Drop both index lines and the three Keep tokens occur nowhere, so the covered count is 0 of 3, even while the three sentences still fill the span block. The sentences sit inside the band, so they do not supply the edge copy. Page order can keep the index, because the index is copied from the Keep line, and still fail acceptance: the edge count stays 3 of 3 while span recall falls to 2 of 3, since The city is Lund never enters tokens 13 through 28.
The cache prefix and the few-shot tail
The canonical prefix is tokens 1 through 32 of the accepted fee pack, and the tail is outside that prefix. Prompt caching stores the work already done for a leading run that the next request repeats from token 1. A different token 1 means that stored work does not attach to the new prompt. A different token 33 leaves the first 32 available for reuse. Prefix match is the number of prompts in the trace that start with all 32 canonical tokens, divided by the number of prompts. Prefix reuse is the sum of the shared leading runs, divided by the sum of the prompt lengths. Five prompts of 40 tokens give the denominator 5 times 40, which is 200. The same two scores are what you compute when a frozen corpus is hundreds of pages. You still pack once into the prefix, you still leave the question in the tail, and you still divide those two sums. The seven spans are the version you can number by hand.
The five questions are What is the fee, What are the hours, What is the city, What is the fee, and What is the fee. Each question is 4 tokens, and each chosen example is 4 tokens at positions 33 through 36. All five prompts start with the same 32 tokens. Prefix match is 5/5, which equals 1. The shared-run sum is 5 times 32, which is 160. Prefix reuse is 160/200. Because 200 times 0.80 is 160, the reuse is 0.80. The bar in this lesson is a prefix match of at least 0.80 and a prefix reuse of at least 0.80. The five-prompt trace meets both. Move the question What is the fee to tokens 1 through 4 on one prompt, and leave the other four prompts as they were. Token 1 on the moved prompt is What, while canonical token 1 is Follow, so that shared run is 0. Prefix match is 4/5, which is 0.80. The shared-run sum is 4 times 32, which is 128. Prefix reuse is 128/200, and 200 times 0.64 is 128, so the reuse is 0.64. Match still clears 0.80. Reuse falls short until the question returns to tokens 37 through 40.
Choose the example with an overlap you can count. Lowercase, strip punctuation, and drop these stopwords: the, is, a, are, on, of, for, what. Overlap is the size of the shared set of remaining words. The bank has four lines, each 4 tokens. E1, id 1: Fee answer 40 cites. E2, id 2: Hours answer 20 cites. E3, id 3: Fee guest asks twice. E4, id 4: City answer Lund cites. For What is the fee the content set is fee. E1 shares fee and scores 1. E3 shares fee and scores 1. E2 scores 0. E4 scores 0. On a tie, the smaller id wins, so k=1 selects E1, and tokens 33 through 36 are Fee answer 40 cites. For What are the hours the content set is hours, E2 scores 1, and the selection is E2. For What is the city the content set is city, E4 scores 1, and the selection is E4. The example sits after the prefix, so a different example changes token 33 and leaves tokens 1 through 32 in place. k=2 on the fee question also takes E3. Two examples are 8 tokens. Added to a 40-token pack they make 44, which is 4 over the budget.
Pruning and the delegation contract
When the prompt is over 40, drop whole spans and whole examples. Leave every Keep token written the way the Keep line writes it. The drop order is fixed. First drop filler spans from the highest page to the lowest page. Then drop dynamic examples from the lowest overlap to the highest overlap, and on an equal overlap drop the larger id first. The rules, both key indexes, every keyed span, and the question stay. If the length is still over 40, write the word overflow and ship nothing. A pack that lost the sentence The city is Lund, or the token fee=40, or the token hours=20, waits for a new budget. It does not go out under a shorter spelling of those keys.
Start from the accepted 40-token fee pack and add E3, which is 4 tokens, for a total of 44. The excess is 4. The only filler in that pack is f4 on page 3, and f4 is 4 tokens. Drop f4 and the excess is 0. The span block is now k1, k2, and k3, which is 12 tokens. Span recall stays 3 of 3. The new layout through the second index is rules 8, index 4, k1 4, k2 4, k3 4, index 4, which is 28 tokens, and the tail is E1, E3, and the question, 12 tokens. 28 plus 12 is 40. Against the canonical 32, the shared leading run stops at 20. Tokens 1 through 20 are still the rules, the first index, k1, and k2. Token 21 in the pruned pack is The, the first piece of The city is Lund. Token 21 in the canonical prefix is Lobby, the first piece of Lobby has green plants. This pruned prompt contributes 20, not 32, to a reuse sum, and it does not count as a prefix match against the 32.
Write a delegation contract before the pack, and hand those six lines to the later pass. A delegation contract is the delegeringsmall: a fixed handoff so the later pass packs from the lines instead of from memory. The lines and their token counts are Goal pack under 40 tokens (5), Inputs pages 1 to 8 (5), Keep fee=40 hours=20 city=Lund (4), Drop filler before any key (5), Order rules index spans index example question (7), and Budget 40 (2). Add 5 plus 5 to get 10, plus 4 to get 14, plus 5 to get 19, plus 7 to get 26, plus 2 to get 28. The contract is 28 tokens. The Keep line is the list of key tokens, and it is also the source of both index copies. If that line is missing, the later pass can walk page order with span budget 16, keep k1, k2, f4, and f5, and omit The city is Lund. Span recall is then 2 of 3. With the Keep line present, the same pass sorts keyed spans first, and span recall stays 3 of 3.
The fee prompt, token by token
Write the accepted fee prompt as nine groups. Group A, tokens 1 through 8: Follow these rules. Cite spans. Keep keys verbatim. Group B, tokens 9 through 12: Keys fee=40 hours=20 city=Lund. Group C, tokens 13 through 16: The fee is 40. Group D, tokens 17 through 20: The hours are 20. Group E, tokens 21 through 24: Lobby has green plants. Group F, tokens 25 through 28: The city is Lund. Group G, tokens 29 through 32: Keys fee=40 hours=20 city=Lund. Group H, tokens 33 through 36: Fee answer 40 cites. Group I, tokens 37 through 40: What is the fee. Group A is 8 tokens and the other eight groups are 4 each. Eight times 4 is 32, and 32 plus 8 is 40.
Read the three acceptance checks off those groups. The full sentences in C, D, and F are The fee is 40, The hours are 20, and The city is Lund, so span recall is 3 of 3. In B the Keep tokens are fee=40 at 10, hours=20 at 11, and city=Lund at 12. In G they are fee=40 at 30, hours=20 at 31, and city=Lund at 32. None of 10, 11, 12, 30, 31, or 32 lies inside 13 through 28, so the covered count is 3 of 3. The length is 40. On the five-query trace this prompt contributes a shared run of 32. Five such runs sum to 160, and 160/200 is 0.80.
Two nearby packs fail a different check. Move group I to the front and this prompt's shared run against the canonical prefix is 0. Four untouched prompts still contribute 32 each, the sum is 128, and 128/200 is 0.64, with prefix match 4/5. Build groups C through F by page order instead, and those groups become The fee is 40, The hours are 20, Lobby has green plants, and Staff meet every Monday. The city is Lund is absent, so span recall is 2 of 3. Groups B and G can still list city=Lund, because they are copied from the Keep line, and the pack still fails acceptance on span recall.
Score a hours question and a broken trace
Use the rules above. For the question What are the hours, with k=1, name the example id and write the four tokens at positions 33 through 36. Then take span budget 16 and page order, name the four span ids you keep, and write the span recall. Next, take five prompts of 40 tokens where four of them share the 32-token prefix and one has the question at token 1. Write the prefix match and the prefix reuse. Then start from the 40-token fee pack, add E3, drop f4, and write the shared leading run against the canonical 32, plus the word at token 21. Last, write the contract length and the line that lists fee=40, hours=20, and city=Lund.
The hours question has content set hours, so k=1 selects E2. Tokens 33 through 36 are Hours answer 20 cites. Page order with span budget 16 keeps k1, k2, f4, and f5, and span recall is 2 of 3 because The city is Lund is absent. The broken trace has prefix match 4/5, which is 0.80, and prefix reuse 128/200, which is 0.64. After you add E3 and drop f4, the shared leading run is 20, and token 21 is The. The contract is 28 tokens, and the Keep line lists the three key tokens.
Check questions
With span budget 16 and keyed-first order, which four span ids do you keep, and what is the span recall?
k1, k2, k3, and f4. Span recall is 3 of 3, because The fee is 40, The hours are 20, and The city is Lund are all in the span block.
On five prompts of 40 tokens that each start with the same 32-token prefix, what are the prefix match and the prefix reuse?
Prefix match is 5/5, which is 1. Prefix reuse is 160/200, which is 0.80, because 5 times 32 is 160 and 5 times 40 is 200.
In the accepted fee pack, at which token positions does hours=20 appear, and does token 17 sit inside the middle band?
hours=20 appears at token 11 and at token 31. Token 17 is inside the middle band, tokens 13 through 28, and it is the first token of The hours are 20.
With span budget 16 and page order, which full keyed sentence is absent, and which contract line lists the key token that sentence carries?
The city is Lund is absent, so span recall is 2 of 3. The Keep line lists city=Lund, together with fee=40 and hours=20.
An accepted 40-token pack fills a 16-token span budget with keyed spans first, repeats Keys fee=40 hours=20 city=Lund at tokens 9 through 12 and 29 through 32, and leaves the example plus the question in the last 8 tokens. Five prompts that share those 32 prefix tokens score prefix reuse 0.80. One question moved to token 1 drops that reuse to 0.64, and page order drops span recall from 3 of 3 to 2 of 3.
Final task
Submit a deterministic context-packing program that processes a frozen 200-page corpus within a strict token ceiling, achieves ≥80% prefix match across a 30-query trace, and retains all 30 keyed evidence spans.
Terms in this course
This course has no terms of its own in the glossary yet. Terms from earlier courses are used here too.
The first lesson is open to read. The rest of the course is being written, and tests and diplomas come later. Join the waitlist