Track state, progress and context
Checkpoint progress so work can resume, and keep the context window focused with compaction, notes and just-in-time retrieval.
- Design explicit, durable progress state
- Resume after an interruption without repeating work
- Manage a long run’s context with compaction and notes
A multi-step loop needs state: the goal, constraints, completed steps, key results and what’s next. Keep it explicit, compact and durable - saved outside the model’s context - so a crash, timeout or restart doesn’t lose an hour of work. The model can propose a state update; your code validates it before saving.
Long runs also fight context rot: as the context fills up, models recall details less reliably (Anthropic, “Effective context engineering for AI agents”). Techniques:
- Compaction - summarize old history and drop stale tool output, keeping decisions and recent details.
- Structured notes - the agent keeps a progress file or to-do list outside the context and rereads it.
- Just-in-time retrieval - keep references (paths, IDs, queries) and load content only when needed.
- Sub-agents - delegate focused work to agents with clean contexts that return short summaries.
Try it
Up to 15 XPWhat stays in the context?
A long coding session. Switch window sizes: what gets dropped? Which parts would you compact into a summary, and which would you keep as notes outside the context?
Using 23,800 of 32,000 tokens (4,000 reserved for the answer) · the full conversation is 94,800 tokens.
- System prompt + tool docs6,000
- Early exploration (file listings)30,000
- Test output from 40 runs45,000
- Design decisions so far3,000
- Progress notes (to-do list)1,500
- Last 5 steps9,000
- Current instruction300
1import json
2state = {"goal": "migrate 3 tables", "done": ["users"], "next": "orders"}
3saved = json.dumps(state)
4print(json.loads(saved)["next"])orders
Key takeaways
Keep explicit, validated, durable state: goal, done, next.
Checkpoint so interrupted work resumes instead of restarting.
Fight context rot with compaction, notes, just-in-time retrieval and sub-agents.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Python
Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.
Resume from a checkpoint
Line 1 is a JSON checkpoint {"done": [...]}. Line 2 is the JSON plan (a list of step names). Print resuming: D/T steps already done, then skip STEP or run STEP for each planned step, then done: T/T.
- Two of four done
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Compact a long history
Line 1 is N (at least 1). Each following line is a message role: content. Keep the system message(s) and the last N other messages verbatim. Replace everything older with one line summary: K earlier messages compacted (T tool results dropped), placed right after the system messages. Print the result (omit the summary line if nothing was compacted).
- Keep the last two
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…