Context Engineering for Coding Agents: Using Compressed Summaries to Sustain Long-Running Tasks

    This article explores context compression as a core context engineering practice for coding agents. It explains why longer context is not always better, how a structured summary works as a "save point" for resuming work, what a proper compression request contains, how clients rebuild context after compression, and the broader lessons for building reliable agent systems.

    1. From “Can’t Live Without Coding Assistants” to “Can’t Live Without Coding Agents”

    Over the past few years, developers’ reliance on intelligent coding tools has gone through a clear generational shift. Early coding assistants centered on “completion”—developers still held all the context themselves. Today’s coding agents can autonomously understand tasks, plan steps, call tools, and modify multiple files, while developers increasingly play the role of “reviewer.”

    This shift is especially tangible in day-to-day work. Tasks like quickly scaffolding a frontend annotation system, building a demo page for an algorithm capability, or implementing a small tool of personal interest used to take half a day to a full day of mechanical effort. Now, an agent can often produce a runnable result within a few rounds of dialogue. As the underlying models keep improving, agent performance rises with them, and this has become part of many developers’ daily workflows.

    However, the longer and more autonomous the task chain, the more one core technical problem surfaces: context management. Against the backdrop of development agents characterized by “long conversations + tool calls + multi-file modifications,” this article examines one of the most delicate pieces of the puzzle—context compression.

    2. Why Longer Context Isn’t Always Better

    Intuitively, the more complete the context you give a model, the better it should perform. But in real-world development agent scenarios, this intuition is wrong. There are four reasons:

    1. The window overflows. A complex refactoring task may involve dozens of conversation turns, hundreds of tool call results, and numerous file diffs. Accumulated together, these quickly exceed the model’s context window limit.

    2. Costs rise. Every call resends the entire history, so token consumption grows linearly—or even superlinearly—with conversation length. Long tasks can become prohibitively expensive.

    3. Attention gets diluted. Dead ends from early exploration, failed attempts, and verbose tool outputs drag down the model’s grasp of “the current task focus,” degrading output quality.

    4. Task drift. This is the most insidious problem: as the conversation stretches, the model may “forget” the original instructions and gradually deviate from the user’s core intent, making changes that seem reasonable but point in the wrong direction.

    Therefore, one of the key capabilities of a high-quality agent is not “remembering everything,” but selective forgetting—retaining the minimal sufficient information needed to resume work while discarding procedural noise.

    3. Context Compression: A “Save Point” for Work

    The best analogy for understanding compression is a save point in a video game.

    • Before saving: the model must carry the entire history—user messages, tool calls and their results, file changes—to keep working.
    • After saving: the model only needs to read a structured summary to quickly restore its state and continue.

    Note that this is not a “literary summary,” but state compression oriented toward continued development. The structured design of the summary directly determines the quality of resumption. It typically covers the following dimensions:

    • The user’s core requirements: what the user actually wants and what the constraints are. The summary is required to list all user messages, preventing the original intent from being “averaged out” and diluted.
    • Work completed: how problems were solved and where progress currently stands.
    • Key technical decisions: which interfaces were used, which technology choices were made, which tools were called.
    • Files and code involved: specific filenames, key code snippets, function signatures. This item is especially important—without it, the model cannot directly get hands-on with the code after resuming.
    • Errors and fixes: what pitfalls were encountered and how errors were resolved, so they aren’t repeated.
    • To-do items and next steps: a clear task list, which serves as the anchor for “seamless resumption.”

    As you can see, every aspect of this summary format reflects the orientation of a development agent: it is not a retrospective report written for humans, but a snapshot of the work site written for the model.

    4. What a Compression Request Actually Contains

    A common misconception is that compression means sending the conversation history to a model with a note saying “please summarize this.” In reality, a well-formed compression call consists of at least three parts:

    1. The complete historical context: including user messages, assistant replies, and all tool_use and tool_result interactions—tool execution results often contain factual information essential for resuming work (file contents, error messages, etc.).

    2. Detailed structured output instructions: a long, highly specific set of formatting requirements that stipulate, item by item, which fields the summary must include and which items must be listed exhaustively (e.g., “list all user messages,” “list the filenames and function signatures involved”). The more specific the instructions, the higher the state fidelity of the summary.

    3. Role definition and tool descriptions: the system message and the list of available tools are packaged into the same request, so the model completes compression within a single call, and the summary’s “contextual perspective” stays consistent with the original conversation.

    5. Post-Processing the Output and Restoring State

    The summary returned by the model does not directly become the new conversation—the client still performs critical assembly work:

    • Replace the original lengthy conversation history with the summary, rather than appending to it;
    • In the next request, inject the summary as a “previously on…” preamble at the front of the context, so the model attends to it first;
    • Preserve the necessary system prompts and tool definitions to ensure the agent’s behavioral capabilities don’t degrade after resumption.

    A schematic processing flow looks roughly like this:

    1. Original context (overlong):
    2. [system] + [msg_1..msg_N] + [tool_use/tool_result × M]
    3. │ compression triggered
    4. ▼
    5. Compression request = [system] + [msg_1..msg_N] + [compression task instructions]
    6. │ model generation
    7. ▼
    8. Structured summary = {
    9. core requirements, completed work, technical decisions,
    10. involved files and code snippets, errors and fixes, to-do list
    11. }
    12. │ client-side replacement
    13. ▼
    14. New context = [system] + [structured summary] + [subsequent new messages]

    6. General Lessons for Context Engineering

    The ideas behind this compression mechanism offer useful lessons for anyone building agent systems:

    1. Context is a resource, not a warehouse. Manage context the way you manage memory: with a budget, an eviction policy, and a “working set.”
    2. A summary must be state, not narrative. Structured information oriented toward task resumption is far more valuable than a fluent prose recap.
    3. The specificity of instructions determines summary quality. “Summarize this for me” and “list all user messages and function signatures” produce fundamentally different results.
    4. Compression should be a routine mechanism, not an emergency measure. Proactively and periodically saving state during long tasks is the only way to avoid crude truncation after the window overflows.

    Context engineering is becoming a core competency in agent development. Understanding the “compression as save point” design paradigm is the first step toward building reliable development agents that can sustain work over the long haul.