Day 22 — Bonus 5 May 4, 2026

Context Engineering — Filling the Context Window Like an Engineer, Not a Prompter

In 2024 the bottleneck of an LLM application was the model. By mid-2026 it is almost always the context — what goes in the window and in what order. The teams shipping reliable agents have stopped writing prompts and started engineering context: prompt-cache hygiene, just-in-time tool injection, compaction, sub-agent isolation, and aggressive measurement of context-rot. If you cannot speak about KV cache reuse, lost-in-the-middle, and compaction strategies, you are still in the prompt-engineer chair while your peers have moved into the context-engineer chair.

Context Window
Prompt Cache
KV Cache
Compaction
Just-in-Time Context
Lost in the Middle
Context Rot
Sub-agents
Curriculum
Bonus · Day 22 of 17 + 5
Core Concept

What Context Engineering Actually Is — The Discipline Underneath the Prompt

Analogy first. Imagine briefing a brilliant but amnesiac contractor every morning. She forgets everything overnight. Each day you choose what folders to put on her desk, in what order, with what sticky notes on top — and after eight hours her desk is full and she stops being effective. Prompt engineering is the sticky note. Context engineering is the whole desk: what is on it, how it’s arranged, what gets cleared away mid-day, what gets summarised onto a fresh sheet so the project keeps moving. Karpathy’s phrasing on X (June 25, 2025) is the cleanest one-liner: context engineering is “the delicate art and science of filling the context window with just the right information for the next step.”

Mechanism. An LLM call is a stateless function: tokens in, tokens out. Every input token costs latency, money, and (worse) attention dilution — with each extra token, the share of attention any single token can claim shrinks. Empirically (Liu et al. 2023, Hsieh et al. 2024) accuracy degrades non-linearly with input length and is especially bad for information buried in the middle of a long context. The context engineer’s job is therefore to maximise signal-per-token, exploit cache hits on the static prefix, and choose when to compact, when to fetch just-in-time, and when to spawn a sub-agent with a fresh window.

The four pillars an engineer must distinguish:

The four levers of context engineering

A
SELECT
Selection — what gets in
Which of the candidate documents, tools, memories, and prior turns belong in this call?
Retrieval (Day 4’s territory) is one half. The other half is tool selection — an agent given 100 tools loses ~30% accuracy vs. one given the right 10. Anthropic’s “just-in-time” pattern is the canonical fix: keep an index of available tools/data, pull the specific ones into the window only when the agent decides it needs them.
B
ORDER
Ordering — where it sits
Why does putting the answer at the top of the document beat putting it in the middle by 25 points?
The lost-in-the-middle phenomenon (Liu et al. 2023, TACL) is the most replicated empirical finding in long-context work. Beginning and end positions get measurably more attention. Production stacks now treat ordering as a tunable: stable system instructions first (cache-friendly), large reference docs in the middle with explicit headers, the user’s actual question last.
C
CACHE
Caching — what costs nothing
Why is the order of your context blocks now a P&L decision?
Anthropic, OpenAI, and Google all expose prompt caching: a deterministic prefix gets its KV tensors cached server-side and reused for free on the next call within a TTL. Cache hits are typically 10× cheaper and 3–5× faster on time-to-first-token. The corollary: if the variable user input lands at position 2 instead of position N, you’ve just nuked the cache for everyone.
D
COMPACT
Compaction — when to throw it away
Why does Claude Code summarise the conversation at ~80% context fill and start a new window?
Past a threshold (typically ~75–90% of the model’s window), models degrade faster than fresh ones. Compaction is the Anthropic-coined practice of summarising the trajectory so far and reseeding a new window. The harder cousin is sub-agent isolation: spawn a child agent with its own clean window for a sub-task, then absorb only its return value into the parent. Both are mechanical answers to a non-mechanical question: when has the context outlived its usefulness?

The three timescales of context. A useful 2026 vocabulary: turn-level context (the system prompt and current message), session-level context (the running conversation, tool traces, and scratchpads), and persistent context (long-term memory, codebase, RAG corpora). Each scale has its own engineering: turn-level is about ordering and caching; session-level is about compaction and sub-agents; persistent is about retrieval and storage hierarchies. Mixing them up — e.g. dumping persistent memory into turn-level context — is the most common rookie mistake and the one that turns a working demo into a $40K/month invoice.

