Stop Reworking with AI Coding Assistants: Master Context Management and a 4-Stage Workflow

    When using AI coding assistants, developers often encounter buggy code, contradictions, and hallucinations, leading to endless rework. The root cause usually lies in disorganized context rather than the model's capabilities. This article explores context engineering principles—such as maintaining a 60% context usage limit and providing precise information—and introduces a practical four-stage workflow: Research, Plan, Implement, and Validate. By dividing complex tasks into manageable stages and passing results through documentation, developers can minimize errors, improve AI output quality, and standardize their AI-assisted development process.

    Many developers new to AI coding tools share a familiar experience: you ask the AI to implement a moderately complex feature. The initial code looks fine, but errors emerge upon execution. After several rounds of patching, the AI starts contradicting itself, repeating rejected solutions, or even hallucinating non-existent APIs. Ultimately, you abandon the conversation and start from scratch.

    Most people blame the model’s capabilities. However, extensive practice shows that the root cause often isn’t the model itself, but the chaotic and unstructured context provided by the user. This article offers an actionable four-stage workflow from the perspective of context engineering to help developers eliminate rework.

    1. Why More Context Isn’t Always Better

    1.1 Intuition vs. Reality

    Human intuition suggests that the more comprehensive the information given to an AI, the more accurate its response will be. Consequently, beginners often stuff every relevant file into the conversation, attempting to turn the entire codebase into context.

    The result is the exact opposite. When context is bloated, the model’s attention is diluted, critical constraints are buried by irrelevant details, and output quality drops sharply: code becomes incomplete, solutions contradict themselves, and the conversation loops in circles.

    This relates to how Large Language Models (LLMs) work: attention resources are limited. The longer the context, the more likely early or middle instructions are “forgotten”—a phenomenon known as “lost in the middle.” Context is a scarce resource, not a free warehouse.

    1.2 Setting a Context Usage Red Line

    A widely accepted practice is to keep context usage under 60%. Beyond this threshold, output instability increases significantly.

    Note that available context space is smaller than expected. When a new session starts, infrastructure content like system prompts, project-level configurations, tool definitions, and custom agent descriptions often consume about 30% of the capacity. This means the actual space left for business dialogue might only be 25% to 30%, equivalent to tens of thousands of tokens.

    Therefore, proficient developers develop two habits:

    • Assess “baseline usage” at the start of a session to understand the remaining available space.
    • Regularly check context usage during conversations, decisively wrapping up, clearing, or restarting the session when nearing the red line.

    1.3 Precise Supply vs. Total Feeding

    Instead of dumping the entire project, provide only three types of information:

    1. Directly relevant code: Files and functions touched by the current change.
    2. Constraints and standards: Coding style, architectural conventions, and prohibitions.
    3. Clear task goals: Inputs, outputs, and acceptance criteria.

    All other “potentially useful” content should be retrieved on-demand via tools, rather than pre-stacked into the context.

    2. The 4-Stage Workflow: Divide and Conquer

    Complex features lead to rework because developers try to accomplish “understand current state, design solution, write code, and validate” all in a single conversation. Blowing up the context is inevitable.

    The correct approach borrows the “divide and conquer” principle from software engineering, splitting the process into four stages. Each stage uses an independent session or clean context, passing results between stages via documentation.

    2.1 Research: Understand the Status Quo Before Coding

    An old programming adage says: don’t write code if you can avoid it, because no code means no bugs. Another interpretation is: fully understand the existing codebase before taking action.

    The goal of the Research stage is to produce a Research Document, which should include:

    • Discovery summary: The current status of modules related to the requirement.
    • Code references with file paths and line numbers: This is critical for precise location. It allows subsequent stages to jump quickly and prevents the AI from inaccurately “reciting” code from memory.
    • Architecture insights: Module dependencies and data flow.
    • Questions to clarify: Explicitly list uncertainties instead of letting the AI guess.

    For large projects, multiple sub-tasks can explore different modules in parallel, eventually aggregating into one document. No business code is produced in this stage.

    2.2 Plan: Write Down the Solution Before Acting

    Based on the research document, the Plan stage produces an Implementation Plan, including:

    • Which files are involved and what changes to make in each.
    • Responsibilities and interface definitions for new modules.
    • The sequence and dependencies of implementation steps.
    • Potential risks and rollback strategies.

    After the plan is written, a human must review it. The cost of this step is much lower than reworking after writing code. Many “AI got it wrong” issues stem from unclear thinking during the planning phase; the AI simply executed a vague instruction faithfully.

    2.3 Implement: Code with a Clean Context

    When entering the Implement stage, open a new context and load only two things: the Implementation Plan and the relevant files specified in the plan. Let the AI implement step-by-step strictly according to the plan, confirming after each step, rather than generating all code at once.

    Disciplines for the Implement stage include:

    • Do not allow the AI to deviate from the plan and “improvise.” If the plan is flawed, return to the Plan stage to revise it.
    • Commit in small steps; save every verifiable increment promptly.
    • When context approaches the red line, log current progress and start a new session to continue.

    2.4 Validate: A Non-Negotiable Closed Loop

    Finally, the Validate stage is also recommended to use a new context to avoid the cognitive inertia of the implementation phase affecting judgment. Validation includes:

    • Running automated tests, and having the AI supplement missing test cases if necessary.
    • Checking item-by-item against initial requirements and acceptance criteria.
    • Reviewing code style, security, and edge case handling.

    Issues found during validation: if they are local defects, fix them in the current session; if systemic flaws are exposed, return to the Plan stage to re-derive, rather than patching during validation.

    3. Solidify the Workflow with Custom Commands

    Manually executing the four stages is prone to deviation. It is recommended to write the operational rules of each stage as structured prompt documents (e.g., four Markdown files) and register them as shortcut commands via the tool’s custom command mechanism. This way, triggering the corresponding command at each stage makes the AI execute the task according to the predefined role, inputs, outputs, and formats, standardizing and making the process reproducible.

    A good stage prompt should include: role definition, input materials, execution steps, output format (especially hard requirements like “file path:line number”), and prohibitions.

    4. Conclusion

    The ceiling of AI-assisted development depends heavily on the user’s engineering capabilities, not just the model. Remember two core principles:

    1. Context is a scarce resource: Keep it under 60%, supply precisely, and retrieve on demand.
    2. Complex tasks must be staged: Research, Plan, Implement, and Validate independently, connecting stages with documents, and clearing context after each stage ends.

    When teams need greater context capacity or stronger model reasoning performance, they can also leverage cloud LLM services (such as the model inference and development platform capabilities provided by Baidu AI Cloud) to build a more stable assisted development environment. Tools will iterate, but the engineering mindset of “managing context, divide and conquer” will remain effective long-term.