Context Clearing Technique in RPI Workflows: A Complete Guide to LLM State Management
The context clearing technique is a disciplined engineering practice that resets the LLM's conversation history between Research, Plan, Implement, and Review phases using the /clear command, ensuring each agent operates on a clean slate without recency bias or context contamination.
The context clearing technique is foundational to the Research → Plan → Implement → Review (RPI) workflow implemented in the microsoft/hve-core repository. By explicitly isolating each phase through conversation resets, this method prevents token accumulation and enforces strict separation of concerns across complex AI-assisted development cycles.
What Is the Context Clearing Technique?
The context clearing technique explicitly resets the LLM's conversation state between each RPI phase by issuing the /clear command or initiating a fresh chat session. According to the source code documentation in docs/rpi/context-engineering.md (lines 55-62), this operation removes all prior chat tokens while preserving phase artifacts stored in .copilot-tracking/, leaving only the system prompt and the current phase's instructions.
Unlike simple UI convenience, this technique functions as an architectural constraint that prevents context contamination—the phenomenon where implementation details leak into research phases or exploratory speculation pollutes production code generation.
How Context Clearing Works Across RPI Phases
The technique operates differently across each workflow phase to maintain phase purity and prevent recency bias (the model's tendency to overweight recent tokens).
Research Phase Isolation
Without context clearing, the researcher’s prompt mixes with implementation code and validation logs, causing the model to skip fact-finding in favor of discussing recent implementation details. With /clear, the researcher works from a clean slate containing only the system prompt and research instructions, enabling focused fact-finding without distraction from downstream phases.
Planning Phase Independence
When the planning agent encounters uncleared implementation code dumps, it generates plans that reference already-written code, violating the research-first principle. After /clear, the planner sees only research artifacts from .copilot-tracking/ and the system prompt, producing implementation-independent architectural plans.
Implementation Phase Purity
Uncleared research discussions can reintroduce exploratory ideas that are no longer relevant to the approved plan. Context clearing ensures the implementor receives only planning artifacts and system prompts, writing fresh, production-ready code rather than iterating on discarded concepts.
Review Phase Precision
Reviewers processing full conversation histories may miss subtle mismatches because earlier phase outputs drown out review instructions. The /clear command isolates the reviewer to final implementation artifacts, enabling precise validation against specifications without historical noise.
Why Context Clearing Is Critical for RPI Workflows
As documented in docs/rpi/why-rpi.md (lines 172-176), the technique delivers five concrete engineering benefits:
-
Prevents Recency Bias — The model inherently weights recent tokens more heavily; clearing forces re-evaluation of each phase on correct, isolated inputs rather than whatever appeared last in chat history.
-
Maintains Phase Purity — Each RPI agent (researcher, planner, implementor, reviewer) can be fine-tuned for its specific purpose without being polluted by other phases' outputs, ensuring specialized optimization.
-
Keeps Token Usage Manageable — Without clearing, a full RPI cycle can exceed 200,000 tokens, far beyond standard LLM context windows. Clearing shrinks each request to a few thousand tokens, maintaining the effective context window within model limits.
-
Improves Predictability — Teams observe consistent, verifiable behavior: research never sees implementation code, and implementation never sees exploratory speculation, leading to higher-quality outcomes.
-
Supports Automation — Automated agents (e.g.,
rpi-agent) rely on deterministic inputs;/clearensures the same prompt always yields the same phase-specific result, as noted indocs/rpi/using-together.md(lines 38-45).
Implementing Context Clearing in Practice
Manual Context Clearing Between Agents
For manual RPI execution, issue /clear before transitioning between phases:
# Phase 1 – Research
/task-researcher ...
# ---- Reset ----
/clear
# Phase 2 – Planning
/task-planner ...
# ---- Reset ----
/clear
# Phase 3 – Implementation
/task-implementor ...
# ---- Reset ----
/clear
# Phase 4 – Review
/task-reviewer ...
Each /clear invocation removes conversation history while preserving artifacts in .copilot-tracking/, ensuring subsequent /task-* calls start with fresh context.
Automated Clearing with rpi-agent
The rpi-agent orchestrates the full workflow and internally inserts /clear between sub-agents:
/rpi # Starts full RPI workflow; handles /clear automatically
Critical warning: When running a second RPI cycle in the same chat, you must manually clear first:
/clear # Mandatory before subsequent /rpi requests
/rpi
This requirement is documented in docs/hve-guide/lifecycle/implementation.md (lines 95-102).
Programmatic Context Clearing via CLI
For automation scripts using the Copilot CLI or SDK, explicitly sequence the clear operation:
copilot chat send "/clear"
copilot chat send "/task-researcher ..."
copilot chat send "/clear"
copilot chat send "/task-planner ..."
The CLI forwards each message as user input, preserving the same isolation guarantees as manual interaction.
Key Configuration Files and Agent Definitions
The context clearing technique is embedded throughout the microsoft/hve-core codebase:
docs/rpi/context-engineering.md— Explains why/clearworks and how artifacts survive resets (lines 55-62)docs/rpi/README.md— High-level RPI overview emphasizing mandatory clearsdocs/rpi/using-together.md— Step-by-step guide with clear commands (lines 38-45)docs/rpi/why-rpi.md— Justification of RPI and contamination mitigation (lines 172-176).github/agents/hve-core/*.agent.md— Agent definitions embedding/clearprompts within each sub-agent configurationdocs/rpi/task-researcher.md,task-planner.md,task-implementor.md,task-reviewer.md— Phase-specific documentation including clear steps
Summary
- The context clearing technique uses the
/clearcommand to reset LLM conversation history between RPI phases. - It prevents recency bias and context contamination by ensuring each agent sees only relevant phase artifacts from
.copilot-tracking/. - The technique manages token budgets, keeping requests to a few thousand tokens rather than exceeding 200,000 tokens across full cycles.
- It enforces phase purity, allowing specialized agents to optimize for their specific objectives without cross-phase interference.
- Implementation requires explicit
/clearcalls in manual workflows, whilerpi-agenthandles clearing automatically during orchestrated runs.
Frequently Asked Questions
What is the context clearing technique in RPI workflows?
The context clearing technique is an architectural practice in the Research → Plan → Implement → Review workflow that resets the LLM's conversation state between phases using the /clear command. This ensures each specialized agent (researcher, planner, implementor, reviewer) operates on isolated inputs without contamination from previous phases' outputs.
How does the /clear command prevent recency bias?
The /clear command removes all prior chat tokens from the model's context window, eliminating the recency bias where LLMs overweight the most recent tokens in their output generation. By clearing between phases, the model evaluates each new phase's instructions and artifacts (stored in .copilot-tracking/) on their own merit rather than being influenced by whatever conversation occurred most recently.
Where are RPI artifacts stored when context is cleared?
When /clear executes, conversation history is purged but phase outputs persist on disk in the .copilot-tracking/ directory. Subsequent agents import these files as inputs without requiring the massive chat history to be resent, maintaining the effective context window within manageable limits while preserving workflow continuity.
Can I automate context clearing in the microsoft/hve-core workflow?
Yes. The rpi-agent automatically inserts /clear operations between its internal sub-agents when you invoke /rpi. For custom automation via the Copilot CLI, explicitly send /clear as a chat message between task calls: copilot chat send "/clear". According to docs/hve-guide/lifecycle/implementation.md, you must manually clear before starting subsequent /rpi cycles in the same chat session.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →