Lesson 7

Context Engineering Strategies: Write, Select, Compress, Isolate

The four-move playbook for managing an agent's context

The Problem: An agent works great for three steps, then degrades: it forgets the goal, repeats tool calls, and gives worse answers as its context window fills with noise. What is going wrong, and how do you keep the context tight?

The Solution: Write, Select, Compress, Isolate

Context engineering is the practice of curating the smallest high-signal set of tokens a model sees on each turn. As an agent runs, its context window fills with tool calls, retrieved documents, and chat history. Past a point this causes context rot — the model attends less reliably to what matters (the "lost in the middle" effect) — or context poisoning, where a single wrong or malicious token steers the rest of the run. LangChain formalized four moves to fight this: Write, Select, Compress, and Isolate.

The four moves

Write persists state outside the window — scratchpads, files, or external memory — and recalls it on demand, so the prompt stays small. Select pulls in only what is relevant right now via retrieval, tool selection, or file selection. Compress summarizes or trims long history and verbose tool output to keep the remaining tokens dense. Isolate splits the work across separate sub-agent contexts so each one stays focused and one task's noise never pollutes another.

Choosing the right move

Each move fixes a different failure. If the prompt is bloated with state you keep re-sending, Write it out. If you are stuffing a whole corpus, Select the relevant slice. If history and tool dumps are eating the budget, Compress them. If one agent is juggling too many concerns, Isolate them into sub-agents. In real systems you combine all four: write state aside, select what you need, compress what is long, and isolate parallel work.

Think of it like managing a desk while you work:

  • 1. Write: Offload state to external memory (scratchpads, files) instead of stuffing it into every prompt
  • 2. Select: Retrieve only the currently-relevant context — the right docs, tools, and files
  • 3. Compress: Summarize long history and trim verbose tool output to keep tokens dense
  • 4. Isolate: Partition the task across sub-agents, each with its own separate context

In production agents you rarely use just one move — you layer all four together.

Where These Strategies Matter

  • Long-running agents: Multi-step agents that run for minutes or hours must offload state and trim history, or the window rots
  • Multi-agent systems: Isolate gives each sub-agent its own clean context so one task cannot pollute another
  • RAG applications: Select retrieves only the few relevant chunks instead of stuffing the whole knowledge base
  • Coding agents: Agents over large codebases use Select for files and Compress for tool output to stay focused

Fun Fact: The four-move framing (Write / Select / Compress / Isolate) was popularized by LangChain in 2025 — the same year the phrase "prompt engineering is dead, long live context engineering" went viral. The truth is calmer: prompting is now one slice of the bigger job of curating context.

Try It Yourself!

Use the interactive visualization below to toggle each strategy and watch how many tokens it frees from a filling context window.

Context Engineering Strategies

Toggle Write / Select / Compress / Isolate and watch how many tokens get freed.

Used: 17,200 / 16,000Overflow: +1,200
System prompt800
Agent state / scratchpad2,600
Whole knowledge base5,200
Full chat history3,400
Verbose tool output2,800
Parallel sub-task context2,400
⚠ Window overflowed — context rot
Freed: 0 tokens
💡 Takeaway: Each strategy fixes a different failure mode. Real agents layer all four to keep the window small and high-signal.

Frequently asked questions

What are the Write, Select, Compress, and Isolate strategies?

They are the four context engineering moves popularized by LangChain. Write persists state outside the context window (scratchpads, external memory) and recalls it on demand. Select retrieves only what is relevant right now (RAG, tool and file selection). Compress summarizes or trims history and verbose tool output to keep tokens dense. Isolate splits work across separate sub-agent contexts so each one stays focused. Together they let you curate the smallest high-signal set of tokens the model sees.

What is context rot and how do these strategies fix it?

Context rot is the degradation in reasoning that happens when a context window fills with low-signal or contradictory tokens — irrelevant history, stale tool output, or poisoned data. As the window grows, the model attends less reliably to what matters (the 'lost in the middle' effect) and is more easily distracted. Select keeps only relevant tokens in, Compress shrinks verbose ones, Write moves state out of the window, and Isolate prevents one task's noise from polluting another — each one raising the signal-to-noise ratio.

How is this different from prompt engineering or context engineering basics?

Prompt engineering is about wording a single instruction well. Context engineering is the broader discipline of curating everything the model sees. This lesson is the practical playbook layer: the concrete Write / Select / Compress / Isolate operations you apply to a running agent, especially long-running and multi-agent systems. It builds on the context engineering intro rather than repeating it — here you learn which move to reach for when a specific failure mode appears.

Try it yourself

Interactive demo of this technique

Technique Comparison
Demo Mode
Pre-recorded responses
TaskAdvancedAnalysis

A long-running codebase-analysis agent — one bloated context vs. the four strategies

Without technique
Without technique
Prompt
[Single prompt] Here is the ENTIRE codebase (180 files, ~95K tokens), the full 35-step agent history, and the complete raw output of every tool call. Task: find the cause of the memory leak in the worker and propose a fix.
Response

Memory leaks usually come from objects not being released. Check for global variables, unclosed connections, or event subscriptions. I recommend profiling the app and looking for growing data structures.

Tokens:124000/70
Time:9200ms
Quality:
With context-strategies
With technique
Prompt
[Write] The agent’s full plan and state are offloaded to a scratchpad; only the goal and open TODOs are pulled into the prompt. [Select] Of 180 files, the 3 relevant to the worker are retrieved (queue.ts, worker.ts, cache.ts). [Compress] The 35-step history is summarized into 250 tokens; raw tool output is trimmed to key fields. [Isolate] Memory profiling ran in a separate sub-agent that returned only its conclusion. Task: find the cause of the memory leak in the worker and propose a fix. Context: - Summary: RSS growth appears only under queue load. - Profiling sub-agent: an unbounded Map `cache` in cache.ts grows per job; entries are never evicted. - Relevant code: cache.ts lines 12-40.
Response

Cause: in cache.ts (lines 12-40), cache is an unbounded Map; every job adds an entry but nothing is ever removed. Under queue load it grows linearly — hence the RSS growth.

Fix: replace the Map with a size-bounded LRU cache (e.g. lru-cache, maxSize=1000) or add a TTL and periodically evict stale entries. Call cache.delete(jobId) once a job is no longer needed.

👁️Write moved the agent state out of the window — the prompt is no longer bloated
🧠Select kept only the 3 relevant files; Compress shrank history and tool output
🧠Isolate delegated profiling to a sub-agent that returned a precise conclusion, not noise
Result: a compact, high-signal window — the model pinpointed the exact line and fix
Tokens:1800/110
Time:1400ms
Quality:
Why this works

Dumping everything into one window causes context rot — a generic, expensive answer (124K tokens). Layering Write+Select+Compress+Isolate yields a tight context and a precise answer in ~1.8K tokens.

1 / 2

Lesson Quiz

1 of 4

1.An agent keeps re-sending its entire growing plan and scratchpad in every prompt, bloating the window. Which strategy directly fixes this?

Practice Challenges

Create a free account to solve challenges

3 AI-verified challenges for this lesson

This lesson is part of a structured LLM course.

My Learning Path