CONTEXT WINDOW LAYOUT — high-signal, cache-friendly order ┌─ STATIC PREFIX (cached) ─────────────────────────────┐ [01] system prompt (rarely changes) [02] tool schemas (changes per release) [03] persona / style guide (rarely changes) └──────────────────────────────────────────────────────┘ ┌─ SESSION CONTEXT (semi-stable) ──────────────────────┐ [04] compacted history (refreshed each compact) [05] JIT-loaded docs (skill / file just used) └──────────────────────────────────────────────────────┘ ┌─ HOT TURN (uncached, every call) ────────────────────┐ [06] recent tool outputs (last 1-3 calls) [07] user message (the question) └──────────────────────────────────────────────────────┘ → ~85% of tokens hit cache · ~3-10x cheaper · TTFT < 500ms

Papers to Know

Three Papers That Define the Field

SEMINAL TACL 2024
Lost in the Middle: How Language Models Use Long Contexts
Nelson F. Liu, Kevin Lin, John Hewitt, Ashwin Paranjape, Michele Bevilacqua, Fabio Petroni, Percy Liang · arXiv:2307.03172
The empirical paper everyone in context engineering points to. The authors test multi-document QA and key-value retrieval as a function of where in the input the relevant information is placed. Result: a U-shaped curve — performance is highest at the very beginning and very end of the input, and drops sharply when relevant content lives in the middle, even for explicitly long-context models. The effect persists across model families and scales. This is the empirical foundation for “put the question last” and for ordering being a first-class engineering concern.
What changed: ordering became a hyperparameter. Every modern agent stack now has explicit logic for where chunks land in the prompt, not just which chunks.
arXiv:2307.03172 →
BENCHMARK COLM 2024
RULER: What’s the Real Context Size of Your Long-Context Language Models?
Cheng-Ping Hsieh, Simeng Sun, Samuel Kriman, Shantanu Acharya, Dima Rekesh, Fei Jia, Yang Zhang, Boris Ginsburg (NVIDIA) · arXiv:2404.06654
A synthetic benchmark that goes well beyond simple needle-in-a-haystack. RULER mixes retrieval with multi-hop tracing, aggregation, and multi-key/value variants, all at configurable sequence lengths. Headline finding: of 17 models claiming ≥32K context, only about half actually maintain quality at 32K. The result is the empirical gap between advertised context size and useful context size, and it’s why a context engineer doesn’t take vendor specs at face value.
What changed: “1M-token context” stopped being a marketing claim accepted at face value. Buyers now ask for RULER scores at the lengths they actually plan to use.
arXiv:2404.06654 →
2025 arXiv preprint
Agentic Context Engineering: Evolving Contexts for Self-Improving Language Models
Qizheng Zhang et al. · arXiv:2510.04618 (October 2025)
Frames context as the artifact being optimised. The ACE framework runs a generator–reflector–curator loop: a generator produces reasoning trajectories, a reflector extracts insights from outcomes, and a curator merges those insights into a structured “playbook” that becomes the new context for the next iteration. Reports +10.6% on agent benchmarks and +8.6% on domain-specific tasks vs strong baselines. The clever bit is the deduplication and helpful/harmful counters — this is closer to a learned context cache than a static prompt.
What changed: the context itself can be trained without touching weights. For teams that can’t afford RL fine-tuning (Day 21), this is the next-best optimisation surface.
arXiv:2510.04618 →
A note on the literature: “context engineering” as a named discipline only crystallised in mid-2025 (Karpathy, Chase, Willison, Anthropic blog posts within ~10 days of each other). Most of the empirical work it builds on — Lost in the Middle, RULER, prompt-cache — predates the term. The naming has done useful work: it gave a label to a job that was being done badly because nobody owned it.

GitHub Pulse

Five Repos Where Context Engineering Is Actually Built

