← Back to blogעברית

Context management in coding harnesses: what gets sent, and why it matters

How coding harnesses assemble model context from instructions, history, tools, and results; how previous messages are replayed; efficient strategies for long sessions; and what tool-call and MCP messages actually look like.

When people say a coding agent “remembers” the conversation, what they usually mean is simpler and more mechanical: the harness keeps sending the model a carefully assembled view of what matters right now. That assembly step is context management. It decides which instructions, which previous messages, which tool definitions, and which results are included in the next model request.

This is one of the most important jobs in a coding harness. Good context makes a capable model useful. Bloated, stale, or poorly structured context makes the same model slow, expensive, and confused.

How a harness assembles and updates context

What “context” means in a harness

For each turn, the harness builds a request from several sources:

  • Instructions: system/developer rules, safety policy, product behavior, and project guidance (for example AGENTS.md).
  • Conversation history: prior user messages, assistant replies, and records of tool use.
  • Tool surface: schemas/descriptions for tools the model can call now (built-in tools, plugins, MCP tools, etc.).
  • Fresh results: the latest tool outputs, command results, diagnostics, file contents, or environment facts.
  • State summaries: compacted history or notes that replace older detail when the window is tight.

The model does not see your whole repository or your whole session unless the harness puts relevant parts into this request. “Memory” in most agent loops is mostly state + selection.

Sending previous messages: replay, not magic

Most chat-style model APIs are stateless between requests. To continue a conversation, the harness sends a sequence of messages each time (or an equivalent structured history). A simplified shape looks like this:

[
  { "role": "system", "content": "You are a coding agent..." },
  { "role": "user", "content": "Fix the failing test in auth.ts" },
  { "role": "assistant", "content": "I'll inspect the test and implementation." }
]

On the next turn, the harness appends what happened since (for example a tool call and its result, plus the next assistant message) and sends the updated sequence. Different providers name fields differently, but the core idea is stable: previous turns are represented as messages in the request.

This means history is editable infrastructure. A harness can keep every message, drop irrelevant ones, rewrite old turns into summaries, or attach extra structured context before the model sees it.

How messages look with tool calls

When the model wants to take an action, it typically emits a tool/function call instead of (or alongside) plain text. The runtime executes that call and appends a tool result message linked to the call ID. A representative flow:

[
  { "role": "system", "content": "You can use tools to read and edit files." },
  { "role": "user", "content": "What does package.json name contain?" },
  {
    "role": "assistant",
    "content": null,
    "tool_calls": [
      {
        "id": "call_1",
        "type": "function",
        "function": {
          "name": "read_file",
          "arguments": "{\"path\":\"package.json\"}"
        }
      }
    ]
  },
  {
    "role": "tool",
    "tool_call_id": "call_1",
    "content": "{ \"name\": \"yonatanhanan.com\", ... }"
  }
]

After receiving the tool result, the model is called again with this expanded history and can answer in natural language or request more tools. The loop is: request -> tool call -> execution -> tool result -> request.

Important details harnesses handle around this:

  • Call/result pairing: every tool result must point to the right call (IDs matter).
  • Ordering: results are appended so the model can see cause and effect.
  • Parallel calls: some turns include multiple tool calls; results for all of them must be recorded.
  • Large outputs: raw stdout/file dumps are often truncated or saved to files, with a pointer placed in context.
  • Errors as data: failed tool runs are usually fed back as tool results so the model can recover.

Where MCP fits into the same picture

MCP (Model Context Protocol) standardizes how a client discovers and calls capabilities exposed by servers (tools, resources, prompts). From the harness’s perspective, MCP is a capability boundary. From the model’s perspective, MCP-provided tools still become part of the available tool surface.

In practice, the flow is:

  1. The harness connects to MCP servers and discovers tool definitions/schemas.
  2. Those tools are presented to the model alongside native tools (namespacing varies by harness).
  3. The model emits a tool call targeting an MCP tool name.
  4. The harness routes that call to the MCP server, executes it, and converts the result into the normal tool-result form the conversation uses.
  5. The model sees the result as context and continues.

So MCP does not create a separate “kind of message” for the model in most harnesses. It changes where tools come from and how they are executed, while the conversation still revolves around tool calls and tool results.

Efficient ways to manage context

Context windows are finite, latency and cost scale with tokens, and irrelevant detail can hurt reasoning. Efficient harnesses treat context as a budget:

  • Select, don’t dump: include files/snippets/results that matter to the current step; fetch more on demand.
  • Summarize or compact: replace older turns with faithful summaries when full detail is no longer needed, while keeping key decisions and outcomes.
  • Spill large outputs: store big logs/artifacts outside the prompt and pass short excerpts plus a path/handle.
  • Deduplicate: avoid repeating unchanged blobs every turn when the platform/harness can safely reference prior state.
  • Keep prefixes stable: consistent system/project/tool sections can improve cache reuse on providers that support prompt caching.
  • Prefer structured signals: diagnostics, diffs, and targeted query results are often more useful than raw walls of text.
  • Track what is “ground truth”: tool results and environment observations should be preserved carefully because later reasoning depends on them.

The goal is not the smallest possible prompt. The goal is the highest-signal prompt: enough evidence for a correct next step, without drowning the model in noise.

A useful mental model

Think of the harness as a compiler for context. Its input is the full evolving state of work: instructions, history, tools, and observations. Its output is the next message sequence the model will read. Tool calls and MCP integrations are part of how that state grows; summarization and selection are part of how it stays usable.

Once you see context this way, many agent behaviors make sense: why compaction exists, why tool output formatting matters, why MCP can feel “native,” and why two agents with the same model can behave very differently.

Sources

Weekly series

Read the posts in order:

  1. Adding a blog to my personal site (without starting over) - 2026-11-11
  2. Why PostHog is one of the first tools I add to a new product - 2026-11-18
  3. Anatomy of a coding harness: what makes AI coding agents work - 2026-11-11
  4. Context management in coding harnesses: what gets sent, and why it matters (this post) - 2026-11-18
  5. Goose under the hood: an MCP-native harness with a protocol at the center - 2026-11-11
  6. Pi under the hood: a minimal harness built around session trees and primitives - 2026-11-18
  7. OpenCode under the hood: one loop, many models, real environment feedback - 2026-11-11
  8. DeepSeek Harness under the hood: everything is a plugin, and the log is the contract - 2026-11-18

Previous: Anatomy of a coding harness: what makes AI coding agents work Next: Goose under the hood: an MCP-native harness with a protocol at the center