Why the long-running agent loses the thread
Read a multi-step agent's symptoms and name the missing pattern (carried state, context pruning, memory or subagents), then pick the change that applies it, using a framework only where it adds value.
Scenario
A migration agent moves 60 API handlers to a new error format. At step 5 it settles the format. By step 45 it is writing handlers in a different one. The next morning it reopens all 60 files to work out which ones it already did.
The model didn't get worse overnight. The agent around it is missing patterns.
Explanation
Most multi-step failures come from one of four missing patterns. Read the symptom, then name the pattern.
| Symptom | Missing pattern | Fix |
|---|---|---|
| Forgets what happened two steps ago, within one run | Carried state | The Messages API is stateless: append every assistant response and its tool results to messages and send the whole list each turn |
| Late in a long run: slower, costlier, contradicts its own earlier decisions; most of the context is old tool output | Context pruning | Clear old tool results (context editing replaces them with placeholders), load data just in time by reference (file paths, queries), or compact the history into a summary |
| Each new session starts from zero | Memory | Let the agent write notes outside the context: the memory tool (files under /memories that your code stores), or a progress file plus git history it reads first |
| One agent handles several independent deep investigations, and raw material from all of them piles up | Subagents | Give each part to a subagent with a fresh context; only its final summary returns to the lead. Pass everything it needs in its prompt: it doesn't see the parent's conversation |
Why pruning matters even when the context isn't full: as the token count grows, the model's ability to recall what's in it drops. The goal is the smallest set of high-signal tokens. Keep decisions; drop raw output once it has been used.
Compaction and pruning work inside one run. Memory survives between runs. Pick by when the information is needed again.
Frameworks. The exam guide names Strands, LangGraph and PydanticAI. Frameworks simplify standard plumbing (calling the model, defining and parsing tools, chaining calls), which pays off when you would otherwise write and maintain that plumbing yourself. They also add layers that can hide the actual prompts and responses. A framework doesn't add a missing pattern by itself: you still choose what to keep, prune and remember, and you need to know what it sends.
Worked example
Before. The migration agent from the Hook, in one long session:
Step 5: decides errors are {code, message, details}
Steps 6-40: read_file on 34 handlers, each result kept in full
Step 44: request is 150k tokens, 120k of them old file contents
Step 45: writes {error, msg} in handler 37
Next day: new session, no notes, re-reads all 60 handlers
Two patterns are missing:
- Context pruning (steps 6-44): 34 files stay in context long after they were edited, and the step 5 decision is buried under them.
- Memory (next day): nothing outside the context records the format or the progress.
After.
Step 5: writes the format to /memories/error-format.md
Each handler: read, edit, run tests, append "done: <file>" to /memories/progress.md
Context editing clears old tool results past a threshold
Next day: reads /memories first, continues at handler 38
The decision now lives in a file the agent re-reads, old file contents no longer crowd the context, and the next session starts from the progress list.