All chapters
  1. Start hereA model knows only what it can see now
  2. Foundations · 1The context window: tokens, messages and order
  3. Foundations · 2Signals and authority: who is allowed to instruct the model
  4. Foundations · 3The context assembly pipeline: select, rank, deduplicate, pack
  5. Retrieval · 4Retrieval fundamentals: lexical search, embeddings, hybrid search, reranking
  6. Retrieval · 5RAG architecture: chunks, metadata, grounding, citations, freshness
  7. Budget · 6Context budgeting: truncation, dilution, token cost and caching
  8. Budget · 7Compression, summarization and compaction, with loss tests
  9. Agents · 8Coding-agent context: repository maps, instruction files, diffs and tool outputs
  10. Agents · 9Working context, durable memory and checkpoint state
  11. Agents · 10Dynamic context: just-in-time retrieval, selective tool disclosure, delegation
  12. Agents · 11Multi-agent context isolation and scoped handoffs
  13. Trust · 12Context security: injection, poisoning, access controls, tenant boundaries
  14. Trust · 13Measuring context quality: recall, utilization, grounding, ablation
  15. Practice · 14Frameworks and standards: what each one assembles for you
  16. Practice · 15Context in production: versions, drift, refresh, observability, incidents
  17. Practice · 16The insurance lab: one submission, every context decision
  18. ReferenceGlossary
  19. AboutAbout this site and its sources
Practice · 14

Frameworks and standards: what each one assembles for you

MCP resources, provider prompt caching, LangChain, LlamaIndex and agent SDKs, each read on its own documentation and described by what context work it does and does not do.

Four kinds of thing
A standard, a provider feature, a library and an agent SDK sound alike and do different work.
One template each
What it solves, its data model, UW-004 in it, where it stops. Every claim read in its own documentation on 2026-11-17.
What stays yours
Deciding what is relevant, current and authorized, and writing down what was left out and why.

The assembler, described on Context assembly, has worked for a month in plain Python. For case UW-004 (submission SUB-1004) it drops what the underwriting agent may not read, drops the superseded roof rule, ranks and packs the rest, and writes a context manifest: rule R-ROOF-03, the roof age of 9 years from the application and of 22 years from the provider, each with its source. The stand-in agent reads that window and recommends escalate. The model recommends; a person decides.

Then four messages arrive in one week.

