Skip to main content

Context Engineering

Everything the agent knows at any moment is what fits in the context window. This page covers the techniques for deciding what goes in and keeping it small: window management, memory tiers, chunking, and relevance scoring.

This is the technique layer beneath Memory Management β€” that page covers when an agent needs memory; this one covers how each mechanism works.


Context window management​

The context window is a flat list of messages β€” system prompt, user turns, model reasoning, tool calls, tool results. Every token counts against the budget, and tool results are usually the biggest consumer: one large file read can cost thousands of tokens in a single step.

Four strategies, three reactive and one proactive:

StrategyHow it worksTrade-off
Sliding windowKeep system prompt + last N messages, evict the restEarly observations vanish β€” if step 15 depends on step 2, the agent fails or hallucinates
Auto-compactSummarize old messages with a model call before evicting; inject the summary backCosts latency and tokens; the summary may drop a detail that later matters
Output truncationCap tool results before they enter memory ([output truncated])The model can't see what was cut; pair with a way to re-fetch
Token budget signalingInject remaining-budget count into the system prompt so the model self-regulates verbosityThe only proactive strategy β€” but too aggressive a warning makes the model terse too early

Decision guide

  • Short sessions that fit comfortably β†’ do nothing; management costs more than it saves
  • Long tool-heavy sessions β†’ truncate outputs first (cheapest), then add a sliding window
  • Sessions where early decisions must survive β†’ auto-compact with a summary prompt that preserves tool results, decisions, and open tasks β€” not prose
  • Agent overshoots the limit mid-task β†’ add budget signaling so it lands the task before the wall

Used in: Data Validation Agent β€” session state as an in-context YAML block, updated per tool call.


Memory tiers​

Memory is what survives beyond the current window. Three tiers, ordered by scale:

TierSurvives restartScales toContext cost
Short-term β€” the context window itselfNo~200K tokensO(history)
Intermediate β€” facts in a file, injected at session startYesDozens of factsO(n) β€” everything injected
Long-term β€” indexed store, retrieved by relevanceYesThousands of factsO(k) β€” top-k only

Intermediate memory breaks at hundreds of facts: token cost grows linearly, attention dilutes across irrelevant context, and eventually the injection alone blows the budget. The fix is retrieval β€” a clean system prompt, with the agent fetching only the top-k facts relevant to the current query. Scoring those facts is the relevance scoring problem below.

Used in: Data Validation Agent (intermediate β€” in-context state block) Β· Researcher Agent (retrieval scoring applied statelessly β€” no store at all).


Chunking strategies​

Retrieval and compression both operate on chunks, and chunk boundaries decide what a relevance score can see. A chunk that splits mid-sentence scores poorly for every query; a chunk with three topics scores mediocre for all of them.

StrategyHow it splitsUse when
Fixed-sizeEvery N chars, with overlapBaseline; text has no exploitable structure
Paragraph-awarePack whole paragraphs up to the size limit; hard-slice only oversized onesProse with real paragraph breaks β€” scraped articles, docs. A few lines of code, no library
RecursiveTry splitting by section, then paragraph, then sentence, then charsMixed or nested structure (LangChain's RecursiveCharacterTextSplitter)
SemanticSplit where embedding similarity between adjacent sentences dropsRetrieval quality justifies an embedding pass per document

Two knobs regardless of strategy:

  • Chunk size β€” smaller chunks score more precisely but lose surrounding context; larger chunks preserve context but dilute the score. 500–1000 chars is the common range.
  • Overlap β€” repeats the tail of one chunk at the head of the next so a fact straddling a boundary survives in at least one chunk. Only applies where hard slicing happens.

Used in: Researcher Agent β€” paragraph-aware at 800/100, chosen over recursive to avoid a LangChain dependency.


Relevance scoring​

Given a query and a pile of chunks (or facts), which ones deserve context space? Two base methods, each blind where the other sees:

MethodStrengthWeakness
BM25 (keyword)Exact tokens β€” names, IDs, URLs, codeMisses paraphrase: "deployment infra" won't match "CI/CD"
Embedding cosine similaritySemantic matches β€” concepts, synonymsSmooths over exact tokens; "AWS" may not outrank a vaguely-cloudy chunk
HybridBothOne extra scoring pass

Hybrid scoring​

Normalize both score sets to [0, 1], combine with a weighted sum, take top-k. A common split is 0.3 BM25 + 0.7 vector β€” semantic carries most queries, keyword rescues exact-token ones.

Hybrid scoring, in one pass
bm25_scores = BM25(chunks).score(query) β†’ normalize to [0,1]
vector_scores = cosine(embed(chunks), embed(query))
final = 0.3 Γ— bm25 + 0.7 Γ— vector β†’ sort, keep top-k

Two implementation notes that matter more than the weights:

  • Batch the embedding call. Embed all chunks plus the query in one API call, not one call per chunk. Faster, cheaper, and the idiomatic use of a batchable API.
  • Stateful vs stateless is a real fork. A memory store embeds once at add() and persists the index for many future searches. Compression scores throwaway chunks once and discards them β€” no store, no persistence, plain functions. Same formula, different lifecycle; don't build the store when nothing needs to persist.

Beyond hybrid​

Techniques to append here as they come up in future use cases:

  • LLM re-ranking β€” a second pass where a model re-orders the hybrid top-k; better precision, one extra call per query
  • Cross-encoder re-ranking β€” scores query and chunk jointly instead of comparing separate embeddings; more accurate, doesn't scale past a shortlist

Used in: Researcher Agent β€” hybrid at 0.3/0.7, stateless, batched embeddings.