DEV Community

Patrick
Patrick

Posted on

Context Window Pollution: The Silent Agent Degradation Problem

Context Window Pollution: The Silent Agent Degradation Problem

Your AI agent starts sharp. Give it a task, it executes cleanly.

Give it the same task two hours later, after running continuously? It might fumble it.

This is not a model problem. It is a context window problem.

What Happens Over Time

Every tool call leaves residue in the context: partial API responses, dead-end reasoning steps, error messages from retries, stale task context. The model does not cleanly distinguish "fresh instructions" from "junk from 47 steps ago."

Result: degraded decisions. The right answer is buried under noise.

The Fix: A Context Flush Rule

Add this to your SOUL.md:

Every 20 steps, summarize completed work to memory/progress.md,
clear intermediate reasoning, and reload only the current task context.
Enter fullscreen mode Exit fullscreen mode

Fresh context = sharper decisions. Less noise, more signal. Progress is preserved via the summary file — nothing important is lost, just restructured.

Three Signs You Have Context Pollution

  1. Agent quality degrades mid-task. Early decisions are good; later ones are sloppy.
  2. Unnecessary retries. Agent re-doing work it already did.
  3. Token usage per step grows linearly instead of staying flat.

Staged Context: A More Advanced Pattern

For long-running agents, separate context into layers:

  • Core context (always loaded): identity, rules, current task
  • Working context (loaded per step): recent tool results, current state
  • Archive context (on-demand only): historical results, previous decisions

Only pull archive context when explicitly needed. Core reasoning stays clean.

The Takeaway

Agent reliability is not just about what you tell the agent at the start. It is about what the context looks like after 50 steps.

Design for context hygiene from the beginning.


The Ask Patrick Library includes agent configs with context flush rules built in. → askpatrick.co

Top comments (2)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.