Context Engineering and Agent Reliability

Many agent failures begin before the model generates a token.

When an agent fails, the model gets blamed. Often the cause is upstream, in the information environment somebody assembled around it.

Context engineering is the design and runtime management of what an agent is allowed to know, trust, remember, and act on. It is the Q dimension of readiness, and for most teams it is the fastest place to make an agent measurably better, because it can be changed this week without retraining anything.

What actually arrives in the context#

On any given turn, the model sees a single block of text assembled from very different sources.

HOW CONTEXT IS ASSEMBLEDSystem instructionstrustedPoliciestrustedUser interactionuntrusted intentRetrieved documentsuntrusted evidenceTool outputsobservation + provenanceMemoryprior state + ageEnvironmental stateobservationOther-agent messagesuntrusted evidenceASSEMBLYtrust rules applied hereMODELthis turnGoverning the template in your repository is not the same as governing this.
Figure 7.1Context is assembled at run time from sources of different trustworthiness.

Those sources deserve different standing, and naming that standing explicitly is the whole discipline.

To the model, a sentence in your system prompt and a sentence inside a retrieved PDF are both simply text in the window. Whatever difference exists between them has to be created and enforced by the layer that assembles the context.

Once those categories are explicit, useful rules become available: instructions outrank evidence, untrusted content cannot introduce policy, stale memory can be challenged, and high-impact facts require provenance. The injected waiver that later defeats the payment agent in Chapter 16 disappears the moment the attachment is labeled as evidence rather than merged into the instruction channel.

Seven things worth checking before an agent runs#

We assess context before the agent is invoked at all, reading the system prompt, the tool schemas, and whether a knowledge corpus was supplied.

CriterionWhat it checksTypical failure
Role clarityRole, scope, goals and success criteria are explicitGoal drift
Guardrail coverageRefusals, escalation and restricted actions are specifiedUnsafe compliance
Instruction consistencyDirectives are coherent and ordered by precedenceRule conflict
Tool schema qualityNames, arguments, side effects and when-to-call are clearTool misuse
Grounding sufficiencyThere is enough reliable evidence for the claims it must makeHallucination
Injection hardeningTrusted instruction is separated from untrusted contentPrompt injection
Token efficiencyThe context spends its budget on reliabilityContext bloat

Seven criteria are assessed; five contribute to the score.

Instruction consistency and token efficiency are reported and deliberately excluded, because both improve as the artifact shrinks. An almost-empty prompt has nothing to contradict and no boilerplate to trim. On a deliberately thin 450-character prompt those two criteria scored 90% and 80%, which lifted its overall context score above a substantially better 1,033-character prompt and inverted the ranking. A criterion that rises as the artifact empties cannot serve as evidence of quality, so we demoted it rather than defending it.

What we found when we changed only the context#

Context is scored independently of behavior on purpose. Derived from behavioral results, it could not predict them; kept independent, it becomes a signal you can read before a scenario exposes a weakness.

To isolate the effect, we held the agent model fixed and changed only the information environment around it, across three conditions and three regulated domains. C1 Poor has a vague role, weak tool guidance, limited grounding, and little guardrail coverage. C2 Structured adds a clear role and scope, typed schemas, and domain grounding. C3 Hardened adds explicit refusal conditions, escalation thresholds, injection separation, and confirmation requirements.

ONE MODEL, THREE CONTEXTS010Context quality4.48.18.7Overall behavior3.155.495.16Safety3.154.954.8Hallucination res.3.215.615.4Tool use3.466.255.82C1 PoorC2 StructuredC3 HardenedScores 0–10. Poor context costs more than a model upgrade usually buys. Source:arXiv:2607.14275, Tables 4 and 5.
Figure 7.2Behavior across the context ladder, model held fixed. Source: arXiv:2607.14275, Tables 4 and 5.

The result is practical. Moving from poor to structured context nearly doubled overall behavior and cut critical failures from an average of 4.11 per run to 1.33, with no change to the model. Hardening raised context quality further, from 8.08 to 8.68, while behavioral scores plateaued — by that point the remaining failures were no longer context failures.

The criteria also behave as designed, each predicting the outcome it was built to predict.

CONTEXT CRITERIA AND WHAT THEY PREDICTCONTEXT CRITERIONBEHAVIORAL OUTCOMEPEARSON RGrounding sufficiencyHallucination resistance0.63Guardrail coverageManipulation resistance0.60Instruction consistencyInstruction following0.57Injection hardeningSafety0.48Tool schema qualityTool use0.47Guardrail coverageSafety0.44Role clarityTask success0.40Model held fixed; only the context varied. Source: arXiv:2607.14275, Table 6.
Figure 7.3Each context criterion against the behavior it predicts. Source: arXiv:2607.14275, Table 6.

Grounding sufficiency predicts hallucination resistance. Guardrail coverage predicts manipulation resistance. Instruction consistency predicts instruction following. Tool schema quality predicts tool use. That correspondence is what makes context assessment a governance instrument rather than a style guide: a weak criterion tells you which behavior to expect to fail, before it fails.

Context is not static, and memory is a write path#

Context changes with every turn. A tool result becomes new evidence. A retrieved page introduces untrusted text. A summary replaces raw history. A sub-agent contributes a conclusion. You can begin a session with a carefully designed prompt and end it with a degraded context, which makes this a runtime discipline rather than a document review.

Memory needs its own rules for the same reason. Decide what may be written, by whom, with what provenance, and for how long, and define how a wrong entry gets corrected. Any constraint that affects safety, a legal obligation, or an entitlement should survive the whole session — and when a safety-relevant constraint is lost, it is worth recording as a safety finding rather than a memory finding, because that is what the consequence will be.

Improving context is also the most available intervention. You probably cannot retrain the model, but you can almost always sharpen role boundaries, ground the sources, type the tool descriptions, separate trust, and specify escalation. Measuring it turns that work into a loop: assess, change, re-evaluate, compare.

A strong model operating in weak context can still produce an unreliable agent.

Apply This Chapter

Score the context before testing the behavior, then use the weak criteria as predictions. This is the fastest improvement available to most teams, because context can be changed this week without retraining anything.

Get these as working templates