cline/cline ~58K
Open-source autonomous coding agent for VS Code (and now JetBrains, Zed, etc.).
Architectural interest: Cline’s context manager is one of the cleanest reference implementations — explicit file-level context budget, codebase summarisation on overflow, deliberate truncation strategy. Read the core/context directory before designing your own.
humanlayer/12-factor-agents ~14K
Twelve principles for production-quality LLM applications. Factor 3 is “own your context window” — a manifesto for context engineering before the term existed.
Architectural interest: the doc explicitly argues against framework abstraction over the context. The opinion is “you should be able to print your context window verbatim and explain every token in it”. That’s a useful test.
LMCache/LMCache ~5K
Open-source KV cache layer that persists cached prefixes across GPU/CPU/disk/S3 and reuses them across vLLM & SGLang instances.
Architectural interest: cross-query cache reuse moves the cache from per-request to per-fleet, claiming up to 15× throughput on multi-round QA workloads. This is what lets self-hosted stacks match Anthropic-style prompt caching economics.
NVIDIA/RULER ~1.3K
The benchmark itself. Synthetic generators for needle-in-haystack, multi-hop, aggregation, and multi-key tests at any sequence length.
Architectural interest: small repo, big leverage. Run RULER at the context lengths you actually plan to use against your candidate models — this is how you stop trusting marketing-page numbers.
ace-agent/ace actively maintained
Reference implementation of the Agentic Context Engineering paper — generator/reflector/curator loop that evolves context as a playbook.
Architectural interest: the cleanest open codebase showing “train the context, not the weights”. Worth reading even if you don’t adopt it — the abstractions clarify what context optimisation can look like.
anthropics/claude-cookbooks actively maintained
Anthropic’s official recipes, including a dedicated context-engineering notebook covering memory, compaction, and tool clearing.
Architectural interest: closest thing to a vendor-blessed playbook. The compaction recipe in particular is what Claude Code uses internally — and reading it tells you a lot about how the SDK reasons about context budgets.
Star counts are approximate snapshots from May 2026 verification searches. Star numbers move fast; treat them as orders-of-magnitude, not precise figures.

Community Pulse

What the KOLs Are Saying About Context

Andrej Karpathy · X · June 25, 2025
“+1 for ‘context engineering’ over ‘prompt engineering’. People associate prompts with short task descriptions you’d give an LLM in your day-to-day use. When in every industrial-strength LLM app, context engineering is the delicate art and science of filling the context window with just the right information for the next step.”
The post that crystallised the term. Karpathy frames context engineering as “just one small piece of an emerging thick layer of non-trivial software that coordinates individual LLM calls into full LLM apps”. Source: x.com/karpathy/status/1937902205765607626
Harrison Chase · LangChain Blog · June 23, 2025
“Context engineering is building dynamic systems to provide the right information and tools in the right format such that the LLM can plausibly accomplish the task.”
From “The rise of context engineering”. Chase’s contribution is the emphasis on dynamic systems — context engineering is not a static prompt template, it is the orchestration code that assembles a fresh context for every step. Source: blog.langchain.com/the-rise-of-context-engineering
Anthropic Engineering Blog · September 29, 2025
“The engineering problem is optimizing the utility of those tokens against the inherent constraints of LLMs to consistently achieve a desired outcome.”
From “Effective context engineering for AI agents”. Anthropic introduces “just-in-time” context (lightweight identifiers, dynamic loading at runtime) and compaction (summarise + reseed at window-fill threshold) as the two primary levers Claude Code uses internally. Source: anthropic.com/engineering/effective-context-engineering-for-ai-agents
Simon Willison · Personal Blog · June 27, 2025
Willison has argued that “context engineering” will stick where “prompt engineering” failed because the inferred meaning matches the actual work — the term reads as “the engineering discipline of context” rather than “the trivial act of typing into a chatbot.”
Paraphrased from his Jun 27 post. The notable point: prompt engineering as a job title was hurt by the public’s mental model. Context engineering is recovering the same idea under a less-degraded label. Source: simonwillison.net/2025/jun/27/context-engineering
The convergence to notice: Karpathy, Chase, Willison, and Anthropic all published within a ten-day window in late June 2025. That kind of synchronous naming usually signals a discipline that’s been quietly maturing for 18 months and just got a flag planted in it. The flag is now planted.

Platform Deep-Dive

How the Big Four Implement Context Engineering Today

