Context Engineering
Managing context windows effectively — avoiding poisoning, distraction, and context overload
What Is Context Engineering?
Context Engineering = the art of managing information in an LLM’s context window. The more complex the agent, the larger and harder to manage the context becomes.
Prompt Engineering is crafting one good prompt. Context Engineering is designing a system to manage all information an agent receives.
Types of Context
| Type | Description | Example |
|---|---|---|
| System | Fixed instructions | System prompt, role, rules |
| User | User input | Messages, files, preferences |
| Tool Results | Results from tool calls | API responses, DB queries |
| Memory | Past interaction info | Conversation history, user profile |
| Scratchpad | Working notes | Agent’s internal reasoning |
Context Management Strategies
1. Agent Scratchpad
Agent keeps “draft notes” to organize its thinking:
import { generateText, tool } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";
let scratchpad: string[] = [];
const addNote = tool({
description: "Save important notes to scratchpad for later reference",
parameters: z.object({
note: z.string(),
category: z.enum(["fact", "question", "decision", "todo"])
}),
execute: async ({ note, category }) => {
scratchpad.push(`[${category}] ${note}`);
return { saved: true, totalNotes: scratchpad.length };
}
});
const result = await generateText({
model: openai("gpt-4o"),
system: `You are a research agent. Use the scratchpad to organize information.
Note all important information before concluding.
Current scratchpad:
${scratchpad.join("\n")}`,
tools: { addNote, searchWeb },
maxSteps: 10,
prompt: "Research AI agent framework trends in 2025"
});
2. Context Summarization
When context gets too long, summarize to maintain focus:
async function summarizeContext(
messages: Message[],
maxTokens: number = 2000
): Promise<string> {
const tokenCount = estimateTokens(messages);
if (tokenCount < maxTokens) {
return messages.map(m => m.content).join("\n");
}
const oldMessages = messages.slice(0, -5);
const recentMessages = messages.slice(-5);
const { text: summary } = await generateText({
model: openai("gpt-4o-mini"),
prompt: `Summarize this conversation concisely, keeping important info:
${oldMessages.map(m => `${m.role}: ${m.content}`).join("\n")}`
});
return `[History summary]: ${summary}\n\n[Recent]:\n${recentMessages.map(m => m.content).join("\n")}`;
}
3. Multi-Agent Context Sharing
In multi-agent systems, each agent has its own context. Sharing must be selective:
interface SharedContext {
facts: string[];
decisions: string[];
constraints: string[];
}
async function passContext(
fromAgent: string,
toAgent: string,
fullContext: Message[]
): Promise<SharedContext> {
const { object } = await generateObject({
model: openai("gpt-4o-mini"),
schema: z.object({
facts: z.array(z.string()),
decisions: z.array(z.string()),
constraints: z.array(z.string())
}),
prompt: `Extract key information from ${fromAgent}'s context for ${toAgent}:
${fullContext.map(m => m.content).join("\n")}`
});
return object;
}
4. Tool Loadout Management
Too many tools = confused agent. Research shows < 30 tools is optimal:
async function selectTools(task: string) {
const { object } = await generateObject({
model: openai("gpt-4o-mini"),
schema: z.object({
selectedTools: z.array(z.string()).max(10)
}),
prompt: `From the tool list, select up to 10 most relevant tools for:
Task: ${task}
Available: ${Object.keys(allTools).join(", ")}`
});
return object.selectedTools.reduce((acc, name) => {
acc[name] = allTools[name];
return acc;
}, {});
}
Context Failures
1. Context Poisoning
Agent receives bad info → saves to memory → makes wrong decisions continuously.
Prevention: Validate before saving to memory.
2. Context Distraction
Too much irrelevant information → agent loses focus.
Solution: Periodic summarization + relevant context filtering.
3. Context Confusion
Too many tools or similar descriptions → agent picks wrong tool.
Solution: Tool loadout management + clear descriptions.
4. Context Clash
Contradictory information from multiple sources.
Solution: Conflict resolution + source ranking.
Summary
- Context Engineering > Prompt Engineering for complex agents
- Scratchpad helps agents organize thinking
- Summarization keeps context concise and focused
- Tool loadout < 30 tools for optimal performance
- Prevent 4 failures: poisoning, distraction, confusion, clash