I. Invocation boundary
The input the model sees
For one invocation, model context is the complete model-visible input: application instructions, the current request, selected conversation turns, retrieved material, tool definitions and schemas, tool results, examples, and structural markers added during serialization. These components eventually become an ordered token sequence. Count the complete request explains that final representation; Reserve room for generation explains its capacity bound.
The context window bounds the sequence available to the invocation. It does not contain everything the application can access, and prompt caching does not enlarge it: a reused prefix still occupies capacity. Likewise, accepting a long input does not guarantee that the model will use every included fact reliably. Controlled long-context experiments have found position-sensitive behavior in several historical models and tasks, so capacity and effective use are different claims.
Context changes computation at inference time, not the model's stored parameters. Removing an instruction or example from the next call removes that invocation-time influence unless the application supplies it again. What a prompt changes develops this distinction; parameter updates belong to training and post-training, not context engineering.
The practical question is therefore not “How much can we fit?” but “What should this call be allowed and expected to use?” An application may possess a document that is relevant but unauthorized, a tool result that is permitted but stale, a correct record that contributes nothing to the present decision, or a large catalog whose definitions crowd out the task itself. Exclusion is part of the design, not evidence that the information was lost.
State beyond the prompt
Once a call is understood as a temporary projection, the fuller task record needs another home. Authoritative task state is the application-controlled account of goals, constraints, accepted decisions, artifacts, observations, pending operations, and completion status. It may live in a database, event log, repository, workflow record, or another durable store. The model can receive a selected view of that state, but its generated claims do not modify the authoritative record until application code validates and accepts them.
A conversation transcript is useful evidence about what was said, but it is not automatically a database of what is true. A user may correct an earlier statement; a tool result may report a transient observation; a model may assert that work completed when the external system still shows it running. Separate observations from state explains why observations need interpretation, while Preserve state at acceptance supplies the broader rule for accepting changes.
Persistent memory is different again. It retains selected information across interactions—such as a preference, prior event, or reusable fact—and makes that information available for later retrieval. It is a candidate source for a call, not the call itself and not necessarily the authoritative state of the present task. Memory formation, correction, expiry, and forgetting belong in Agent Memory.
Three different lifetimes
| Store | Primary question | Typical lifetime | Acceptance rule |
|---|---|---|---|
| Task state | What has been accepted about this task? | Until the task and its records expire | Application validation and state-transition rules |
| Persistent memory | What may be useful across interactions? | Across sessions, subject to retention policy | Memory write, correction, provenance, and expiry policy |
| Current model input | What should this invocation see now? | One invocation or a short tool loop | Context assembly policy |
II. Constructing a working set
From sources to a request
A context assembler constructs the complete input for a specific model call. It first identifies the decision the model must make—such as answering a question, choosing a tool, assessing an artifact, or proposing the next step. It then finds the information that decision depends on, excludes sources that are not permitted or valid, and converts the selected material into an ordered model-visible request.
Assembly stages
- Identify the decision — State the immediate task, required output, and facts or constraints that decision depends on.
- Enumerate eligible sources — Resolve current identities, permissions, source versions, and validity requirements before ranking content.
- Select a sufficient set — Choose material that covers required dependencies while controlling redundancy, disclosure, latency, and token cost.
- Transform deliberately — Extract, summarize, normalize, or reference material while retaining links to the source representation.
- Assign roles and provenance — Distinguish application instructions, user input, external evidence, memory, and tool observations.
- Order and serialize — Produce the exact provider-specific request, including tool schemas and structural markers.
- Count and record — Check the complete serialized input against the budget and record the policy and source versions used.
A system instruction is application-supplied model-visible instruction carried with provider-specific authority semantics. It is not a universal wire format. Similarly, a tool definition is context that describes an available operation; its executable implementation and authorization policy remain outside the model. Tool definitions shape available choices owns that interface in detail.
The final request is a derived view. A concise context record should identify the assembly-policy version, source identities and versions, material transformations, ordering, and token count. This does not make the request true or safe by itself. It makes its construction inspectable and allows a failure to be reproduced without pretending that the prompt was the authoritative source.
Roots of programmable context
The recent label combines several older engineering problems. Limited active capacity, relevance-ranked information access, explicit interaction state, invocation-time task specification, and external knowledge were developed along partly independent lines. The milestones below are useful antecedents, not a claim that one directly caused the next or that newer techniques replaced older ones.
Must-know developments
| Date | Development | Contribution to the context problem |
|---|---|---|
| 1968 | Denning's working-set model | Described a changing set of recently used information for allocating bounded paged memory; it supplies an analogy for active information, not an LLM policy. |
| 1980 | Hearsay-II blackboard account | Described independent knowledge sources coordinating through explicit shared hypotheses and a separate focus-of-control mechanism. |
| 1999–2000 | TRINDIKIT research | Represented dialogue state explicitly and used update and selection rules rather than treating the transcript as the complete working representation. |
| 2020 | GPT-3 few-shot evaluation | Demonstrated task specification through instructions and examples during forward passes without gradient updates. |
| 2020 | REALM and RAG | Connected model prediction or generation to dynamically selected passages in external indexes. |
| 2023 | MemGPT | Framed the context window as a limited working tier connected to external recall and archival stores through retrieval functions. |
These developments address different constraints. Working sets concern active capacity; retrieval concerns candidate selection; dialogue state and blackboards concern explicit state; prompting concerns task information supplied at invocation time; RAG concerns evidence-conditioned generation; tiered-memory systems concern reacquisition after information leaves the active window. Modern context engineering must combine these concerns because an agent request may contain instructions, evidence, state projections, tool choices, and untrusted material at once.
Larger windows reduce some pressure but do not decide permission, truth, freshness, provenance, or which dependencies a decision requires. Nor do they guarantee stable use of information across positions and tasks. Context engineering remains an application concern because those questions depend on the application, not only on the model architecture.
Select for the decision
Selection begins with eligibility, not relevance. A record is eligible only if current policy permits its use for this actor, purpose, and task. Among eligible candidates, the assembler asks whether a set is sufficient for the current decision. A useful passage can still be excluded because it is stale, redundant, too sensitive, or unnecessary; a highly ranked set can still be unusable because it omits a required connection.
Two retrieval failures need different diagnoses. Low precision includes irrelevant material that can distract generation. Low recall omits evidence needed to answer. Raising top-k may help recall while worsening precision, disclosure, and budget use; nearest-neighbor rank also returns candidates when a corpus has no valid answer. Search, indexing, candidate generation, and reranking belong in Search and Retrieval. Here the concern is which candidates become model-visible. To answer whether the current build passed, a result must be associated with the commit being checked.
A passing run needs a commit association
ExampleRelevant records are insufficient when the association required by the question is missing.
Read the diagram as text
- Missing association: c7. Candidate commit c7. The available records do not establish whether run r4 checks this commit.
- Missing association: r4. Run r4 is completed with conclusion success. Its commit association is absent from the selected context.
- Association present: c7. The same candidate commit c7.
- Association present: r4. The same run r4, status completed and conclusion success. Its authoritative head_sha is now included and matches c7.
- Association present: r4 → Association present: c7: head_sha = c7.
Candidate dispositions
| Candidate | Eligibility | Task contribution | Validity and trust | Disposition |
|---|---|---|---|---|
| Current project requirement | Permitted | Supplies a required constraint | Current, authoritative source | Include with source version |
| Another tenant's similar incident | Not permitted | Potentially relevant | Authority fails | Exclude before ranking |
| Old build result | Permitted | Could answer status question | Superseded by a newer run | Exclude as current evidence; retain as history |
| Repeated tool dump | Permitted | Duplicates facts already retained | Large and low marginal value | Replace with a reference or focused excerpt |
| One fact without its connecting premise | Permitted | Useful but insufficient | Current and trusted | Retrieve the missing dependency or abstain |
Record meaningful omissions. “Excluded because unauthorized,” “superseded by version 12,” and “not loaded because the current decision does not require it” imply different uncertainty and recovery paths. Omission records also make evaluation possible: a failed answer can be traced to absent evidence rather than vaguely blamed on the model.
Reveal detail on demand
Progressive disclosure exposes enough metadata to discover a resource before loading its full contents. A compact catalog entry might carry a name, purpose, scope, source, and retrieval reference. If the task makes the resource relevant, the assembler can load a focused excerpt; only a later need justifies the full artifact. This is useful for large documentation sets, skill libraries, tool catalogs, and long tool results.
An Agent Skill is one concrete staged format: discovery metadata is available first, instructions load when the skill is activated, and referenced resources load separately as needed. The general mechanism is broader than skills. File trees, indexes, manifests, graph views, and deferred tool definitions all separate discoverability from immediate inclusion.
Deferral creates a new dependency: the path back to detail must work. A preview can omit the answer, an entry can expire, a vague description can prevent discovery, and repeated reads can add latency. A retained reference is not recoverability unless storage still contains the source, permissions still allow access, and the agent has a functioning retrieval operation. Controlled research also suggests that gains depend on corpus size, metadata design, task, and the harness's existing navigation behavior.
Follow a reference to the needed detail
ExampleA discovery entry supports selective retrieval; it does not require eventual loading of the full artifact.
Read the diagram as text
- Input: catalog entry. Deployment runbook R1, version v3. Purpose: deployment and rollback guidance. Reference: R1@v3. Current question: what rollback procedure applies?
- Retrieval operation. Resolve R1@v3 and the requested section. Require retained source content, current access permission, and a functioning retrieval operation.
- External: runbook R1@v3. The full source remains outside active input. A focused read selects its rollback section; a broader read is optional.
- Input: rollback excerpt. Only the requested section of R1@v3 enters the model input.
- Input: full runbook. Optional alternative response from R1@v3 when the task requires broader detail; not a mandatory next stage.
- Detail unavailable. A missing source, denied access, or unavailable retrieval operation yields an explicit unavailable result, not an empty successful read.
- Input: catalog entry → Retrieval operation: request through R1@v3 reference.
- Retrieval operation → External: runbook R1@v3: read when access conditions pass.
- External: runbook R1@v3 → Input: rollback excerpt: return requested rollback section.
- External: runbook R1@v3 → Input: full runbook: return full content only if needed.
- Retrieval operation → Detail unavailable: report failed access condition.
III. Representation and limits
Order and label the material
After selection, the model receives an ordered serialization rather than an unordered set. Application instructions, the current task, demonstrations, retrieved sources, tool observations, and output expectations occupy positions and roles. Changing their order or labels changes the input, even when their human-readable facts remain the same. Order and format are interventions covers the broader prompt-sensitivity evidence.
Role metadata describes authority assigned by the application. A retrieved document cannot promote itself by claiming to be a system message. Conversation history and retrieved evidence should remain distinguishable, and derived statements should retain their links to source versions. Time labels matter when a current request includes historical records or observations collected at different moments.
Controlled representation changes
| Keep fixed | Change | Question answered |
|---|---|---|
| Sources, wording, model, decoding | Position of the answer-bearing source | Does location affect use on this workload? |
| Sources, wording, order | Role and source labels | Does explicit role separation change interpretation? |
| Facts and authority | Prose versus structured fields | Does representation improve correct extraction or omit nuance? |
| Evidence and request | Adjacency of claim and provenance | Does the model preserve source attribution more reliably? |
There is no universal best template. Stable conventions make changes reviewable, but their behavior still needs workload-specific tests. Repeating every instruction may improve salience while consuming budget and adding conflicts; placing all evidence first may help one task and obscure another. Preserve complete serialized inputs so an ordering experiment can actually be reproduced.
Allocate the request budget
A context budget is an application policy over the complete counted request. Instructions, the current task, evidence, history, tools, structural tokens, and generated output share capacity. Let be the complete serialized input, the generation allowance reserved by the application, and the supported context capacity. The basic guard is:
The symbols describe capacity, not quality. Count the complete provider-specific serialization with the intended model. Hosted counting endpoints may provide exact counts or estimates under different contracts, and provider-added material can affect the result. Reserve output before spending the remainder on input; reasoning models may require additional provider-specific accounting.
A practical allocation policy
- Protect — Guarantee space for indispensable instructions, the current decision, required identifiers, and output contract.
- Reserve — Set aside generation capacity and any provider-specific reasoning allowance before adding optional context.
- Allocate flexibly — Let evidence, history, examples, and tool definitions compete within explicit priorities.
- Reduce deliberately — On overflow, remove duplication, offload bulky observations, narrow candidates, or compact according to declared invariants.
- Recount — Serialize the revised request again; tokenization of joined text is not safely additive.
A fitting request can still perform poorly because required relationships are scattered, distractors remain, or useful material occupies a position the model handles weakly. Conversely, a shorter request may incur extra retrieval latency or omit a dependency. When longer context helps covers architectural behavior; the application must evaluate the allocation on its own task distribution.
IV. Validity and authority
Refresh, supersede, remove
Freshness is not one age threshold. Observed-at time records when the application obtained a value. Effective time records when the source says the value applies. A version identifies a particular representation; an expiry or validation rule says when reuse requires another check. Authority identifies which source may decide the fact. These dimensions can disagree.
Consider a build status cached at 10:00. A cache hit at 10:05 proves reuse of the stored representation, not that the build is still running. A newer observation from an unofficial mirror may also be less authoritative than an older record from the build system. The assembler needs a source-specific rule: refresh on read, conditional revalidation, a time-to-live, event-driven invalidation, or rejection when current validity cannot be established.
Staleness can corrupt downstream decisions even when the original write succeeds. In one reported multi-agent deployment, a new credit score reached PostgreSQL but a shared cache was not invalidated; a later agent read the older cached record and made its decision from stale evidence. The lesson is architectural: source success, cache coherence, and downstream decision correctness are separate claims.
A successful write can leave a stale request
ExampleDatabase update success and downstream freshness depend on different paths.
Read the diagram as text
- Upstream writer. Successfully updates the authoritative record. Cache invalidation does not complete.
- Database: version B. The authoritative record has changed from A to B. This successful write does not establish cache coherence.
- Shared cache: version A. Retains the older representation because invalidation did not complete. No completed invalidation path is shown.
- Assembler reads A. The downstream read occurs after the database update but returns the cached representation A.
- Next input: version A. Source version: A. Observed-at: a later read. The newer observation time does not change the source version or establish current validity.
- Upstream writer → Database: version B: successful write: A → B.
- Shared cache: version A → Assembler reads A: read returns A.
- Assembler reads A → Next input: version A: serialize A.
Different lifecycle operations
| Operation | What it changes | What it does not establish |
|---|---|---|
| Hide from active context | The next model-visible projection | Deletion from durable storage or derivatives |
| Supersede | Which version is current for a purpose | Erasure of historical records |
| Invalidate | Whether a record may support a current claim | Physical deletion |
| Delete source record | One authoritative or stored representation | Removal from summaries, indexes, caches, traces, or backups |
| Rebuild derivative | A summary, index, or materialized view | Correction of every independently retained copy |
When required freshness or current permission cannot be established, fail closed for the affected claim or operation. Historical information may remain useful as history, but it must not silently authorize a current action. Maintain fitness through change covers general source change, while Enforce current authority covers permission decisions.
Keep untrusted content in role
Retrieved documents, webpages, attachments, memory records, and tool results can be relevant without being trusted instructions. Prompt injection occurs when attacker-controlled content causes an instruction-following model to cross the application's intended task or instruction boundary. An indirect injection arrives through material the application meant to treat as data. When content behaves like instruction develops the attack mechanism.
Assembly should retain source identity, derivation, trust classification, current permission, and data role. Delimiters, role labels, encodings, and classifiers may improve behavior, but they are instructions to a fallible model rather than a hard isolation boundary. A Base64 spotlighting counterexample illustrates the general limitation: changing representation does not by itself prove that content cannot influence instruction following.
Any model-proposed external effect must cross a separate enforcement boundary. Trusted application code binds the authenticated actor to the requested operation, target resource, current state, and delegated scope. A valid tool call, persuasive explanation, or provenance label cannot grant permission. Controls belong on attack paths and Authorization belongs at the protected operation cover those controls.
Model guidance and effect authorization
Labeling untrusted content and authorizing an external effect are separate boundaries.
Read the diagram as text
- Trusted instructions. Application-authored task and policy guidance.
- Untrusted source content. Relevant data that may contain imperative language.
- Labeled model input. Preserves roles, source identity, and trust classification.
- Proposed action. Model output is a request, not an effect.
- Authorization gate. Checks actor, operation, resource, arguments, state, and delegated scope.
- Denied outcome. No protected operation is dispatched.
- Protected resource. Receives only an authorized, validated operation.
- Trusted instructions → Labeled model input: instruction role.
- Untrusted source content → Labeled model input: data role plus provenance.
- Labeled model input → Proposed action: model computation.
- Proposed action → Authorization gate: untrusted proposal.
- Authorization gate → Protected resource: authorized and valid.
- Authorization gate → Denied outcome: denied or invalid.
Apply least disclosure before the call as well as least privilege at execution. If the immediate task does not require a secret, private record, internal identifier, or destructive capability, do not expose it merely because the application can. Reducing unnecessary context narrows both distraction and the material available for exfiltration. Minimize without losing the task supplies the broader governance rule.
V. Continuity and evidence
Compact, but preserve recovery
Compaction replaces a larger active history with a smaller representation. Summarization is one lossy compaction method, but it is not the only one. An application can remove duplicated structure, mask old tool observations, retain selected extracts, write an abstractive summary, build hierarchical summaries, or extract explicit task state. Each method preserves different information.
Methods and losses
| Method | Retains directly | Characteristic risk |
|---|---|---|
| Structural deduplication | One copy of repeated material | Removing a repetition that supplied useful salience |
| Observation masking | Actions and recent turns, with placeholders for old outputs | A later question needs a masked value |
| Extractive retention | Selected original passages | Selection omits a dependency |
| Abstractive summary | A shorter synthesized account | Changed entities, modality, time, causality, or exceptions |
| Task-state extraction | Named decisions, obligations, artifacts, and statuses | The extraction schema omitted an unforeseen detail |
| External offload with references | A preview plus a route back to full data | Storage expiry, missing retrieval access, or a misleading preview |
Define invariants before compacting. For long tasks these often include the current goal, accepted decisions and their rationale, unresolved obligations, safety constraints, pending operation identities, artifact references, source versions, and known failures. Preserve authoritative raw records or recoverable artifacts outside the compacted prompt. A fluent summary cannot certify that every later-relevant detail survived.
Compaction can also remove governing instructions or collapse distinctions between current and historical state. Some implementations therefore pause after compaction so the application can reinsert selected instruction-oriented or recent material. Others reset the active context and reconstruct it from durable handoffs. Neither approach is universally superior: both depend on the quality, provenance, and completeness of what survives outside the window.
Reconstruct the next call
Long-running work survives when continuity flows through durable records rather than hidden model persistence. Useful records include task identity, accepted state version, current plan, artifacts, source versions, unresolved questions, pending operation identities, and verification results. Each call receives a fresh projection sufficient for its next decision.
The model returns observations and proposed changes. Application code validates them against the current state before acceptance. Before dispatching an external effect, it assigns a stable operation ID and atomically compares the expected state version while recording the operation as pending. This durable reservation closes the gap in which another worker could advance the task after a version check but before dispatch. After reconciling the effect by operation ID, the application atomically accepts the resulting transition only if the pending state still has the expected version; otherwise it preserves the pending record for further reconciliation and rebuilds context from current state.
An interruption around an external mutation creates an additional problem: the effect may be unknown. Cancellation does not necessarily roll back server work, and a missing response does not prove failure. Preserve a logical operation identifier, query authoritative status, and retry only under an idempotent contract. Represent uncertain external outcomes explains this boundary.
A conceptual acceptance boundary
Illustrative pseudocode
Python-like pseudocodeRuntime scheduling, checkpoint implementation, cancellation propagation, and worker recovery belong in Agent Runtimes and Harness Engineering. Context engineering owns the reconstructable projection: which durable facts and references the next invocation must receive after an interruption, compaction, handoff, or model replacement.
Test the context policy
Treat the assembler as versioned software. A context manifest is a useful application contract even though no provider-neutral standard defines one. It can record the policy version, current decision, source identities and versions, eligibility results, selected and omitted components, transformations, ordering, token counts, trust labels, and a privacy-controlled reference to the final serialization.
Test deterministic assembly separately from model behavior. Unit and snapshot checks can establish that unauthorized records are absent, required components precede serialization, source versions are recorded, overflow follows declared priorities, and compacted records retain named invariants. These checks do not establish that a model will use the resulting context correctly.
Claim-to-check map
| Policy claim | Deterministic check | Matched behavioral comparison | Activation gate |
|---|---|---|---|
| Eligibility | Forbidden source never enters serialization | Permitted versus denied-source fixtures | Confirm the identity and permission path ran |
| Sufficiency | Required source dependencies are present | Complete versus one-fact-missing context | Confirm the missing dependency is actually absent |
| Ordering | Same items and bytes except declared position or role change | Compare outputs on matched tasks | Hash or diff both serializations |
| Budget | Final count plus reserve stays within capacity | Compare controlled allocations at equal total size | Confirm truncation or allocation policy activated |
| Freshness | Required source version passes validation | Current versus stale record | Confirm the stale fixture was selected in the treatment |
| Compaction | Named invariants and references survive | Full history versus compacted history | Confirm compaction crossed its trigger |
| Untrusted input | External content remains data and protected operations require authorization | Clean versus injected source pairs | Confirm the attack content reached the model and the effect boundary was exercised |
| Continuity | Restart loads latest accepted state and rejects stale versions | Uninterrupted versus interrupted execution | Confirm a real restart or context replacement occurred |
Activation gates matter. A short conversation can score perfectly in a compaction evaluation simply because it never triggered compaction. One reported context-policy experiment checked trigger activation separately and found that single-turn cases often failed to exercise the strategies under study. Similarly, a freshness test is uninformative if the stale cache was bypassed, and a progressive-disclosure test says little if the full resource was already loaded.
For behavioral comparisons, keep the model, decoding settings, tools, task set, and grader fixed while varying the named context policy. Include an untouched-history baseline before assuming that compression helps. Use development tasks to choose the policy and held-out tasks for final assessment; repeated trials should reset memory when they are meant to be independent. Compare changes on matched work develops the comparison design.
Telemetry should answer the operational question without collecting every prompt by default. Useful fields include input and output tokens, cached tokens, latency, tool calls, summarization events, policy version, source versions, and activation status. Full instructions, inputs, and outputs may be sensitive and large; Record meaning-changing boundaries and Minimize without losing the task cover controlled capture and minimization.
Open questions
How can a context manifest become interoperable without pretending that every provider exposes the same message roles, token accounting, or model-visible request boundary? A useful standard would preserve source identity, versions, eligibility, transformations, trust, ordering, and final serialization while allowing provider-specific fields.
How should an assembler optimize several constraints without collapsing them into an unjustified score? Relevance, dependency coverage, authorization, freshness, disclosure risk, latency, and token cost can conflict. Progress would require controlled, workload-specific policies that report each constraint and its failure modes separately.
How much semantic and provenance loss accumulates across repeated compaction cycles? Existing work establishes that summaries can be lossy and variable, but a strong answer needs repeated-cycle experiments against an uncompacted authoritative record, including temporal relations, exceptions, permissions, and source links.
How should systems measure context quality directly rather than infer it only from downstream task success? Useful progress would distinguish whether required information was present, whether its roles and versions were correct, and whether the model used it, without treating fluent output as proof.
How can deletion and changed permissions propagate through source records, indexes, summaries, memories, caches, traces, and backups? Progress requires representation-specific ownership, durable tombstones or invalidation records, rebuild procedures, and evidence that each retained copy honored the change.
How should context policies adapt when models change without turning every model-specific workaround into permanent harness complexity? Progress would pair replaceable policy components with model-version-specific evaluation, then remove resets, ordering tricks, or compaction rules when controlled evidence shows they no longer help.








































































































































































































































































































































































































































































































































































































































































































