Context Management for Coding Agents: Designing a Graph-Based Plugin to Reduce Context Pollution

    This article presents the design and implementation of a context management plugin for tool-using AI agents in software development. It explains why context quality degrades over long sessions, how a graph-based model makes context visible and editable, how agent execution nodes resolve context through incoming edges, and the engineering practices required for permissions, storage, and observability. It also covers a hybrid rendering pipeline that combines still frames with code-driven rendering to control production costs. The approach is product-agnostic and can be reused in in-house toolchains or engineering practices on Baidu AI Cloud.

    1. The Problem: Context Pollution in Long Sessions

    When using tool-capable agents for development tasks, the recurring bottleneck is rarely model capability. It is the steady decay of context quality as the conversation grows. After dozens or even hundreds of turns, the history typically contains four kinds of content at once: assumptions that have already been disproven, large volumes of tool output produced during debugging, branch explorations unrelated to the current goal, and the decisions and constraints that genuinely need to be retained long term. In a linear conversation, all of it is stacked flat, with no difference in weight.

    Given this history, a model has only two default strategies: read everything, or automatically compress it into a summary. Neither resolves the core conflict, which is that the user cannot accurately tell which content is still influencing the next round of reasoning. Compression loses information and is not controllable; reading everything continuously consumes budget and amplifies noise. What engineering actually needs is to hand over the right to select context: before the next round begins, a human decides which context the agent carries forward.

    2. Core Design: Make Context Visible and Editable

    The central idea of the plugin is to reshape a session from a timeline into a graph. The interface adds a toggle above the existing conversation area to switch between a conversation view and a graph view, and an existing session can be opened as a continuously updated context graph. The whole system follows a single hard rule: an edge is context.

    Node types fall into three categories: dialogue nodes carry historical turns, material nodes carry external input, and note nodes carry conclusions distilled by a human. The semantics of an edge are unique and explicit — content connected into a question node forms the context actually submitted to the model when that question is executed; removing an edge takes a branch out of subsequent requests, while the node and its content remain on the canvas and can be restored at any time.

    This design yields three direct benefits. First, pruning is reversible: deleting context becomes “cutting an edge” rather than “destroying content,” so auditing and rollback are cheap. Second, compression becomes controllable: a long conversation can be condensed by hand into a shorter new path, the original record is never overwritten, and the summary coexists with the source with traceable provenance. Third, input is unified: document parsing results, web excerpts, and supplementary notes can all be attached as material nodes to new question nodes, forming a single context pool.

    3. Execution Path: Agent Nodes on the Graph

    The graph needs one special node type representing a real agent execution. Once selected, the question is submitted as an actual run, still able to call the tools and permissions already configured in the runtime; the result is written back into the graph as a new node and also kept in the run log, so it aligns with the existing session audit system.

    A sketch of the data structure looks like this:

    1. {
    2. "nodes": [
    3. {"id": "n1", "type": "dialogue", "round": 18, "summary": "Caching hypothesis attempts"},
    4. {"id": "n2", "type": "material", "ref": "schema_diff_report"},
    5. {"id": "n3", "type": "note", "text": "Root cause: data structure change"},
    6. {"id": "n4", "type": "agent", "question": "Propose a fix based on the above"}
    7. ],
    8. "edges": [
    9. {"from": "n3", "to": "n4"},
    10. {"from": "n2", "to": "n4"}
    11. ],
    12. "context_resolution": "Recursively collect along incoming edges only, deduplicate, then concatenate in topological order"
    13. }

    Context resolution should follow three constraints: collect recursively along incoming edges only; deduplicate repeated content by content fingerprint; concatenate in topological order to keep the sequence stable and avoid prompt jitter caused by differences in traversal implementation.

    4. A Real Scenario

    Imagine debugging a code defect. The first twenty turns all suspected the caching layer, with extensive searching, changes, and failed attempts, until the root cause turned out to be a change in the data structure. In a linear session, those twenty turns in the wrong direction stay in the history and keep interfering with judgment.

    After the redesign, the handling is: keep the “caching hypothesis” branch as an auditable record but do not connect it to the current question; distill the analysis of the data structure change into a note node; attach the before-and-after structure diff report as a material node; the subsequent question node then receives only these three sources. The result is a substantial drop in context length, with a clear basis for every choice, so teammates can review why a given judgment was made at the time.

    5. Another Capability: Rendering Instead of Generating to Control Cost

    Frontend and multimedia output has the same kind of cost structure problem. A purely generative approach scales cost close to linearly as duration and resolution increase, and offers weak controllability. A more economical path is still frames plus code rendering: use image generation to produce key still frames, use a node-based workflow to control style and composition, then drive camera movement, transitions, and subtitle composition with code, calling a video generation model only for the segments that genuinely need motion. This hybrid pipeline shifts most of the compute from frame-by-frame generation to deterministic rendering and composition, making cost more predictable and easier to version and reproduce.

    6. Engineering Implementation Notes

    • Permissions and isolation: when material nodes ingest external content, the source must be labeled separately from instructions, so external text is not executed as high-priority instructions.
    • Storage and versioning: the graph structure should be serializable and diffable across versions, and node content should use content addressing to enable reuse and deduplication.
    • Observability: record the fingerprint of the context actually in effect for each execution, making it easier to determine whether an abnormal result stems from context selection.
    • Incremental adoption: start with a read-only view of the context composition, then open up editing, to lower the team’s learning cost.
    • Working with existing capabilities: the context graph decides what to include, while logs and traces record what was done; the two complement rather than replace each other.

    7. Conclusion

    The focus of context engineering is shifting from how to fit in more to how to give precisely less. Making context a visible, editable graph — where pruning is reversible, summaries are traceable, and inputs can be attached uniformly — is a practical way to improve the stability of agents on long tasks; combined with a hybrid output pipeline of still frames and code rendering, it can also optimize cost while keeping quality under control. These ideas are not tied to any specific product and can be reused directly in in-house toolchains or in engineering practice on Baidu AI Cloud.