Progress

Level 06 · Agentic

Context Engineering

Context engineering means the model only sees a limited amount of text at once, so you choose what to include. This level shows how to budget that space, cut or move the rest, and keep a long task going after one call fills up. An AI engineer needs this because stuffing every note into the prompt makes answers worse and slower, even when the facts were available on disk.

Assumes You can already sketch a small tool-using helper that sends a prompt and reads a result back. · 13 study sessions · 8 modules

0 of 42 lessons0%

Ship it

Capstone: Context-Managed Long-Horizon Agent

Start from a small tool-using script and give it a task long enough that keeping every note in the prompt would bury the goal. The model only sees a limited amount of text at once, so you add a numeric budget, a shorter summary, files for the bulky state, and a simple loop around the same script.

Finish by comparing the careful version with a keep-everything version on that same task. You do not need a paid key. Plain Python, local files, and a free chat window are enough.

  1. 1. A long task that degrades naively

    1. Open naive_agent.py and invent a six-step task, such as packing a handbook into notes. Each step appends a forty-word observation to a list.
    2. Do not trim. Print the word count after each step and the goal line.

    3. On purpose, let a later observation contradict an early constraint, and keep both.

    4. Paste the full transcript into a free chat window and ask what the constraint was.

    5. Done when the word count climbs at every step and you saved the transcript as baseline.txt, including whether the chat missed the constraint.

  2. 2. An explicit context budget

    1. Open budget.py and set a word cap, such as 300, for the text you would send.
    2. Before each pretend model call, label every block keep, fetch later, compress, offload, or drop.

    3. The function must read the live message list, not a hand-edited copy you only wrote once.

    4. Print the label and the word count per block.

    5. Done when a block that would overflow the cap is labeled compress, offload, or drop, and the keep blocks fit under the cap.

  3. 3. Compaction, offloading & retrieval

    1. Replace older turns with a summary that still names the goal. Store it in summary.txt.
    2. Write bulky observations into files and leave only a path in the message list.

    3. On the current step, read only the one note that matches that step.

    4. Shrink a fake tool payload to id and status before it re-enters the list.

    5. Done when you can point to four functions or four clearly named blocks, and the printed window word count stays under the cap from the previous step.

  4. 4. Wrapping the agent in a harness

    1. Add progress.json and tasks.md that the script updates after each step.
    2. Refuse to write done until a check you chose has passed, such as a required field in the JSON or a tiny test file.

    3. Cap the loop at six steps so a runaway cannot continue.

    4. Start a second process that reads only the files and prints the next unchecked task. It must not import the old transcript string.

    5. Done when the second process prints the right next task and a failed check leaves done unset.

  5. 5. Holding the goal across a long run

    1. Put the original goal, the constraints, the finished work, and the open item in progress.json.
    2. Make the resume path read those four fields every time, not the casual observations.

    3. Write a compactor that drops observations but must copy the four fields forward. Test it by deleting a constraint on purpose and showing that your check fails.

    4. Fix the compactor so the constraint survives.

    5. Done when a resume print shows all four items after compaction and the intentional deletion was caught before you continued.

  6. 6. Managed vs keep-everything comparison

    1. Run the keep-everything script and the managed script on the same six-step task.
    2. Record whether each stayed on the goal, plus the word count you would send, as a stand-in for cost and delay.

    3. Keep the task text and the tool functions the same. Only the context policy may differ.

    4. Write which failures the budget removed, such as a forgotten constraint, and which ones it did not.

    5. Done when a short note shows two rows, managed and naive, and the managed word count is lower while the goal is still present.