Claude (Sonnet/Opus 4.x)
Anthropic
The most context-engineering-native platform. Prompt caching is first-class — cache_control markers on any block, 5-minute and 1-hour TTLs, ~10× cheaper on cache hits. Claude Code (the CLI) ships compaction as a default behaviour: at ~75–85% window fill it summarises the conversation and reseeds. The Claude Agent SDK exposes sub-agents as a primitive — a parent can delegate to a child with its own clean window. Tool-clearing (drop a tool from the schema mid-conversation) is also exposed. The cookbook’s tool-use-context-engineering notebook is the canonical reference.
GPT-5.4 / Codex
OpenAI
Automatic prompt caching on prefixes ≥1024 tokens, no opt-in needed; the cache hit rate is reported back in usage metrics so engineers can tune for it. The Responses API has shifted the surface from raw chat-completions toward a stateful “previous_response_id” pattern that does some compaction for you. Codex CLI does manage context but is less explicit than Claude Code about its strategy — the prompt-cache hit rate is the main observable signal. OpenAI’s context-engineering posture is “auto-magic by default, levers if you ask”.
Gemini 3.1
Google
The 1M-token context model is the most aggressive on raw window size, and its cached-content API lets you persist a static document set with TTL pricing distinct from per-call input. Practical reality (RULER and internal evals): quality at 1M is usable but visibly weaker than at 200K, so the context-engineering job on Gemini is paradoxically about choosing not to use the whole window. Vertex AI’s “context caching” product is the managed equivalent of LMCache.
OpenClaw
Skill / Hub Layer
OpenClaw is the most explicit just-in-time context system in production. SKILL.md files act as lightweight identifiers — the agent sees a one-line description of every available skill, and only when it decides to use one does the full skill body load into context. This is the “tool index, then JIT-load” pattern Anthropic’s blog describes, hardened as the platform’s primary distribution mechanism. The 1,184-malicious-skill story (Day 14) is partly a context-engineering story: skills are how arbitrary content reaches the agent’s window, which is exactly why their integrity matters.

The platform tell. If you want to know how seriously a platform takes context engineering, look at three signals: (1) does it expose an explicit cache control? (2) does it expose compaction or sub-agents as primitives? (3) does its docs mention “just-in-time” context? Anthropic scores 3/3, OpenAI 1/3, Google 1.5/3 (caching, no compaction), and the open-source stack is now catching up via LMCache + LangGraph + sub-agent patterns.


Vocabulary

Ten Terms to Speak Fluently

