Combining the 6 Types: The 4 Stages (Write, Read, Compress, Isolate)
~13 min read
The book's 4 operational stages — Writing, Reading, Compressing, Isolating context — are the mechanism for actually assembling the 6 context types into a working agent pipeline.
The previous three subtopics catalogued WHAT the six context types are. This subtopic covers HOW this course says you actually operationalize them — because listing six types of information an agent might need doesn't, by itself, tell you how to get each type into the context window at the right moment. This course answers this with a second framework, presented just before the six types: context engineering can be broken down into 4 fundamental stages.
Writing context means saving it outside the context window to help an agent perform a task — you can write it to long-term memory (persists across sessions), short-term memory (persists within a session), or a state object. This is the stage that POPULATES your Memory context type (Type 4) in the first place — nothing becomes long-term memory unless some step explicitly writes it there.
Reading context means pulling it into the context window to help an agent perform a task — now this context can be pulled from a tool, memory, or a knowledge base (docs, vector DB). This is the stage that actually RETRIEVES your Knowledge (Type 3), Memory (Type 4), and Tool Results (Type 6) into the live context window at the moment they're needed — Writing stores it; Reading is what brings it back.
Compressing context means keeping only the tokens needed for a task. The retrieved context may contain duplicate or redundant information (multi-turn tool calls), leading to extra tokens and increased cost — context summarization helps here. This stage is what keeps a growing conversation, full of accumulated Tool Results and retrieved Knowledge, from simply overflowing the context window as a session gets longer.
Isolating context involves splitting it up to help an agent perform a task — popular ways to do so are using multiple agents (or sub-agents), each with its own context, using a sandbox environment for code storage and execution, and using a state object. This connects directly to the multi-agent-orchestration-patterns topic elsewhere in this curriculum: splitting context across sub-agents is itself a context-engineering technique, not just an orchestration pattern.
This course's own summary ties both frameworks together: 'when you are building a context engineering workflow, you are engineering a context pipeline so that the LLM gets to see the right information, in the right format, at the right time.' The 6 types are the WHAT (which categories of information matter); the 4 stages are the HOW (the concrete operations — write, read, compress, isolate — that move information between storage and the live context window). The companion context-engineering-workflow-build topic shows this entire combination applied in one real, working multi-agent system.
💻 Code example
# Modeling the 4 stages (Write/Read/Compress/Isolate) as the operations
# that move the 6 context types between storage and the live context
# window -- the HOW that operationalizes the previous 3 subtopics' WHAT.
class ContextPipeline:
def __init__(self, max_tokens: int = 200):
self.long_term_memory: dict = {}
self.max_tokens = max_tokens
def write(self, key: str, value: str, store: str = "long_term"):
"""Stage 1: WRITE context outside the live window (Memory type)."""
if store == "long_term":
self.long_term_memory[key] = value
def read(self, keys: list[str]) -> list[str]:
"""Stage 2: READ context back into the window (Knowledge/Memory/Tool Results)."""
return [self.long_term_memory[k] for k in keys if k in self.long_term_memory]
def compress(self, chunks: list[str]) -> list[str]:
"""Stage 3: COMPRESS -- keep only what fits, dropping duplicates first."""
unique_chunks = list(dict.fromkeys(chunks)) # drop exact duplicates
result, used = [], 0
for chunk in unique_chunks:
if used + len(chunk) > self.max_tokens:
break
result.append(chunk)
used += len(chunk)
return result
def isolate(self, task: str, sub_agent_contexts: dict) -> str:
"""Stage 4: ISOLATE -- route to a sub-agent with its OWN narrow context."""
return sub_agent_contexts.get(task, "general_agent")
pipeline = ContextPipeline(max_tokens=60)
pipeline.write("refund_policy", "Refunds process within 5 business days.")
pipeline.write("user_pref", "User prefers email over SMS.")
retrieved = pipeline.read(["refund_policy", "user_pref", "refund_policy"]) # dup on purpose
compressed = pipeline.compress(retrieved)
assigned_agent = pipeline.isolate("billing", {"billing": "billing_sub_agent"})
print("Read (with duplicate):", retrieved)
print("Compressed (deduped, token-limited):", compressed)
print("Isolated to:", assigned_agent)
💬 Deep Dive with AI
Key points
- •The 6 context types are the WHAT (categories of information an agent needs); the 4 stages are the HOW (operations that move information into and out of the live context window)
- •Writing saves context outside the window (to long-term memory, short-term memory, or a state object) — this is what populates the Memory context type
- •Reading pulls context INTO the window from a tool, memory, or knowledge base — this is what surfaces Knowledge, Memory, and Tool Results when needed
- •Compressing keeps only the tokens needed for the task, using summarization to cut duplicate/redundant retrieved context (like repeated multi-turn tool call info) and control cost
- •Isolating splits context across multiple sub-agents, a sandbox, or a state object — directly connecting context engineering to multi-agent orchestration patterns