The platform team wants the 20 policy documents behind an MCP server, so the underwriter cockpit and the agent read the same text. Finance sees the instructions and policy passages resent on every call of a 40-step run and asks about caching. The long run for SUB-1004 (UW-203, this site's own case) hits the limit, and someone suggests context editing. A new engineer asks why the team does not use LangChain, "since it does context engineering for you".

These are not four versions of one thing. One is a protocol that offers documents and assembles nothing. One is a switch in a provider's API. One is a library that runs in your process. One is a whole agent loop that loads files of its own.

The question that sorts them is not which one is good. It is: which step of building the window does this do for me, and what does it leave to me? Behind it stand the three questions of this site: what did the model see, what was left out, can we prove why.

A framework can move, count, cache or trim context. It does not know which roof rule is current or who may read the reinsurance memo. That judgment, and the record of it, stays in your code.

This is the only page of the site that makes statements about named products and standards; other pages link here. No snippet on this page was run. The only code this site presents as run is examples/uw-context/, and the agent there is a scripted stand-in, so its results show how the mechanism works, not how good any model is.

1. The steps, and who can take one over#

Building one window has the same steps whatever code does them: offer candidates; choose the relevant, current, authorized ones; count and pack; send, reusing what did not change; trim a history that grew too long; record what happened. The diagram places each kind of tool on those steps.

Diagram: Where each kind of tool acts while one window is built

Read left to right, the way one model call is built. Standards and libraries offer candidates. Your assembler chooses what is relevant, current and authorized, and writes the manifest. Provider features count, cache and trim the request. An agent SDK runs the whole loop and loads some context of its own. The amber lane is the one no product on this page takes over.

  1. 1 · Offer candidates
    1. MCP resources: A server offers each item at a URI, with hints for audience, priority and age.
    2. AGENTS.md: One instruction file a coding agent puts into its window at the start of a session.
    3. LlamaIndex: Splits documents into chunks, embeds them and returns the most similar ones.
  2. 2 · Choose and record
    1. Your assembler: Keeps only relevant, current, authorized items and writes what it left out and why.
  3. 3 · Send the request
    1. Count tokens: The provider counts the window with the model's own tokenizer before you send it.
    2. Cache the prefix: The provider reuses the unchanged start of the window; the model sees the same text.
  4. 4 · Over many calls
    1. Trim the history: Old tool results are cleared or summarized when the window passes a threshold.
    2. Agent SDK loop: A ready loop that calls the model, runs tools and loads some context files itself.

Four words carry the rest of the page, so here they are in plain English first.

A standard is an agreement written down so that two programs from different authors can work together. It is text, not code. Example: the Model Context Protocol says what a request for a document looks like, so any client can ask any server.

A provider feature is a capability of one model provider's API, switched on by a field in your request. Example: a cache marker in a Claude request tells the provider to keep the start of the prompt for reuse.

A library is code you import into your program. It runs in your process and you call it. Example: LlamaIndex splits documents into chunks and returns the most similar ones.

An agent SDK is a library that already contains the agent loop: call the model, run the tools it asks for, call it again. Example: the Claude Agent SDK runs the same loop as Claude Code inside your Python program.

Step of building a windowStandardProvider featureLibraryAgent SDK
Offer candidate itemsMCP resourcesLlamaIndex readersMCP clients, file tools
Choose: relevant, current, authorizedSimilarity search; your filtersThe model chooses, with tools
Count tokensCounting endpointsReports usage
Reuse an unchanged prefixPrompt cachingUses the provider's
Trim a long historyContext editing, compactionSummarization middlewareAutomatic compaction
Record what was left out and whyCounts of what was clearedYour middlewareHooks you write

Read the row "Choose". No standard or provider feature fills it, and the library and the SDK fill it with similarity or the model's judgment. Authorization and freshness, the filters that decided UW-201 and UW-204 on the RAG page, stay your code in every column. In the last row, only context editing reports what it removed, and it reports counts, not item ids.

Plain English. A framework is somebody else's code for some of these steps. Read each one by asking which rows of this table it takes over.
What this is not. Tool design, MCP tools included, belongs to Tools Engineering; the runtime loop belongs to Harness Engineering, which has a chapter on MCP.

2. How each one was read#

Each item was read on its own documentation on 2026-11-17; every claim is logged with its address in docs/facts/frameworks.md. Where a page does not state something, the text says "the documentation read here does not say". No prices, no ranking. Every snippet is a documented shape, not run here: Python compiled, JSON parsed, nothing installed.

Each item has the same parts: what it is and the problem it solves, UW-004 in it, what it does for the three questions, and when plain code is better. The data model, strengths, limitations, security and sources are folded below each. Four groups stay apart: standards (MCP resources, AGENTS.md), provider features (token counting, prompt caching, context editing), libraries (LangChain with LangGraph, LlamaIndex) and agent SDKs (Claude, OpenAI).

MCP resources#

Standard. The Model Context Protocol (MCP) is a protocol by which an AI application asks a separate program, called a server, for context. The application is the host; for each server it opens one client connection. A server can offer three kinds of thing: tools (functions the model may ask to run), prompts (templates) and resources. A resource is a piece of data with an address, such as a file or a policy section, that the application can read and put into the window. Example: a server offers uwguide://2026.2/R-ROOF-03, and the host reads it.

The problem it solves. Without a standard, the cockpit and the agent each need a connector to the policy store. With MCP, one server serves every host.

UW-004 in MCP. The host's request for the roof rule, then the server's answer with annotations for audience, importance and age. Success proves the text arrived with its address, not that it is current or that this agent may read it. Documented shape; not run here (the scheme uwguide:// is invented; the specification allows custom schemes).

{
  "jsonrpc": "2.0",
  "id": 7,
  "method": "resources/read",
  "params": {
    "uri": "uwguide://2026.2/R-ROOF-03",
    "_meta": {
      "io.modelcontextprotocol/protocolVersion": "2026-07-28",
      "io.modelcontextprotocol/clientInfo": {"name": "uw-host", "version": "0.1.0"},
      "io.modelcontextprotocol/clientCapabilities": {}
    }
  }
}
{
  "jsonrpc": "2.0",
  "id": 7,
  "result": {
    "resultType": "complete",
    "contents": [
      {
        "uri": "uwguide://2026.2/R-ROOF-03",
        "mimeType": "text/markdown",
        "text": "R-ROOF-03. Roof age on the application and from the provider differ by more than 5 years: escalate to a senior underwriter.",
        "annotations": {"audience": ["assistant"], "priority": 1.0, "lastModified": "2026-07-01T00:00:00Z"}
      }
    ],
    "ttlMs": 60000,
    "cacheScope": "private"
  }
}

For the three questions. The specification calls resources application-driven: the host decides how they enter context. MCP answers "where did this come from" (the URI) and gives hints (priority, lastModified). It does not choose and records nothing about omissions; its architecture page says it does not dictate how an application manages context.

When plain code is better. When one program reads one store it owns, a function call is shorter than a server. MCP earns its place when several hosts read the same sources.

MCP resources: data model, security, version, sourcesAnnotations, authorization, caching hints, what changed in 2026-07-28

Protocol and data model. JSON-RPC 2.0 messages over stdio (local) or Streamable HTTP (remote). A server that offers resources declares the resources capability, optionally with listChanged and subscribe. Methods: resources/list (paginated), resources/read, resources/templates/list (URI templates such as file:///{path}). A resource has uri, name and optional title, description, mimeType, size, icons. Contents are text or base64 blob. Annotations: audience (user, assistant), priority from 0.0 to 1.0 (1 means effectively required), lastModified (ISO 8601). A missing resource is error -32602, never an empty contents list.

Strengths. One server per source, reused by every host. A stable address per item, which is what a manifest needs as an item id. The priority and lastModified hints map onto two of this site's selection signals.

Limitations. Annotations are hints from the server; a host may ignore them, and the server is not the authority on what your policy says is current. lastModified is not a policy version: in this example the roof rule changed meaning between 2025.4 and 2026.2, and that is a fact about the policy, not about a file.

Integration and security. The specification lists five security points for resources: validate every URI, apply access controls to sensitive resources, encode binary data, check permissions before operations, and sanitize file paths against directory traversal. It also allows the list of resources to vary by the authorization presented on each request, which is where UW-201 (the reinsurance memo) belongs: the server should not list it for the underwriting agent at all. Text read from a resource is data, not instruction: a broker attachment served as a resource is still the untrusted P-NOTE-17 of UW-005 (see Security). Results carry ttlMs and cacheScope, which are freshness hints for the client; a cached list can be stale for that long.

Version, read 2026-11-17. The pages read show protocol version 2026-07-28. Two changes in that version matter here and were surprising on the day: the protocol is described as stateless (every request carries its version and capabilities in _meta, and server/discover replaces a stateful handshake), and roots, sampling and protocol logging are deprecated. Who stewards MCP: the documentation read here does not say.

Sources. Resources · Architecture overview.

AGENTS.md#

Standard (an open format). AGENTS.md is a Markdown file at the root of a repository that tells coding agents how to work in it: build steps, test commands, conventions. Its site calls it "a README for agents". There are no required fields. Example: a line that says Run make test before every commit.

The problem it solves. One agreed file name lets one file serve many coding agents. The site lists more than twenty compatible tools and names the Agentic AI Foundation under the Linux Foundation as steward.

For a coding agent. This is the site's second example. AGENTS.md enters the agent's window at the start of every session, so every line costs tokens on every call. Instruction files in full: Coding agents.

The repository of the example, in AGENTS.md. A successful use proves only that the file entered the window; whether the agent follows it is a separate question. Documented shape; not run here.

# AGENTS.md (repository of the underwriting assembler)

## Setup
- Python 3, standard library only. No network in tests.

## Tests
- Run the example: python3 examples/uw-context/run.py
- A change to the assembler must keep the manifest test green.

## Rules
- The agent recommends; a person decides. Never write that the agent approves or declines.
- Every omitted item needs an omission reason from the fixed list.

For the three questions. The AGENTS.md site says the file closest to the edited file wins. Claude Code's documentation, read the same day, concatenates every CLAUDE.md from the root down, and reads AGENTS.md only when no CLAUDE.md is present (a setting changes this). Same repository, two agents, two windows: check your agent's rule before relying on a nested file.

When plain code is better. A rule that must hold every time belongs in a test or a hook, not in the file. Claude Code's documentation says it treats these files as context, not enforced configuration.

AGENTS.md: format, precedence, security, sourcesNested files, migration, what an instruction file cannot enforce

Format and data model. Plain Markdown, any headings. Root file plus nested files in subprojects of a monorepo. The FAQ says explicit chat prompts from the user override the file, and that agents will run the test commands it lists. Migration from another name: rename and keep a symbolic link under the old name.

Strengths. One file for many agents; no schema to learn; versioned with the code it describes.

Limitations. No version number is stated, and precedence between nested files is a convention each agent implements. Claude Code (v2.1.277 or later) reads AGENTS.md when no CLAUDE.md, .claude/CLAUDE.md or CLAUDE.local.md is present in the working directory or above, and does not read AGENTS.local.md, AGENTS.override.md or a .agents/ folder.

Integration and security. Because agents run the commands the file lists, a pull request that edits AGENTS.md changes what an agent will execute. Review it like a build script. Keep secrets out: the file enters the window of every session.

Sources, read 2026-11-17. agents.md · Claude Code: how Claude remembers your project.

Token counting#

Provider feature. A token is the unit a model reads, a piece of a word; the context window page explains it. Token counting is an API call that returns how many tokens a request would use, before you send it. Example: Claude's endpoint answers {"input_tokens": 14} for a short system prompt and one message.

The problem it solves. The example packs with a stand-in tokenizer, which is not any provider's. In production, check the budget with the provider's count, because tokenizers differ by model.

UW-004 in token counting. One tab per provider. Success proves the window fits the budget for that model, nothing about its content. Documented shape; not run here.

Claude

# count_uw_004_claude.py · documented shape; not run here
import anthropic

from my_assembler import build_window  # your assembler: returns system text and messages

window = build_window(submission_id="SUB-1004", run_id="run-7f3a", attempt_number=1)
client = anthropic.Anthropic()
count = client.messages.count_tokens(
    model="claude-opus-5-5",  # count with the model you will call
    system=window["system"],
    messages=window["messages"],
)
print(count.input_tokens)

Gemini

# count_uw_004_gemini.py · documented shape; not run here
from google import genai

from my_assembler import build_window_text  # your assembler, flattened to one text

client = genai.Client()
count = client.models.count_tokens(
    model="your-gemini-model-id",
    contents=build_window_text(submission_id="SUB-1004"),
)
print(count.total_tokens)

For the three questions. Logged in the manifest, a count proves the window fit. It chooses nothing.

When plain code is better. A local estimate is enough while you design budget shares. Count with the provider before you set a hard limit, and again when you change model.

Token counting: estimates, tokenizer changes, three providersWhat each documentation says, and what it does not

Claude. Endpoint /v1/messages/count_tokens, SDK method messages.count_tokens. It accepts the same inputs as a message: system prompt, tools, images and PDFs as base64, thinking. The documentation calls the result an estimate that can differ by a small amount, and says it may include tokens the provider adds for its own optimization. It returns an error for server tools other than the advisor tool, for the MCP connector, and for images or documents passed by URL or file id. It does not use prompt caching. It also accepts context-editing settings and then reports the count before and after editing.

A fact that breaks old budgets. The token counting page says Claude 4.7 and later models use a newer tokenizer that produces roughly 30 percent more tokens for the same text than earlier models. A budget measured on one model is not valid on another; count again.

Gemini. client.models.count_tokens returns total_tokens. The page states a rule of thumb: about 4 characters per token. Surprising on the day: the Gemini API pages at ai.google.dev now redirect to aistudio.google.com, and the token page describes usage fields of an Interactions API (total_input_tokens, total_cached_tokens and others).

OpenAI. The conversation-state page read here points to a tokenizer tool for estimates and does not mention a counting endpoint. It states that with previous_response_id, all earlier input tokens in the chain are billed as input again: the window is resent even when you do not resend it yourself.

Sources, read 2026-11-17. Claude token counting · Gemini tokens · OpenAI conversation state.

Prompt caching#

Provider feature. Every call resends the whole window. Prompt caching lets the provider keep the processed start of a prompt, the prefix, and reuse it when the next request starts with exactly the same text. Example: instructions and policy passages are identical in all 40 steps of UW-203; only the end changes. The concept lives on Budgeting.

The problem it solves. Finance's question: a cache hit means the provider does not process the same prefix again.

UW-004 in Claude's prompt caching. A cache marker ends the stable part, the policy text; the submission and tool results after it may change. A successful second call shows a non-zero cache_read_input_tokens. Documented shape; not run here.

{
  "model": "claude-opus-5-5",
  "max_tokens": 1024,
  "system": [
    {"type": "text", "text": "You are the underwriting agent. You recommend; a person decides."},
    {"type": "text", "text": "<policy version=\"2026.2\">R-ROOF-01 ... R-ROOF-03 ... R-HUMAN-01</policy>",
     "cache_control": {"type": "ephemeral"}}
  ],
  "messages": [
    {"role": "user", "content": "<submission id=\"SUB-1004\">roof_age_years: 9 ...</submission> <tool_result source=\"get_roof_assessment\">roof_age_years: 22</tool_result>"}
  ]
}

For the three questions. Caching does not change what the model sees; Claude's documentation says the response is the same as without it. It pulls against freshness: a change of policy_version must change the prefix.

When plain code is better. Caching needs no code of yours beyond ordering the window: stable first, changing last. If your windows share no long prefix, there is nothing to cache.

Prompt caching: three providers, invalidation, isolationPrefix order, minimum lengths, what breaks a hit

Claude. cache_control: {"type": "ephemeral"}, with optional "ttl": "1h"; the default lifetime is 5 minutes and is refreshed when the entry is used. Automatic caching (one top-level marker that moves forward as a conversation grows) or up to 4 explicit breakpoints. Prefixes are built in the order tools, then system, then messages, and a change at one level invalidates that level and every later one: changing a tool definition invalidates everything. The lookback for an earlier entry is 20 blocks. Minimum cacheable length depends on the model (the table read ranges from 512 to 4,096 tokens); a shorter prompt is processed without caching and without an error, so check the usage fields cache_creation_input_tokens and cache_read_input_tokens. Caches are isolated between organizations and, on the Claude API, per workspace.

OpenAI. On by default for supported models; on GPT-5.6 and later an explicit breakpoint mode exists (prompt_cache_options). Minimum 1,024 tokens on GPT-5.6 and later. Put stable content first and dynamic content last; append messages rather than rewriting earlier turns. The page notes that compaction and truncation can reduce reuse. Cached tokens appear in usage.input_tokens_details.cached_tokens. Caches are not shared across organizations. Retention options differ by model generation; read the page for your model.

Gemini. Implicit caching is on by default for Gemini 2.5 and newer, with minimums of 2,048 to 4,096 tokens in the table read. Explicit caching (client.caches.create, referenced with cached_content=cache.name, default lifetime 1 hour) is in beta and, per the page, not supported for Gemini 4 models or in the Interactions API.

The trade-off a Principal engineer owns. The cache rewards a window that does not change; context engineering keeps changing the window to keep it relevant. Every time compaction rewrites history or a tool result is cleared, the cached prefix after that point is gone. Put the items that change least (instructions, tool definitions, the current policy) at the front, record context_version and policy_version in the manifest, and measure hit rates before and after a change of assembly. The 30 percent tokenizer change noted above also changes whether a prefix clears a minimum length.

Sources, read 2026-11-17. Claude prompt caching · OpenAI prompt caching (moved from platform.openai.com) · Gemini caching.

Context editing#

Provider feature. Context editing is a Claude API feature that removes old content on the provider's side, before the model reads it, once the conversation passes a threshold you set. Example: past 30,000 input tokens, the oldest tool results become a short placeholder and the last three stay. The technique and its test live on Compaction.

The problem it solves. In UW-203, stale tool results crowd out the roof rule. Context editing clears them without changing the history your program keeps.

UW-203 in context editing. The request keeps the three newest tool results and never clears check_eligibility, whose result carries the rule outcome. A successful call returns context_management.applied_edits with the number of tool uses and tokens cleared. Documented shape; not run here (the beta header context-management-2025-06-27 goes with it).

{
  "model": "claude-opus-5-5",
  "max_tokens": 2048,
  "messages": [{"role": "user", "content": "Continue the assessment of SUB-1004."}],
  "context_management": {
    "edits": [
      {
        "type": "clear_tool_uses_20250919",
        "trigger": {"type": "input_tokens", "value": 30000},
        "keep": {"type": "tool_uses", "value": 3},
        "clear_at_least": {"type": "input_tokens", "value": 5000},
        "exclude_tools": ["check_eligibility"]
      }
    ]
  }
}

For the three questions. It answers part of "what was left out": how many tool uses and tokens, not which ones. To prove the roof conflict survived you still need the loss test, run on what the model saw.

When plain code is better. An assembler that builds each window from its own records can leave old tool results out itself, with a reason per omission. More work, better record.

Context editing and compaction: parameters, cache cost, OpenAIDefaults, thinking blocks, memory tool, server-side compaction

Data model. Beta, header anthropic-beta: context-management-2025-06-27. Strategy clear_tool_uses_20250919 with defaults trigger 100,000 input tokens and keep 3 tool uses; clear_at_least (if that much cannot be cleared, nothing is), exclude_tools, and clear_tool_inputs (default false: only results are cleared). Strategy clear_thinking_20251015 handles thinking blocks of earlier turns and must be listed first when both are used. Editing happens on the server; the client keeps the full unmodified history.

Strengths. No change to your stored history; a count of what was removed in every response; tools you name are never cleared.

Limitations and the cache. Clearing tool results invalidates the cached prefix from the point of clearing. clear_at_least exists so that each clear removes enough to be worth the new cache write. The documentation recommends server-side compaction (compact_20260112) for most long conversations and context editing where finer control is needed. Surprising on the day: the client-side compaction_control of the SDK tool runner is deprecated in TypeScript and Ruby and was removed from the Python SDK in v1.0.

Integration. Context editing can be combined with the memory tool: before clearing, the model receives a warning so that it can save information to memory files. That moves facts from working context to durable memory, which is the boundary of Memory boundary.

The same idea at OpenAI. The conversation-state page names compaction on the server (context_management with compact_threshold on /responses) and a standalone /responses/compact endpoint. It does not describe a selective clearing of tool results in the text read.

Sources, read 2026-11-17. Claude context editing · OpenAI conversation state.

LangChain and LangGraph#

Library. LangChain is, in its documentation's words, "the agent framework": it gives model and tool abstractions, an agent loop, and middleware, which are functions that run before or around each model call. LangGraph, documented on the same site, is a lower-level runtime for long-running, stateful agents, built as graphs of nodes over shared state; it does not require LangChain. Example: a middleware function that rewrites the system prompt when the conversation passes ten messages.

The problem it solves. Its context-engineering page names the problem as this site does: the right information and tools, in the right format, on each call. Its answer is hooks. LangGraph adds durable execution and checkpoints and "does not prescribe prompts or agent architecture".

UW-004 in LangChain. One middleware puts the assembler in front of every model call: it replaces the messages with the window the assembler built and keeps the manifest. A successful run proves the agent saw your window, not the library's default history. Documented shape; not run here.

# uw_middleware.py · documented shape; not run here
from langchain.agents import create_agent
from langchain.agents.middleware import ModelRequest, wrap_model_call

from my_assembler import build_window, save_manifest  # the assembler of this site, your version
from my_tools import check_eligibility, find_similar_decisions, get_roof_assessment, search_guidance


@wrap_model_call
def assembled_window(request: ModelRequest, handler):
    window = build_window(submission_id="SUB-1004", run_id="run-7f3a", attempt_number=1)
    save_manifest(window["manifest"])  # included, omitted, a reason for each
    return handler(request.override(messages=window["messages"]))


agent = create_agent(
    model="your-model-id",
    tools=[get_roof_assessment, search_guidance, find_similar_decisions, check_eligibility],
    middleware=[assembled_window],
)

For the three questions. LangChain gives you the place to answer them, the hook before each call, and leaves the answer to your code. Its built-in SummarizationMiddleware trims history with a separate model call and replaces older messages with a summary in state; what the summary dropped is not recorded by it.

When plain code is better. With one loop and four tools, a plain loop around the provider's SDK does the same. LangGraph earns its place when a run must survive a restart or wait for a person, the territory of Orchestration.

LangChain and LangGraph: data model, limits, sourcesState, store, runtime context, and what the pages leave unstated

Data model. Three data sources in the context page: runtime context (static configuration such as user id and permissions), state (short-term memory: messages, files, tool results) and store (long-term memory across conversations). Hooks: @dynamic_prompt, @wrap_model_call with request.override(messages=..., tools=..., model=..., response_format=...), and before_model, after_model, wrap_tool_call. Built-ins named: SummarizationMiddleware(model, trigger, keep) and LLMToolSelectorMiddleware.

Strengths. Per-call control of messages, tools, model and output format in one place; tool filtering by role, which is tool disclosure as described on Dynamic context.

Limitations. The page states that models attend more to final messages and appends context at the end in its examples; that is a placement choice that can work against a cached prefix. A version number is not stated on the pages read, and the pages read do not say directly that create_agent runs on LangGraph (the context page imports Command and a store from langgraph).

Integration and security. Runtime context is the right home for permissions, so that a tool and a middleware can filter before retrieval. A summary written by a model is model output; treat it as data with provenance, not as instruction, in later windows.

Sources, read 2026-11-17. LangChain: context engineering in agents · LangGraph overview.

LlamaIndex#

Library. LlamaIndex calls itself "a Python toolkit for building LLM-powered agents over your data". It has readers that load documents, indexes that split them into chunks (it calls them nodes) and embed them, retrievers that return the most similar nodes, and query engines and agents on top. Example: VectorStoreIndex.from_documents(documents) then index.as_retriever().

The problem it solves. The retrieval half of assembly: from 20 policy documents to a ranked short list. The concepts are on Retrieval and RAG.

UW-004 in LlamaIndex. A chunk of the policy fixture becomes a Document with metadata; the retriever returns candidates for the roof question. Success returns scored nodes; it does not show that R-ROOF-03 is from 2026.2 unless your code checks. Documented shape; not run here.

# uw_llamaindex.py · documented shape; not run here
from llama_index.core import Document, VectorStoreIndex

doc = Document(
    text="R-ROOF-03. Roof age on the application and from the provider differ by more than 5 years: escalate.",
    metadata={"doc": "UW-GUIDE", "version": "2026.2", "status": "current", "audience": "underwriting", "tenant": "insurer"},
)
doc.excluded_llm_metadata_keys = ["tenant", "audience"]  # keep access labels out of the model's text

index = VectorStoreIndex.from_documents([doc])  # embeds with the default model unless configured
retriever = index.as_retriever()
for node in retriever.retrieve("roof age differs between application and provider"):
    print(node.score, node.metadata["version"])

For the three questions. One documented default matters more than any other here: the metadata of a document "is injected into the text for both embedding and LLM model calls". Labels you added for filtering, such as a tenant or an audience, enter the window unless you exclude them. LlamaIndex returns the candidates; deciding which are current and authorized, and recording the omissions, stays with you.

When plain code is better. With 20 documents, the example's plain retrievers and filters are enough and fully explainable. LlamaIndex earns its place with many sources and formats.

LlamaIndex: defaults, filters, rename, sourcesWhat the pages read show, and the filter they do not

Data model. Documents are split into nodes that keep metadata and relationships. VectorStoreIndex stores everything in memory by default; a StorageContext with a vector store persists it. Retrievers come from index.as_retriever(); retrieve(query) returns nodes. Metadata can be kept out of the model's text with excluded_llm_metadata_keys and out of the embedding with excluded_embed_metadata_keys.

Limitations. The default top-k is not stated on the pages read. A metadata filter applied before similarity is not shown on the retriever, vector-index or vector-store pages read; the vector-store page has a per-store column for metadata filtering. Before you rely on the library to keep UW-201's reinsurance memo out, check the filter of the vector store you choose and test it, as Security describes. The starter embeds with OpenAI by default, so a key and a network call are part of the first run.

Surprising on the day. The overview says the hosted platform formerly called LlamaCloud has been renamed LlamaParse, while the code still imports a llama_cloud package (pip install llama-index "llama-cloud>=2.8"). The library version is not stated on the overview.

Sources, read 2026-11-17. Framework overview · Documents · Vector store index · Retriever · Vector stores.

Claude Agent SDK#

Agent SDK. An agent SDK is a library that already contains the agent loop and the context handling around it. The Claude Agent SDK, in its own words, lets you build agents "with Claude Code as a library": the same tools, loop and context management as Claude Code, in Python or TypeScript. Example: query(prompt="...", options=ClaudeAgentOptions(...)) runs a whole agent session.

The problem it solves. You get file tools, subagents with their own windows, MCP connections, permissions, sessions and the context management of Claude Code without writing them.

UW-004 in the Claude Agent SDK. The options decide what enters the window at the start: a system prompt of your own, no settings files from disk, one MCP server for the policy documents, and the write tool removed. A successful run streams messages that end in a recommendation for the underwriter. Documented shape; not run here.

# uw_agent_sdk.py · documented shape; not run here
import asyncio

from claude_agent_sdk import ClaudeAgentOptions, query


async def main():
    options = ClaudeAgentOptions(
        system_prompt="You are the underwriting agent. You recommend; a person decides.",
        setting_sources=[],  # load no CLAUDE.md or settings from disk
        mcp_servers={"uwguide": {"command": "uwguide-server", "args": []}},  # a stdio server
        allowed_tools=["mcp__uwguide__*"],  # MCP tools need permission to be called
        disallowed_tools=["Bash"],  # a bare name removes the tool from the window
    )
    async for message in query(prompt="Assess submission SUB-1004.", options=options):
        print(message)


asyncio.run(main())

For the three questions. The SDK loads context for you; check it. By default setting_sources loads user, project and local settings like the CLI, so a CLAUDE.md on the machine can enter a production agent's window. Set it explicitly. What the model read is visible in the streamed messages; what the loop compacted away is not described on the page read.

When plain code is better. When the agent's tools are four functions and no files or shell, the provider's client SDK and a loop of your own keep every window under your control. The SDK earns its place when the agent works on files and commands, which is the coding-agent case.

Claude Agent SDK: options, defaults, terms, sourcesWhat loads by default, compaction, authentication

Options that shape the window. system_prompt as your own string or as the claude_code preset with an append. allowed_tools auto-approves listed tools and does not restrict the set; disallowed_tools with a bare name removes the tool from Claude's context. agents defines subagents (AgentDefinition requires description and prompt), each with a fresh window, as Isolation describes. Each query starts a new session unless continue_conversation or resume is set; fork_session branches one.

Limitations. Automatic compaction is mentioned on the Python reference only in passing (isAutoCompactEnabled, autoCompactThreshold); when it triggers and what it keeps is not described there. Read the agent-loop page before relying on it for a run like UW-203.

Two MCP defaults that shape the window. The SDK's MCP page says tool search is on by default: MCP tool definitions are withheld from the window and loaded when Claude needs them, which is tool disclosure done for you. And a successful MCP tool result larger than 25,000 tokens is saved to a file and replaced by a message naming the file, so the agent reads it back in portions. That is the remedy for UW-202 (the very long roof report), applied by the SDK; your manifest should still record that the full report was not in the window. MCP tools need explicit permission (allowed_tools) before they can be called.

Integration and security. The overview says third-party products may not offer claude.ai login unless approved, and points to API key authentication. Use of the SDK is governed by Anthropic's Commercial Terms of Service. Because skills, commands and memory load from .claude/ and ~/.claude/ by default, the window of an agent depends on the machine it runs on until you pin setting_sources. The platform URL for the SDK redirected to code.claude.com on the day.

Sources, read 2026-11-17. Agent SDK overview · Python reference · MCP in the SDK.

OpenAI Agents SDK#

Agent SDK. The OpenAI Agents SDK describes itself as "a lightweight, easy-to-use package with very few abstractions": agents (a model with instructions and tools), handoffs and agents as tools, guardrails, sessions and tracing. Example: Runner.run_sync(agent, "...") runs one agent to a final output.

The problem it solves. A small, typed agent loop with delegation between agents and a session store for conversation history.

UW-004 in the OpenAI Agents SDK. Its context page says a local context object, read by tools, "is not sent to the LLM". So the caller's role travels with the run without entering the window. Documented shape; not run here.

# uw_openai_agents.py · documented shape; not run here
from dataclasses import dataclass

from agents import Agent, RunContextWrapper, Runner, function_tool


@dataclass
class RunInfo:
    run_id: str
    caller_role: str  # read by tools; never placed in the window


@function_tool
def search_guidance(wrapper: RunContextWrapper[RunInfo], query: str) -> str:
    """Search the current policy documents the caller may read."""
    role = wrapper.context.caller_role
    return f"(passages for {query!r} visible to {role})"


agent = Agent[RunInfo](
    name="Underwriting agent",
    instructions="You recommend; a person decides.",
    tools=[search_guidance],
)
result = Runner.run_sync(agent, "Assess SUB-1004.", context=RunInfo("run-7f3a", "underwriting"))
print(result.final_output)

For the three questions. The page says the only data the model sees is the conversation history: instructions, input, tools and retrieval results. Which passages a tool held back is your tool's record.

When plain code is better. Same rule as above: with one agent and four tools, a loop of your own is short. The SDK earns its place with several agents and handoffs, where Isolation asks what each one sees.

OpenAI Agents SDK: sessions, providers, sourcesLocal versus model context, and what the pages leave unstated

Data model. Agent (name, instructions, tools, handoffs), Runner.run and run_sync, RunContextWrapper[T] for local context, sessions described as "a persistent memory layer for maintaining working context within an agent loop" (SQLAlchemy, advanced SQLite and encrypted variants are listed). The SDK uses the Responses API by default for OpenAI models; a models page covers non-OpenAI providers, not named on the page read.

Strengths. A clean split between local context and model context, which is this site's distinction between data the program holds and data the model sees.

Limitations. Whether tracing is on by default is not stated on the pages read, and neither is a version number. A session stores history; what a session puts back into the next window, and how it trims, needs a check against the session pages before a long run.

Integration and security. Keep permissions and identifiers in local context and enforce them inside tools. Handoffs move history between agents; scope it, as Isolation shows with the handoff brief.

Sources, read 2026-11-17. Overview · Context management.

The nine side by side#

The filter buttons narrow the rows by kind, where the code runs, and what the item does for selection and for the record.

ItemKindRuns inChooses itemsRecords omissionsWhat it does to the window
MCP resourcesstandardspec onlynonoOffers items by URI with hints: audience, priority, last modified
AGENTS.mdstandardspec onlynonoOne instruction file that enters a coding agent's window
Token countingproviderprovidernonoCounts a window with the model's own tokenizer; an estimate
Prompt cachingproviderprovidernonoReuses an unchanged prefix; the model sees the same text
Context editingproviderproviderpartlypartlyClears old tool results past a threshold; reports counts
LangChain and LangGraphlibraryyour processpartlypartlyHooks before each call; summarization; durable state
LlamaIndexlibraryyour processpartlynoChunks, embeds, retrieves; injects metadata by default
Claude Agent SDKSDKyour processpartlynoFull loop; loads settings files by default; compaction
OpenAI Agents SDKSDKyour processpartlynoFull loop; local context never sent to the model

"Chooses items: partly" means by similarity, the model's judgment or age, never by authorization or policy version. "Records omissions: partly" means a count (context editing) or a hook where your code writes the record (LangChain). Nothing in the table says which one to choose.

What stays yours#

Back to the four messages. MCP resources can carry the 20 policy documents to every host. Prompt caching answers finance once the stable part comes first. Context editing can keep UW-203 under the limit. LangChain gives a hook where your assembler runs.

None of them decides that the 2025.4 roof rule is superseded, that the reinsurance memo is not for this agent, or that the conflict between 9 years and 22 years must survive every trim. And none writes a manifest that says what was left out and why. The diagram shows the seam that keeps those decisions in one place, so that changing a library does not change them.

Diagram: What stays yours when a framework comes in

Read left to right. The left lane holds the decisions that make a window right for this insurer: who may read what, which policy version is in force, and the manifest that proves it. Keep them in your own code. The middle lane is one small adapter per tool. The right lane is replaceable. Changing a library then changes the middle lane only.

  1. 1 · Yours, in your repository
    1. Access and freshness rules: Who may read each item and which version is current, checked before any ranking.
    2. The manifest: The record of one window: each item in or out, with a reason from a fixed list.
  2. 2 · The seam
    1. One adapter per tool: Hands your chosen items to the tool and brings its results back as candidates.
  3. 3 · Replaceable
    1. Transport: Moves items between servers and hosts with an address for each.
    2. Retrieval: Finds and ranks candidates; trims history with hooks.
    3. Loop and provider: Runs the calls, counts, caches and trims on the provider's side.
Before AI. A Spring application can use JPA, a connection pool and a second-level cache, and the WHERE clause that enforces a tenant is still yours: Hibernate does not know your tenant rule. The comparison stops at determinism: a query returns the rows you asked for, while a model may weigh the same window differently from call to call, so you also record what it saw.

A checklist before you adopt one#

Apply it to any item on this page, or to the next one that appears.

QuestionWhy it mattersWhere on this site
Which step of building the window does it take over?Name the row of the table in section 1 before the decisionAssembly
Does anything it loads enter the window without your code choosing it?Settings files, injected metadata and summaries are context tooSignals
Does authorization run before similarity, in your code or its filter?A relevant-looking unauthorized passage must never be a candidateSecurity
How does it handle a superseded version?A stale chunk scores as high as the current oneRAG
What does it trim, and can you test what survived?The loss test needs the window the model actually sawCompaction
Does it break your cached prefix, and how often?Editing and compaction rewrite the windowBudgeting
Can you still write the manifest: included, omitted, a reason each?Without it, "can we prove why" has no answerProduction
Which version and which date did you read?These pages moved and changed on the day this page was writtendocs/facts/frameworks.md
Trade-offs a Principal engineer ownsCache against freshness, hints against authority, moving pages

Cache stability against relevance. A window that never changes caches perfectly and goes stale; a window rebuilt for every step stays relevant and caches nothing after the first change. The working compromise is an order: tools and instructions, then the current policy, then case facts, then the latest tool results. Changes concentrate at the end. Record the boundary in the manifest so that a drop in cache hits can be traced to an assembly change.

Server hints against application authority. MCP's priority and lastModified, LlamaIndex's metadata and a model's choice in an agent SDK are all signals. Authority over what may enter, and which policy version is in force, stays with the application. A design that lets a server's priority: 1.0 override the assembler's authorization filter has handed a security decision to whoever runs the server.

Provider-side trimming against a client-side record. Context editing keeps your history intact and edits on the server; your manifest then describes the window you sent, not the one the model read. Log applied_edits next to the manifest, or do the trimming yourself when the record must be exact.

Moving documentation. On the day this page was read, OpenAI's guides had moved to a new host, the Gemini API documentation redirected to AI Studio, a library's hosted product had a new name, MCP had a new protocol version with deprecations, and Claude Code had started reading AGENTS.md. A framework decision is a dated decision: keep the fact log and read it again before each upgrade, as Production does with context versions.

In one breath#

Building a window has the same steps whoever writes the code: offer candidates, choose what is relevant, current and authorized, count, pack, reuse the unchanged prefix, trim the history, and record what was left out. MCP resources and AGENTS.md are agreements about how context is offered; token counting, prompt caching and context editing are provider switches that count, reuse and trim; LangChain and LlamaIndex give hooks and retrieval in my process; the two agent SDKs give me a whole loop that loads context of its own. None of them chooses by authorization or policy version, and none writes my manifest, so those stay in my assembler behind a thin seam, and I read each tool's documentation again, with a date, before I trust a row.

What comes next#

You can now look at a framework, a standard or a provider feature and say which step of building the window it takes over, what it loads without asking, and what you keep in your own code. What remains is time. The policy changes, the index is refreshed on a different day from the instructions, a model with a new tokenizer arrives, and a documentation page moves. Production is about the window over months: context versions, drift, refresh, the manifest as a log line, and how to explain an incident from it.