Context Engineering上下文工程
The discipline of selecting, ordering, caching, and pruning the tokens that go into an LLM call so the model can solve the task. The umbrella term for everything else in this list.
Common misuse: treating context engineering as “writing a longer prompt”. It is the opposite — usually the win is removing tokens, not adding them.
“Our latency dropped 40% after a context-engineering pass — the prompt got shorter, not longer.”
Prompt Cache提示缓存
Server-side reuse of the KV tensors computed for a prompt prefix, so identical prefixes on a subsequent call skip the prefill computation. Typically 5min–1hr TTL, ~10× cheaper on hit.
Trap: assuming any repeated text hits the cache. Most providers cache only at block boundaries you mark (Anthropic) or at ≥1024-token aligned prefixes (OpenAI). Off-by-one variability kills cache hit rate.
“Move the user message to the bottom — you’re evicting the cache by injecting it at position 2.”
KV CacheKV 缓存
The cached key/value tensors of the attention layers for already-processed tokens. The data structure underneath prompt caching; reused intra-call (during generation) and now also cross-call (LMCache, vendor caches).
Don’t equate KV cache with prompt cache. The KV cache is the tensor object; prompt caching is the API feature that lets you reuse it across calls.
“LMCache pages the KV cache to disk so we can reuse it across vLLM replicas.”
Compaction上下文压缩
Anthropic’s coined term: summarise the conversation/trajectory at a fill threshold and reseed a fresh context window with the summary. The first lever before sub-agent isolation.
Trap: thinking compaction is loss-free. Information not in the summary is gone — the engineering decision is what to keep verbatim (e.g. tool schemas, key file diffs) vs summarise.
“We hit compaction at turn 41 — the assistant kept the last three file edits verbatim and summarised the rest.”
Just-in-Time Context即时上下文
The pattern of keeping lightweight identifiers (file paths, tool names, skill summaries) in the prompt, and having the agent dynamically pull the full content of any one into the window only when it needs it.
Trap: pre-loading every tool the agent might need. This is the “dump the kitchen sink” anti-pattern that causes tool-selection accuracy to collapse past ~30 tools.
“The agent reads the skill index, calls load_skill(‘contracts’), and only then does the 4K-token contract template enter context.”
Sub-agent子智能体
A child agent invoked by a parent for a bounded sub-task, running with its own clean context window. Its outputs are returned to the parent as a single tool result, not as a transcript.
Trap: spawning sub-agents for trivial work. Each sub-agent doubles cost and latency. Use them when the sub-task would otherwise consume >~10K tokens of the parent’s window.
“Code review uses a sub-agent per file so the parent doesn’t carry 20 file contents.”
Lost in the Middle中段迷失
The empirical phenomenon (Liu et al. 2023) that LLMs underweight information placed in the middle of a long input vs. its beginning or end. U-shaped accuracy curve.
Trap: assuming bigger context windows fix it. RULER shows the U-curve persists across model scales and explicitly long-context models.
“We re-ranked the retrieved chunks so the highest-scoring one lands at position 1 and the second-highest at the end — lost-in-the-middle workaround.”
Context Rot上下文腐烂
Informal term for the gradual quality decay of an agent’s outputs as a session’s context fills with noisy tool outputs, irrelevant tangents, and contradictory state. Fixed by compaction or restart.
Trap: blaming the model. Context rot looks like “the model got dumber”; the model didn’t change — its inputs did.
“Around turn 60 we saw classic context rot — we hard-compacted and quality recovered.”
Attention Sink注意力陷阱 / 沉锚
The empirical observation that the first few tokens of an input attract disproportionate attention regardless of content (Xiao et al. 2023, “Streaming LLM”). Used to justify keeping the first tokens stable for streaming-context models.
Trap: confusing attention sink with the lost-in-the-middle effect. Attention sink is about the first tokens being privileged; LITM is about the middle tokens being penalised. Different mechanisms.
“We pin a stable BOS-like header so the attention-sink position never gets rotated out.”
Effective Context Length有效上下文长度
The longest input length at which the model still maintains acceptable accuracy on RULER-style tasks — typically much shorter than the advertised window.
Trap: confusing “1M context” (advertised) with the length the model is actually reliable at (often 128K–256K).
“Gemini’s advertised window is 1M but our RULER test put effective length at ~256K for our task.”

Expert Questions

Five Questions That Make AI Engineers Lean In

Q1
What’s the difference between prompt caching and KV cache reuse, and why does that distinction change your architecture choices?
Prompt caching is the API feature that exposes cross-call KV-cache reuse to users; the KV cache itself is the underlying tensor object reused intra-call as well. The architectural consequence: with vendor prompt caching you optimise prefix stability; with self-hosted KV reuse (LMCache/SGLang) you also optimise cache placement across replicas and storage tiers. The former is a prompt-design problem; the latter is an infra problem.
Q2
Why does your agent get worse at hour 2, and what are the three orthogonal fixes?
Context rot — the conversation has filled with stale tool outputs and the lost-in-the-middle effect is now eating your earlier instructions. Three orthogonal fixes: (1) compaction (summarise + reseed), (2) sub-agent isolation (delegate noisy sub-tasks to a child window), (3) tool-clearing (drop tool schemas the agent has stopped using). Most teams reach for compaction first; the other two are higher-leverage when applicable.
Q3
If RULER says my model degrades past 64K, why am I paying for a 200K window?
You probably aren’t getting what you’re paying for. The honest answer is: the advertised window is a billing surface, the effective window is an evaluation result. The right move is to measure RULER on your task, set a hard ceiling (say 50K), and engineer compaction to never exceed it — even on a 200K-capable model. Engineers conflating advertised-with-effective is the single most common reliability bug in long-context apps.
Q4
When does compaction beat sub-agent delegation, and vice versa?
Compaction wins when the conversation is narratively continuous and you mostly need to free space — e.g. a long debugging session. Sub-agents win when the sub-task is structurally separable and its noise would pollute the parent — e.g. exploring an unfamiliar codebase, running a search, batch-processing files. The smell test: if the parent only needs the answer, use a sub-agent; if it needs the journey, use compaction.
Q5
Walk me through the five things you’d audit in a context window before assuming the model is the bottleneck.
(1) Order — is the user’s actual question last? (2) Cache hit rate — what% of input tokens are coming from cache? (3) Tool count — how many tools are exposed and how many actually fire per session? (4) Compaction policy — at what fill % does it trigger and what does it preserve verbatim? (5) Persistent vs session vs turn separation — is anything “long-term” getting injected as turn-level context every call? Five engineering questions before any “maybe we need a bigger model” conversation.

