Page 4 of 13 · What the model sees

Context engineering

Anthropic defines context engineering as the set of strategies for curating and maintaining the optimal set of tokens during inference. In practice: decide what is in the window at each step, when it arrives, and what leaves. Prompt engineering, choosing the words, is now one part of it.

4 runnable samples1 prediction4 questions

Level 1 · The rules

Everything that competes for the window

The model sees one stream of tokens. Instructions, tool definitions, examples, history and tool results all share it.

Instructions
System prompt plus project files such as AGENTS.md or CLAUDE.md.

Rule: short, specific, at the right altitude: neither brittle if-else logic nor vague slogans.

Tool definitions
Every tool's name, description and schema, sent on every call.

Rule: fewer, clearer tools beat many overlapping ones.

Examples
A few canonical cases that show the expected behavior.

Rule: diverse and representative, not an exhaustive edge-case list.

History and tool results
Everything said and returned so far. Grows every turn.

Rule: the biggest consumer; prune, summarize or isolate it.

Level 1 · The rules

The context window is a budget

Every token competes for the model's attention and costs money. Prompt engineering is choosing the words; context engineering is choosing what is present at all.

python[ RUNS ]

Where the budget goes in a real session

window = 200_000loaded = {    "system prompt": 3_000,    "tool definitions": 12_000,    "AGENTS.md": 2_500,    "conversation so far": 95_000,    "tool results": 38_000,}used = sum(loaded.values())print(f"used {used:,} of {window:,} = {used / window:.0%}")biggest = max(loaded, key=loaded.get)print("biggest consumer:", biggest)

[ OUTPUT ]recorded · Python 3.14.8

Press Run to see the output this code printed when the course was built.

python[ RAISES ContextOverflowError ]

Loading everything up front fails

class ContextOverflowError(Exception):    passdef load(context, doc_tokens, window=200_000):    if sum(context) + doc_tokens > window:        raise ContextOverflowError(f"{sum(context) + doc_tokens:,} tokens > {window:,}")    context.append(doc_tokens)context = [3_000, 12_000, 150_000]load(context, 60_000)

[ TRACEBACK ]recorded · Python 3.14.8

Press Run to see the output this code printed when the course was built.

Level 2 · Why it works

Just-in-time context and progressive disclosure

Load the summary always, the detail only when the task needs it. This is how skills work: a one-line description sits in context; the full playbook loads when the task matches.

python[ RUNS ]

Progressive disclosure: what a skill costs before and after it is needed

skills = {    "release": {"description": 40, "body": 2_400},    "pdf-forms": {"description": 35, "body": 3_100},    "db-migrations": {"description": 45, "body": 1_800},}def context_cost(active):    always = sum(s["description"] for s in skills.values())    on_demand = sum(skills[name]["body"] for name in active)    return always + on_demandprint("idle session:", context_cost([]), "tokens")print("migration task:", context_cost(["db-migrations"]), "tokens")print("everything up front:", context_cost(list(skills)), "tokens")

[ OUTPUT ]recorded · Python 3.14.8

Press Run to see the output this code printed when the course was built.

Front-loaded

one long rules file read at start

  • Simple to write
  • Costs tokens every session
  • Rules fade as context grows
  • Compaction may drop them

Just-in-time

skills, hooks that inject, retrieval

  • Small base context
  • Rule arrives at the moment it matters
  • Needs good descriptions to trigger
  • Critical rules still belong in hooks

Level 2 · Why it works

AGENTS.md and Agent Skills: the open standards

Two file conventions now carry context across tools. Both became open standards in late 2025, so what you write works beyond one vendor.

AGENTS.mdAgent Skills (SKILL.md)
AnswersHow this project works: build, test, conventionsHow to do one kind of task
LoadedEvery sessionName and description at startup; full body only when a task matches; bundled files only when referenced
ShapeOne Markdown file in the repoA folder with SKILL.md (frontmatter plus instructions), optional scripts and references
OriginReleased by OpenAI in 2025, now stewarded by the Agentic AI Foundation under the Linux FoundationDeveloped by Anthropic, published as an open specification at agentskills.io
CostPaid on every turn: keep it shortNear zero until used: libraries can grow

Level 2 · Why it works

Compaction, memory and caching

Compaction
Summarizing older turns to free space. Lossy by design.

Rule: anything critical lives in a file, not only in chat.

Memory
Facts saved to files and reloaded in later sessions.

Rule: durable decisions go into versioned files the agent reads.

Prompt caching
The provider reuses an unchanged prompt prefix, so repeated calls are cheaper and faster.

Rule: stable content first, changing content last.

python[ RUNS ]

Why prompt order decides what can be cached

def cacheable_prefix(parts):    stable = 0    for kind, tokens in parts:        if kind != "stable":            break        stable += tokens    return stablegood = [("stable", 3_000), ("stable", 12_000), ("changing", 900)]bad = [("changing", 900), ("stable", 3_000), ("stable", 12_000)]print("stable first:", cacheable_prefix(good), "tokens cacheable")print("changing first:", cacheable_prefix(bad), "tokens cacheable")

[ OUTPUT ]recorded · Python 3.14.8

Press Run to see the output this code printed when the course was built.

Level 3 · Mastery

Four moves when the window fills

Long tasks outgrow any window. Seniors reach for these in roughly this order:

"Old tool output is drowning the current step."

Clear or prune

Drop raw results the agent has already used; keep its conclusions.

"The session is long but the thread still matters."

Compact

Summarize older turns; lossy, so write key decisions to files first.

"Progress must survive across sessions."

Structured notes

The agent keeps a progress file or task list outside the window and rereads it.

"Exploration will read thousands of lines."

Subagent

Do the noisy work in a separate clean window; return only a condensed summary.

Predict the output

Level 1 answer before you look

Check your understanding

Levels 1 to 3 open a question to see the answer

What is the difference between prompt engineering and context engineering?

Prompt engineering is the wording of instructions. Context engineering decides which tokens are in the window at each step, and when they arrive. Prompt engineering is one part of it.

What is context rot, and what does it imply?

As the window fills, the model gets worse at using what is in it. So a bigger window is not a free win: keep the context lean, and compact, prune or isolate before quality drops.

A rule is needed by only one task in ten. AGENTS.md or a skill?

A skill. AGENTS.md is paid on every turn; a skill costs only its short description until the task matches.

How should you order a prompt for caching?

Stable content first (system prompt, tool definitions, long reference docs), changing content last. The cache only covers an unchanged prefix.