Managing context as a bounded runtime resource

← Back to Blog & Articles

For evidence that placement in a long context affects retrieval performance, see https://arxiv.org/abs/2307.03172.

For evidence that placement in a long context affects retrieval performance, see https://arxiv.org/abs/2307.03172.

Context is the complete input presented to a model for one decision. It may contain system instructions, the current goal, state, conversation turns, retrieved evidence, memories, tool descriptions, policy-derived constraints, and a required output schema. Treating that collection as an incidental prompt string makes the system difficult to secure, reproduce, or evaluate. A production runtime treats it as a versioned artifact.

The context packet in the reference implementation is immutable after assembly. It records a packet identifier, run identifier, decision purpose, model and tokenizer identifiers, assembly-policy version, timestamp, ordered segments, estimated token counts, and a digest. Each segment records its semantic role, source, trust classification, sensitivity, freshness, and whether truncation is permitted. The rendered messages are only one projection of this richer structure.

Semantic role and authority are different properties. A retrieved runbook may be rendered near an instruction, but it remains untrusted evidence. A tool description may explain how to call a function, but it cannot grant permission to use that function. A prior assistant message may contain useful reasoning, but it is not proof that an external effect occurred. Preserving these distinctions before rendering allows policy and evaluation code to reason about the packet without parsing prose.

The assembler begins from the decision purpose. A diagnostic decision needs the incident goal, current service state, recent observations, relevant runbooks, and observation tools. An approval decision needs the proposed action, evidence, predicted impact, rollback plan, authority requested, and unresolved uncertainty. Reusing one enormous transcript for both decisions wastes tokens and can expose information to components that do not need it.

Context construction proceeds through six stages. Collection identifies candidate segments. Authorization removes material the run is not allowed to access. Normalization converts content into stable typed representations. Ranking estimates usefulness for the current decision. Packing selects segments within the budget. Rendering maps the selected packet into a provider-specific message format. Telemetry records the result without logging secrets or restricted content.

Provenance must survive every stage. A useful segment points to the original source, retrieval query or event, content version, collection time, and transformations applied. A summary points to the evidence it summarizes. A memory points to the event or approved assertion that created it. Without this chain, an operator cannot distinguish an authoritative record from a model-generated restatement.

Freshness is task-dependent. A year-old architectural decision may remain valid, while a service-health observation can become unsafe within seconds. Each segment therefore carries a validity interval or freshness policy. The assembler can reject expired observations, request refresh, or label stale evidence so that the planner must account for it explicitly.

Context assembly is also a security boundary. Retrieved text, tool output, and messages from other agents may contain imperative language. The runtime wraps them as data with origin and trust labels rather than splicing them into a privileged instruction channel. This does not guarantee that a model will ignore malicious text, but it preserves the control-plane distinction and enables independent defenses at proposal validation and policy enforcement.

The Model Context Protocol distinguishes prompts, resources, and tools and assigns different control relationships to those primitives. The reference runtime does not depend on MCP, but the distinction is useful: contextual resources and executable capabilities should not be collapsed into one undifferentiated string. An MCP adapter maps protocol objects into the runtime's own typed, policy-controlled representations.

Evaluation needs the packet, not merely the output. When an answer is wrong, engineers ask whether necessary evidence was absent, relevant evidence was ranked too low, stale material survived filtering, untrusted instructions influenced the plan, or the model ignored a sufficient packet. Retaining the packet identifier and segment manifest makes those questions measurable.

Data minimization improves both security and quality. Adding every accessible document increases exposure, cost, latency, and distraction. The assembler includes the minimum evidence expected to support the decision, then measures whether each segment changed outcomes. Segments with low marginal utility become candidates for removal or deferred retrieval.

the companion book laboratory constructs two packets for the same incident. The first naively includes an entire runbook repository containing a hostile instruction. The second uses authorization, trust labeling, ranking, freshness rules, and a strict token budget. Tests assert that restricted documents are excluded, expired observations are rejected, immutable instructions are retained, and every included segment has provenance.

Continue with the book

Explore the full treatment, worked examples, and exercises in Building Production-Grade AI Agents.


Companion article for Building Production-Grade AI Agents, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.

Scroll to Top