> ## Documentation Index
> Fetch the complete documentation index at: https://labs.prompthon.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Context Engineering

<div className="not-prose my-4 rounded-md border border-gray-200 bg-gray-50 p-2 text-sm dark:border-gray-800 dark:bg-gray-900/40">
  <div className="mb-2 px-1 text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Need help?</div>

  <div className="flex flex-wrap gap-2">
    <a className="inline-flex items-center gap-2 rounded-md border border-gray-200 bg-white px-3 py-1.5 font-medium text-gray-700 no-underline shadow-sm hover:border-gray-300 hover:bg-gray-50 dark:border-gray-800 dark:bg-gray-950/60 dark:text-gray-200 dark:hover:bg-gray-900" href="https://discord.gg/sDE2HhGTg4" target="_blank" rel="noreferrer">
      <Icon icon="discord" iconType="brands" size={14} />

      <span>Ask in Discord</span>
    </a>

    <a className="inline-flex items-center gap-2 rounded-md border border-gray-200 bg-white px-3 py-1.5 font-medium text-gray-700 no-underline shadow-sm hover:border-gray-300 hover:bg-gray-50 dark:border-gray-800 dark:bg-gray-950/60 dark:text-gray-200 dark:hover:bg-gray-900" href="https://github.com/Prompthon-IO/agent-systems-handbook/issues/new/choose" target="_blank" rel="noreferrer">
      <Icon icon="github" iconType="brands" size={14} />

      <span>Open a GitHub issue</span>
    </a>

    <a className="inline-flex items-center gap-2 rounded-md border border-gray-200 bg-white px-3 py-1.5 font-medium text-gray-700 no-underline shadow-sm hover:border-gray-300 hover:bg-gray-50 dark:border-gray-800 dark:bg-gray-950/60 dark:text-gray-200 dark:hover:bg-gray-900" href="https://github.com/Prompthon-IO/agent-systems-handbook/blob/main/SUPPORT.md" target="_blank" rel="noreferrer">
      <Icon icon="life-ring" size={14} />

      <span>Support guide</span>
    </a>
  </div>
</div>

## Summary

Context engineering is the operational discipline of deciding what the model
sees before each step. It is broader than prompt writing because it includes
instructions, tools, retrieved evidence, notes, active state, and the rules
that keep all of that inside a limited attention budget.

## Why It Matters

Agents fail less often when their context is intentionally assembled. The
failure mode is usually not "the model is weak" but "the model saw the wrong
mix of instructions, history, evidence, and noise".

This matters most for long-horizon work:

* research that spans many search iterations
* development work across multiple files and decisions
* operations tasks with changing state
* multi-agent workflows where each actor should see only what it needs

## Mental Model

Context engineering can be reduced to four actions:

* `write`: persist state outside the immediate prompt
* `select`: choose the most relevant pieces for the next step
* `compress`: summarize or trim what no longer deserves full fidelity
* `isolate`: keep unrelated work, tools, or agents from polluting one another

The practical question is never "how big is the model window?" It is "what is
the minimum high-signal context that still lets this step succeed?"

That leads to four common risks:

* `context poisoning`: a wrong assumption or hallucination gets preserved and
  keeps steering later steps.
* `context distraction`: the prompt is crowded with history, so the model keeps
  attending to the past instead of solving the current problem.
* `context confusion`: extra but irrelevant material dilutes the task.
* `context clash`: two parts of the context disagree, and the model locks onto
  the wrong one.

## Architecture Diagram

```mermaid theme={null}
flowchart TD
  Inputs["Instructions, history, tools, notes, retrieval"] --> Write["Write state outside prompt"]
  Write --> Select["Select relevant signals"]
  Select --> Compress["Compress low-value detail"]
  Compress --> Isolate["Isolate roles, tasks, and environments"]
  Isolate --> Context["Context packet for next step"]
  Context --> Model["Model call"]
```

Context engineering is therefore a packaging system, not a single prompt.

## Tool Landscape

Effective context systems usually combine several surfaces:

* system instructions that stay stable over time
* structured notes or scratchpads for progress and blockers
* retrieval pipelines for external evidence
* lightweight file or environment exploration for just-in-time inspection
* compaction rules that convert long history into durable summaries
* subagent boundaries that prevent one exploration path from flooding every
  other path

The design target is not maximal completeness. It is clean state transfer.
When a system supports long tasks well, it usually means it can hand off only
the necessary state from one step to the next.

## Tradeoffs

* Richer context can improve recall, but it also increases distraction and
  cost.
* Aggressive compression saves tokens, but it risks losing key constraints or
  subtle evidence.
* Just-in-time retrieval keeps prompts lighter, but it adds tool latency and
  requires stronger execution heuristics.
* Subagents improve focus, but they introduce coordination overhead and make
  summarization quality more important.

Useful operating defaults:

* Keep durable constraints outside volatile chat history.
* Summarize old history before the model starts failing under load.
* Treat notes as first-class state, not an afterthought.
* Use isolation whenever different tasks, roles, or environments would
  otherwise compete for the same window.

## Reading Extensions

* [Agent Memory And Retrieval](/patterns/agent-memory-and-retrieval)
* [Protocols And Interoperability](/systems/protocols-and-interoperability)
* [Systems Overview](/systems)

## Update Log

* 2026-04-21: Initial repo-native draft based on imported reference material and lab rewrite rules.
