← All writing

How you structure context is how you structure behavior

Apr 2026·4 min read

Most agent failures in production aren't model failures. They're context failures. The model is reasoning well. It just doesn't have the right information, in the right form, at the right time.

Context engineering shapes the entire information environment the model operates in: system prompts, conversation history, retrieved content, tool outputs, memory, state. That environment is where quality is won or lost.

How you structure what goes in shapes model behavior as much as the prompt itself. Most teams treat these as implementation details. They're architectural decisions.

Design what your tools return

In agentic workflows, tool outputs are one of the fastest ways to fill the context window with noise. APIs return everything available: full records, nested objects, data you never asked for. If you include all of it in your tool output, by the time it responds, you've spent thousands of tokens on data that had nothing to do with the task.

The solution is designing tools to return what's actually useful, not what's available. For search results and fetched content, limit to the highest-ranked results. The model doesn't get smarter with more results, it gets noisier.

Similarly, if you only need certain fields from an API response, return those fields. Less is often more. If the model needs a name and description to reason about a result, there's no reason to also pass through the ID, slug, date, and every other field the API returns.

Lengthy context such as body text, embedded content, or sample data sometimes may need to be trimmed or summarized. Trimming works well for content like sample data or error messages, where additional length stops adding value past a certain point. For lengthy articles or documentation, summarization is the better approach. The summary should be specific to the current task rather than a generic brief. A generic summary risks losing what's actually relevant to the task and opens the door to confabulation.

Chroma's Context Rot study found something counterintuitive: topically-related but incorrect information degrades model performance more than unrelated content does. The semantic similarity makes it harder to filter out. If you're mixing real data with retrieved content or reference examples, be deliberate about what you include alongside it. A few wrong-but-plausible examples can do more damage than obvious noise.

Structure and format shape behavior

The same information, structured differently, gets meaningfully different outputs.

Models don't pay equal attention across a long context. Information in the middle tends to get the least. Stanford's Lost in the Middle study documented this, and while the effect is less pronounced in newer models, it still shows up in complex reasoning tasks. Don't bury what matters. Put what the model needs to act on near the top, or right before the point where it needs to use it.

Models have formatting preferences that vary by family and version. Claude responds well to XML tags for labeling and separating context: <documents> for reference material, <context> for background, <instructions> for what you want done. Other models vary. Some prefer markdown, others handle both equally well. Test what works for your model and standardize on it.

System prompts accumulate over time. A rule gets added, then another, then a special case, then a clarification for the clarification. Before long the system prompt has grown to thousands of tokens, and the instructions start working against each other. Review and clean it up on a regular basis, the same way you'd refactor code before it gets so tangled you can't change a line without wondering what else you broke. Anthropic's context engineering guide has specific recommendations for system prompt structure worth reading.

If you're using prompt caching, content order is a cost decision, not just a semantics one. It's applied from the top of the prefix downward, so what sits early matters a lot. Put the slow-changing part of the prompt first (general instructions and other fixed material) and keep dynamic context toward the end.

Retrieve, don't load

The instinct when the model might need something is to include it at the start. The better approach is to fetch it when you actually need it, usually through a tool the agent can call rather than a static dump in the system prompt.

In practice, that means retrieving what's relevant to the current task and injecting only those pieces: documentation, tickets, playbooks, policy text, user history, configuration. Each turn picks what enters the window rather than a static preload from session start.

The same applies to codebases. A whole repository can't fit in the prompt, so tools like Cursor index the repo into a vector store and retrieve semantically relevant code at query time, so only what's relevant to the current query enters the window. The tradeoff is setup complexity: you need an indexing and retrieval layer. For anything large enough that loading it whole would fill the window, it's worth it.

Managing context over time

For agents that run across many turns or sessions, conversation history turns into noise after a while. The model carries forward everything it's seen (tool results, intermediate steps, dead ends), and most of it stops being relevant to what you're doing now. For approaches to managing that accumulation (rolling summarization, agent handoffs, compaction), Your context window is full. Now what? covers those in depth.

External memory is another option. The agent writes structured notes to a store outside the context window as it works, and reads them back on demand. Decisions made, current state, key findings. What persists is curated, not accumulated. The window becomes a working surface for the current task, not a log of everything that happened.

Two layers cover most products. Working memory is session-scoped: intermediate reasoning, partial results, what you've established so far. Long-term memory spans sessions: user preferences, knowledge that should carry forward, past decisions that inform the next one. Most teams build the first; fewer design the second deliberately. An agent with long-term memory isn't just stateless-but-capable. It gets more useful over time.

The question worth asking

Context design is a set of decisions most teams make once, implicitly, and never revisit. The tools work, the prompts work, and nobody asks whether what's landing in the window is actually what should be there.

A few questions worth a periodic pass: what are your tools returning, and is any of it noise? Is the system prompt longer than it needs to be? Are you preloading what you could retrieve just in time? Is there context deep in history that no longer matters?

Most teams spend their energy on the prompt. The bigger leverage is everything around it. How you structure context is how you structure behavior.

← All writingYour context window is full. Now what?Next read→