Context Engineering to Stop AI Coding Rework: A Four-Stage Workflow

    This article explains why AI coding tools often produce messy, half-finished code and increasing rework, and argues that the root cause is usually chaotic context rather than weak model capability. It introduces context engineering as the foundation of a mature workflow, covering the capacity red line, attention dilution, conflict amplification, and instruction forgetting. It then presents a four-stage workflow—Research, Plan, Implement, and Validate—with a fresh context at each stage, plus guidance on固化ing the process through custom command files and avoiding common anti-patterns. Readers will learn how to control context usage, keep each stage focused on a single goal, and reduce rework by turning long, unpredictable sessions into short, controlled flows.

    1. The Real Cause of Rework: Not a Weak Model, but Messy Context

    Many developers who are new to AI coding tools have had a similar experience: the requirements are described clearly, but the tool starts “making things up” midway, generates a large amount of half-finished code, and becomes harder to fix with every change. The first reaction is often to switch to a stronger model, but the problem usually lies not in model capability, but in the context fed to it—its structure is chaotic, it contains too much noise, and the goal drifts.

    The essence of an AI coding tool is “conditional probability prediction within a limited context window.” When the window is packed with irrelevant files, outdated code, duplicate snippets, and contradictory requirements, the model treats this noise as valid signal and its output naturally loses focus. Therefore, the first cornerstone of a mature workflow is not prompt tricks, but context engineering: consciously selecting, compressing, isolating, and refreshing the information that enters the window.

    2. More Context Is Not Better: The Capacity Red Line and Safety Margin

    Contrary to intuition, the higher the context fill rate, the worse the output quality often becomes. There are three reasons:

    • Attention dilution: key information is drowned out by a large amount of secondary information, making it hard for the model to lock onto the real constraints.
    • Conflict amplification: when old and new versions of code and outdated documentation are present at the same time, the model may waver between them.
    • Instruction forgetting: content in the middle of a long context is the most easily weakened, and specifications set early are gradually “forgotten.”

    In practice, it is advisable to set a capacity red line: when usage exceeds about 60%, stop adding information and switch to organizing, summarizing, or resetting. You can monitor usage at any time with the tool’s built-in context viewing capability (a common approach is to use a slash command to check the usage ratio).

    It is worth noting that when a new session starts, foundational components such as the system prompt, project description files, extension tool declarations, and custom agents already take up a considerable proportion (commonly around 30%). This means the space truly available for business reasoning may be less than 30%. Without active management, a few file references can hit the ceiling. The following pseudocode illustrates “budget awareness”:

    1. Total window = 100%
    2. Base usage ≈ 30% # system instructions, tool declarations, project conventions
    3. Available for business ≈ 25%~30%
    4. Red line = 60% # organize or reset once exceeded

    3. A Four-Stage Workflow: Divide and Conquer, One Goal per Stage

    Breaking complex feature development into four stages and refreshing the context at the end of each stage is key to suppressing rework. The core philosophy is “avoid writing code whenever possible”—no code means no defects, so first clarify the current state and the plan.

    Stage 1: Research

    The goal is to understand the current state, not to produce code. It is recommended to launch multiple read-only subagents in parallel, each responsible for: directory and module structure, key call chains, data models and interface contracts, and tests and configuration. The four streams of results are consolidated into a research document that must include: a summary of findings, code references with file:line (mandatory, for traceability), architecture insights, and a list of questions to clarify.

    Stage 2: Plan

    Based on the research document, produce an implementation plan: a list of files to change, interface signatures, boundary conditions, rollback strategy, and validation cases. In this stage, write only the plan, not the implementation, and explicitly list “what not to do” to prevent scope creep. Only after the plan is reviewed and approved should you move to the next stage.

    Stage 3: Implement

    Advance one verifiable small goal at a time, strictly following the plan. After completing each sub-goal, run a static check or unit test once to avoid accumulating errors. If you find that the plan conflicts with the current state, immediately return to the planning stage to revise it, rather than “patching it up” in the code.

    Stage 4: Validate

    Use testing, type checking, logging, and monitoring alerts to confirm that behavior matches expectations, and add regression cases. After validation passes, output a change summary and remaining items, then reset the context and move on to the next feature.

    4. Solidifying the Process with Custom Commands

    You can solidify the four stages into custom instruction files within the project (a common approach is to place them in an agreed command directory, using Markdown to describe each stage’s responsibilities), so that the tool executes them in a fixed pattern. The illustrative structure is as follows:

    1. .claude/commands/
    2. research.md # read-only research, output a research document with file:line
    3. plan.md # output the change list and validation cases, no implementation allowed
    4. implement.md # small-step implementation, self-check at each step
    5. validate.md # testing, checks, summary, and cleanup

    Each file should state three things clearly: the single goal of this stage, the allowed and forbidden actions, and the deliverable format. This both constrains the model’s behavior and naturally compresses the context when switching stages.

    5. Common Anti-Patterns and Fixes

    • Feeding the entire repository at once: change to targeted references per question, summary first and details later.
    • Not resetting long sessions: start a new session at the end of each stage, carrying only the previous stage’s deliverables.
    • Piling up requirements verbally: write constraints into the project description file to avoid repeated mid-course additions.
    • Skipping validation: break validation into minimal commands that can be executed automatically to lower the execution cost.

    6. Conclusion

    Context engineering and the four-stage workflow essentially transform “uncontrollable long sessions” into “controllable short flows.” By holding the capacity red line, doing only one goal per stage, and actively resetting between stages, the rework rate will drop significantly. Going forward, it is possible to further combine general cloud capabilities such as external knowledge retrieval, log services, and monitoring alerts to automate the research and validation stages, forming a more mature engineering loop.