CloudCodeTree LogoCloudCodeTree
AI NewsTutorialsAbout
CloudCodeTree Logo
CloudCodeTree
  • AI News
  • Tutorials
  • About
← Back to AI News
Context Engineering: Three Strategies for Managing What Goes Into an Agent's Context Window

Context Engineering: Three Strategies for Managing What Goes Into an Agent's Context Window

Chris Harper

4 min read

Jul 6, 2026 · 20:03 UTC

AI
Tutorial
Agents
Best Practices
MCP

TL;DR: Context engineering is what prompt engineering grows into when you build agents — managing a finite window across dozens of turns with memory retrieval, rolling compaction, and per-turn tool clearing.

What you'll be able to do after this:

  • Identify which of three context strategies (memory, compaction, tool clearing) applies to your agent's specific problem
  • Write a just-in-time retrieval step that injects only relevant context at each turn instead of accumulating everything
  • Apply compaction and tool clearing to keep a long agent session from degrading as the window fills

Why context engineering exists

Prompt engineering optimizes a single call: what do I put in the system prompt and user message to get the output I want? Context engineering is the harder problem that emerges once you're building agents: across 50 turns of tool calls, retrieved documents, and model responses, what tokens should actually be in the context window at any given moment?

Anthropic's engineering team defines it precisely: "the art and science of curating what will go into the limited context window from that constantly evolving universe of possible information." The curation happens on every turn, not once.

The three strategies address three distinct sources of context bloat.


Strategy 1: Memory (just-in-time retrieval)

Instead of accumulating everything in the messages array, store facts externally and retrieve only what's relevant to the current moment.

# Bad: append every observation to context
messages.append({"role": "assistant", "content": "User prefers bullet lists."})
messages.append({"role": "assistant", "content": "User's timezone is UTC+9."})
# ... 100 messages later, this is still taking up tokens

# Better: store observations in a vector store; retrieve on each turn
relevant_memories = memory_store.search(query=current_user_message, top_k=5)
system_prompt = base_system + "\n\n## Relevant context:\n" + "\n".join(relevant_memories)

This is what Mem0, LangMem, and similar libraries do. The context window sees only the 5 most relevant memories for this turn, not every fact the agent has ever observed.

When to use it: User preferences, long-term facts, project-level context that grows unboundedly across sessions.


Strategy 2: Compaction (rolling summary)

When conversation history gets long, summarize it. The Claude API's compact_20260112 does this server-side in one parameter; for other providers, implement it yourself:

from anthropic import Anthropic

client = Anthropic()

def maybe_compact(messages, threshold=60000):
    """Summarize conversation history when it approaches the context limit."""
    token_estimate = sum(len(m["content"]) // 4 for m in messages)
    if token_estimate < threshold:
        return messages

    history_text = "\n".join(
        f"{m['role'].upper()}: {m['content']}" for m in messages[:-2]
    )
    summary_response = client.messages.create(
        model="claude-haiku-4-5-20251001",
        max_tokens=2048,
        system="Summarize this conversation. Preserve all code snippets, function names, file paths, and factual decisions verbatim. Compress reasoning and narrative.",
        messages=[{"role": "user", "content": history_text}],
    )
    summary = summary_response.content[0].text
    # Keep summary + last 2 turns
    return [
        {"role": "user", "content": f"[CONVERSATION SUMMARY]\n{summary}"},
        *messages[-2:],
    ]

The Claude Cookbook's Context engineering recipe has the production version with automatic trigger, quality-focused instructions, and the server-side compact_20260112 variant.

When to use it: Single-session agent runs that accumulate turn history. Aim to compact at 40–50% window usage — waiting until 80% means the summary is already degraded.


Strategy 3: Tool-result clearing

Tool outputs are often the biggest context killer. A web search returns 3,000 tokens; a file read another 2,000; a code execution result 1,500. By turn 20 your messages array is dominated by tool outputs that informed earlier decisions but add no value now.

# Use context editing to clear stale tool results
response = client.messages.create(
    model="claude-sonnet-5-20260630",
    max_tokens=8096,
    context_management={
        "edits": [
            {
                "type": "clear_tool_result",
                "message_id": stale_message_id,  # the assistant turn that used the tool
                "tool_use_id": stale_tool_use_id   # the specific tool call to clear
            }
        ]
    },
    messages=messages,
)

The decision model is simple: if a tool result informed a decision that's now reflected in a subsequent summary or code change, clear it. If the raw data might still be needed, keep it.

When to use it: Any agent that makes tool calls. Run a compaction + tool-clearing pass together every N turns.


The practical pattern

In production, all three strategies layer:

  1. Before each turn: retrieve relevant memories from your vector store → inject as system context
  2. Every 10–15 turns (or at 40% window): compact the conversation history
  3. Same pass: scan tool results older than K turns → clear ones that've been acted on

This is what "context engineering" means in practice: not a clever prompt, but a maintenance loop that keeps the context window a precise, high-signal slice of everything that's happened.

Sources: Effective context engineering for AI agents — Anthropic Engineering Blog · Context engineering: memory, compaction, and tool clearing — Claude Cookbook · Context editing — Claude Platform Docs