Why Your Agent Forgets Instructions After Context Condensing
Your formatting rules worked perfectly. For the first twenty exchanges.
Then the context window filled up, the agent condensed, and suddenly it stopped following your output structure.
The symptom
You have strict instructions about output format. Every review comment should follow a specific template. Every code suggestion should include a test case. The agent knows this. It follows the rules.
Until it doesn't.
After a long session, the agent condenses your conversation to fit within context limits. The next response comes back wrong. Not wrong in content, wrong in structure. The formatting rules vanished.
"I was seeing some weird behavior with some of my workflows. Some of my workflows have really like strict instructions about the output how it should look and I saw that it was just after condensing it was just forgetting how to properly format you know comments reviews."
Dan,
You didn't change anything. The agent didn't misunderstand. Something got lost in the condensing.
The root cause
The problem was in what got replaced during condensing. The first message in your conversation contains your directive: the system prompt, your formatting rules, your workflow constraints. That message is the anchor.
When the context window fills up and condensing kicks in, the system needs to summarize. The naive approach: replace the first message with something like "please continue with this summary." The summary captures the conversation state but drops your original instructions.
"It turns out the first message was being replaced with something else when condensing something along the lines of please continue with this summary or something like that."
Dan,
The model now treats the summary as the main instruction. Your formatting rules existed in the first message. The first message is gone. The rules are gone.
Why providers do middle-out
This explains a pattern you might have noticed: many providers use middle-out transformations when hitting context limits. They preserve the first message and the recent messages while summarizing the middle.
"The first message includes... that's why providers for example do middle out transformations when when you reach the context window limit... because the first message contains your directive."
Dan,
The first message is architecturally significant. It's not just another turn in the conversation. It's the directive that shapes everything else. Lose it, and the model loses its purpose.
The fix
The solution is straightforward: preserve the first message during condensing. Don't replace it with a generic "continue with this summary" prompt. Keep the original directive intact and summarize the middle of the conversation instead.
After implementing this fix, agents stay on task across long sessions. The formatting rules persist. The workflow constraints hold. The condensing happens, but the directive survives.
Context condensing approaches compared
| Dimension | Naive condensing | Middle-out condensing |
|---|---|---|
| First message handling | Replaced with summary prompt | Preserved intact |
| Directive persistence | Lost after condensing | Retained across sessions |
| Formatting rule survival | Rules vanish at boundary | Rules persist through condensing |
| Long session reliability | Drift increases over time | Consistent behavior maintained |
| Recovery required | Manual re-instruction needed | No intervention required |
How Roo Code preserves your directives
Roo Code implements middle-out context management to keep your workflow instructions intact across long sessions. When the context window fills, Roo Code summarizes the middle of the conversation while preserving both your original directive and recent context. This means your formatting rules, output structures, and workflow constraints survive the condensing boundary.
Roo Code preserves the first message during context condensing, ensuring your workflow instructions and formatting rules persist across long agentic sessions.
Because Roo Code closes the loop by running commands, tests, and iterating based on results, maintaining consistent instructions is critical. An agent that forgets your output format mid-task creates manual correction work. With directive preservation, you can run complex multi-step tasks knowing your constraints will hold from start to finish.
What to check
If your agent is drifting after long sessions, check what happens at the condensing boundary:
- Is your first message being preserved or replaced?
- Does the summary include your directive, or just the conversation state?
- Are your formatting rules making it through the condensing intact?
The symptom is instruction drift. The cause is usually the first message getting dropped. The fix is preserving it.
Frequently asked questions
Stop being the human glue between PRs
Cloud Agents review code, catch issues, and suggest fixes before you open the diff. You review the results, not the process.