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.
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
A long-running codebase-analysis agent — one bloated context vs. the four strategies
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.
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.
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.
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