CGO Lens

Three Decisions This Forces at botlearn.ai

01Context engineering is your COGS lever, not your research project

If a tutoring agent makes 8–15 model calls per student session and the static prefix (system prompt, pedagogy guide, current chapter) is identical across calls, then a properly-cached architecture is 5–10× cheaper than a naive one. The savings show up immediately in COGS — usually a 30–60% gross-margin improvement on the inference line. This is not optimisation theatre; it is the difference between a unit-economics-positive product and a slow bleed.

Concrete action: get your engineering team to instrument cache hit rate on every model call and report it in the weekly metrics review next to latency and cost. If the number is below 70%, there is a context-ordering bug. If it is above 90%, you’re probably over-caching stale content. The healthy range is 75–90%.

02Pedagogy is a context engineering asset, not just curriculum

botlearn.ai’s competitive moat is the curriculum. In a context-engineering frame, that moat is concretely “a static, cacheable prefix that encodes how we teach” — concept maps, common-misconception traps, our scaffolding ladder. Competitors with bigger compute can’t shortcut this because it’s domain knowledge, not engineering. The same pedagogy delivered as cached context buys: cheaper inference, faster TTFT (a measurable UX gain on student attention), and consistency across teachers. Three product wins from one architectural choice.

Concrete action: make “cacheable pedagogy package” a first-class artifact of the curriculum team, version-controlled like code. Each subject (algebra, biology, etc.) gets a stable cached prefix that the agent inherits at session start. The curriculum lead becomes, structurally, a context engineer.

03Effective context length is in your sales deck, advertised context is in your competitor’s

When a buyer asks “does your tutor handle a full course?” the honest answer is about effective context, not advertised window. RULER scores beat marketing claims. botlearn.ai can ship a number that is empirically true for tutoring tasks — e.g. “effective at 128K on our tutoring RULER, against Gemini’s 1M-advertised which degrades at 256K.” That is a defensible technical claim, and it is the kind of credibility that lets a non-technical CGO win an evaluation against a technically-confident incumbent.

Concrete action: commission a botlearn-tutor-RULER — a custom benchmark of 50–100 tutoring tasks at varying lengths. Publish it. The marketing-defensible number isn’t “1M context”, it’s “90% accuracy at 128K of real curricula”.

The CGO sentence: “In 2026 we don’t pay for tokens, we pay for uncached tokens. Get the cache hit rate to 80% and we just bought ourselves a 50% margin improvement without changing the model.”

Curriculum Tracker

17-Day Track + 5 Bonus Days — Where We Are

D01Full Agent Stack
D02Memory Architecture
D03Planning & Tool Use
D04RAG Deep Dive
D05Agent Frameworks
D06Benchmarks & Eval
D07Multi-Agent Systems
D08Computer Use Agents
D09Code Agents
D10Long-Horizon Tasks
D11Agent Safety
D12Agent Economics
D13Research Frontiers
D14OpenClaw Deep-Dive
D15A2A Protocols
D16Agentic Commerce
D17Synthesis
B18Reasoning & TTC
B19Agent Observability
B20Voice & Realtime
B21Agent RL & Fine-tuning
B22Context Engineering

Bonus arc continues. The original 17 mapped what an agent is; the bonus arc maps how it is actually built. Day 21 was the training layer. Day 22 is the runtime layer — the place where, on every single inference call, you either save or burn 30% of your unit economics.