AI Coding Tools

What Is Context Engineering? A Guide for AI Coding (2026)

Aug 14, 202613 min read

Context engineering is the discipline of designing and managing the entire set of tokens a model sees at each inference - system prompt, tools, code, conversation history, and memory - so the AI produces correct output. Unlike prompt engineering (which only tunes the wording of a single instruction), it manages the whole information "window." For AI coding, output quality depends more on context than on the model. Four foundational strategies: Write, Select, Compress, Isolate.

What is context engineering?

Context engineering is the discipline of designing, curating, and managing the entire set of tokens a language model sees at each inference call, so it has enough - and only enough - of the information it needs to finish the task. That token set has several layers: the system prompt, tool descriptions, the code files you hand it, conversation history, tool results, and long-term memory (for example a CLAUDE.md file).

Put simply: prompt engineering asks "how do I word this instruction well?" while context engineering asks "what exactly do I load into the context window, at what point, and what do I leave out?" Anthropic describes this as a natural progression once agents run many loops and accumulate more and more data (see Effective context engineering for AI agents, published 2025-09-29).

For AI coding, this is the crux: same model, same request, but if you load the wrong files, let history balloon, or forget your project conventions, the generated code drifts off spec. The output quality of a coding agent depends more on context than on the model. That is why a developer who knows how to curate context tends to get more out of the exact same tool a teammate is already using.

Context engineering vs prompt engineering

A lot of people use these two terms interchangeably. Prompt engineering is not obsolete - but it is now one layer inside context engineering. A prompt is how you word a specific instruction; context is the entire information environment surrounding that instruction.

CriterionPrompt engineeringContext engineering
Core questionHow do I word this instruction effectively?What do I load into the context window, when, and what do I drop?
ScopeOne instruction / one turnThe whole token set across many turns and agent loops
PersistenceUsually one-offDurable: memory and conventions applied to every request
Cost of getting it wrongOne bad answerA tax on every request, for months
Example in coding"Refactor this function to be shorter"Structuring CLAUDE.md, choosing which files to load, compressing history, splitting off a subagent

This line is worth pinning to the wall: a bad prompt costs you one bad answer; a bad line in CLAUDE.md is a tax on EVERY request for months. That is exactly why context engineering has far more leverage - you fix one spot and thousands of later sessions benefit. If you want to write your project conventions file properly, see the guide on writing a solid CLAUDE.md for Claude Code.

Why context matters for AI coding (context window & token budget)

Every model has a context window - a limit on how many tokens it can "see" at once. The Claude models currently used in Claude Code have a window of roughly , equivalent to a few hundred pages of text. That sounds like a lot, but in real work it drains faster than you would expect.

Two phenomena explain why a "big window" does not mean "good memory":

  • Attention budget runs down: every token you add dilutes the model's attention. The more you cram in, the harder it is for the model to stay locked onto the details that matter. Context is a scarce resource, not an unlimited warehouse.
  • Context rot / "lost in the middle": information sitting in the middle of a long conversation is easier for the model to miss than information at the start or end. This is why a coding agent so often "forgets" something you said dozens of turns ago, or answers incorrectly after a long session.

A number you can measure yourself: if the "always-on" configuration (system prompt, tool definitions, memory) already takes up ~10-20% of the window, the rest is what is left for code, history, and tool output. Once total usage climbs past most of the window, quality starts to slip - that is when you need to compress or reset. Curating context is precisely the work of keeping what actually matters inside the budget.

A practical mental model: treat the context window like a desk, not a filing cabinet. However wide the desk is, it eventually gets crowded, and once you pile everything onto it, finding the one document you need slows down. The model works the same way - it does not "read every token carefully and evenly," it allocates attention. This is why a focused session with just the few files you actually need usually yields higher-quality code than a "dump the whole repo and ask" session. For AI coding, the discipline of keeping your desk tidy matters as much as picking the right model.

The 4 context engineering strategies (Write, Select, Compress, Isolate)

The community (LangChain, LlamaIndex, and many others) has converged on a compact mental framework of four strategies. This is the easiest set to remember when applying context engineering to your day-to-day coding workflow.

Write - offload to the outside

Instead of stuffing everything into context, push information out: write plans, notes, and decisions to a file (for example NOTES.md, a scratchpad, or durable memory like CLAUDE.md). Context only keeps a pointer to the information and reloads it when needed.

Select - pull in only what you need

Treat context as scarce: load only the exact files or snippets the current task needs instead of dumping the whole repo. This is the spirit of retrieval (RAG), of the @file syntax, and of just-in-time retrieval - fetch data the moment you need it rather than preloading it.

Compress - summarize

Summarize long conversation history and tool output before keeping it. Anthropic calls this technique compaction: when context is about to fill up, condense what you have done into a short summary and continue, instead of dragging the entire conversation tail along.

Isolate - split it off

Move a side task (research, review, lookup) into a separate subagent that has its own clean context. The subagent finishes and returns only a tight summary to the orchestrating agent - the messy details never pollute the main context. Example: when you need to learn an unfamiliar library, hand it to a subagent to read the docs and return "the 3 functions to use plus how to call them," rather than pulling the whole doc page into your main context.

These four strategies are not mutually exclusive - in a real session you usually use all four: write the plan to NOTES.md (Write), load only the file you are editing (Select), compress when history gets long (Compress), and offload research to a subagent (Isolate). Once you internalize this framework, you start seeing every Claude Code feature through the lens of "which context strategy does this serve?"

StrategyIdeaMatching Claude Code technique
WriteOffload outside, keep context lightCLAUDE.md, NOTES.md, plan mode
SelectLoad exactly what is needed@file, MCP, just-in-time retrieval
CompressCompress history / output/compact
IsolateSplit off work, return a summarySubagents

Context engineering techniques in practice with Claude Code

This is the most important part: mapping the four strategies above onto the tools you use every day in Claude Code. The strength of Claude Code is that each feature maps back to a context strategy - so instead of memorizing commands, learn to ask "what context problem does this command solve?"

  • CLAUDE.md = durable memory (Write). This file holds project conventions, repo structure, do's and don'ts. It loads into every session, so this is where the highest leverage lives - write it right once, benefit on every request.
  • /compact and /clear (Compress + reset). When context is nearly full, /compact compresses history into a summary; /clear wipes it clean to start fresh when you move to a completely new task.
  • /context (measurement). This command shows a breakdown of the tokens in use - you can only optimize what eats tokens once you can see it.
  • Subagents (Isolate). Hand research/review work to subagents so they "burn" their own context and return only the result. See the guide to using subagents in Claude Code.
  • MCP and @file (Select). Load data just-in-time from external sources instead of preloading it. If MCP is new to you, read what is MCP.
  • Plan mode / a NOTES.md scratchpad (Write). Keep the long plan outside context; the agent only references it when needed.

A short, pragmatic CLAUDE.md example - enough to keep the agent on spec without burning too many tokens:

# CLAUDE.md

## Project
Booking API, Node.js + Fastify + PostgreSQL (Prisma).

## Structure
- src/routes - endpoint definitions
- src/services - business logic
- src/db - Prisma schema & migrations

## Conventions (DO)
- Validate input with zod at the route layer
- Every query goes through a service; never call Prisma directly in a route
- Commit with Conventional Commits

## Avoid (DON'T)
- Do not add a new dependency without asking first
- Do not edit a migration file that has already been merged
- Do not log user data to the console

Managing the token budget: measure & optimize

Context engineering is not a gut-feel activity - it is a measure-then-optimize loop:

  1. Read /context to see where tokens are allocated: system, tools, messages, files, output.
  2. Spot the token hogs: usually long tool output (logs, test results), a big file loaded whole, or a bloated conversation history.
  3. Treat the cause: compress (/compact) when history is long; load selectively (@file instead of a whole directory) when a file is big; split off a subagent when a side task is too noisy.
  4. Know when to open a new session: when you truly switch tasks, /clear or a fresh session beats dragging old context around.

One field-tested habit: get into the routine of glancing at /context at the start and middle of every long session, especially right after a command that returns large output (build, test, logs). Very often the token hog is not the code you are editing but a pile of logs pasted in from a previous run. Catch it early and a single /compact or a fresh session frees most of the budget back up.

Why is it worth it? Tokens are money and latency. The leaner your context, the lower the cost per turn and the faster the response - and, just as important, the answer quality improves because the model is not distracted. If you care about the cost side, see the article on Claude Code cost & token optimization to connect token counts to your actual bill.

Common context engineering mistakes

Honestly, most "dumb" coding-agent errors come from broken context, not a weak model. Recognizing the failure type helps you fix it fast instead of blaming the tool. Four common patterns:

  • Context poisoning: a piece of wrong information (a false assumption, an old decision that has since changed) gets stuck in context and keeps steering the agent off course. Prevention: fix it or /clear as soon as you notice, do not let the agent stay "confident on a bad foundation."
  • Context distraction: too much irrelevant information dilutes the model's attention. Prevention: load selectively, keep context minimal and sufficient.
  • Context clash: two conflicting instructions coexist (for example CLAUDE.md says use library A while the conversation asks for B). Prevention: keep the source of conventions consistent, update memory when you change direction.
  • Context rot: quality degrades as the conversation gets too long. Prevention: /compact at the right time, lock results to a file, then refresh the session.

Tools & kits that support context engineering

Most of the techniques above you can do entirely by hand: write CLAUDE.md, type /compact, spin up a subagent. You do not need to buy anything to get started. Beyond that, there are prebuilt skill/subagent bundles that package these patterns - memory, compaction, isolate - so you do not have to build them from scratch, for example the AgentKit (20% off via link) bundle for Claude Code (the ak CLI; note this is a different product from OpenAI's AgentKit). If you are curious how these kits package context patterns, read the AgentKit review. And if you want to go deeper specifically on memory and context, see managing context & memory in Claude Code.

Frequently asked questions (FAQ)

How is context engineering different from prompt engineering?

Prompt engineering tunes a specific instruction for a single turn. Context engineering manages the entire token set across many turns - system prompt, tools, code, history, memory. Prompt engineering is now one layer inside context engineering.

Do I need to know how to code to learn context engineering?

Not for the concept, but very much so if you want to apply it to AI coding. The most effective techniques (writing CLAUDE.md, choosing which files to load, compressing history, splitting off subagents) are all tied to a real programming workflow.

How many tokens is Claude Code's context window?

The Claude models in Claude Code have a window of roughly a few hundred thousand tokens (verify the exact number in the official Claude Code documentation before relying on it, since limits and models change fast). More important than the number is knowing how to measure with /context and keep what matters inside the budget.

When should I use /compact?

When the conversation history has gotten long and context is nearly full but you still want to continue the same task. /compact compresses the conversation into a summary to keep your working thread. If you are switching to an entirely different task, use /clear or open a new session instead of compressing.

Is CLAUDE.md context engineering?

Yes, it is a textbook example of the Write strategy: you record project conventions in a durable file to load into every session, rather than repeating the instruction each time. Writing a good CLAUDE.md is one of the highest-leverage context moves there is.

Does context engineering replace RAG?

No - RAG (retrieval) is itself part of the Select strategy within context engineering. Context engineering is the broader framework, while RAG is one specific technique for loading the right information into context.

Conclusion + next steps

Context engineering is the foundational skill for effective AI coding: it is not about "which model is strongest" but "how well you curate context." Master the four strategies - Write, Select, Compress, Isolate - measure with /context, and catch the four context failure types early, and that is how you turn a coding agent from "forgetful" into trustworthy. Read on with managing context & memory in Claude Code to go deeper on memory techniques, and Claude Code cost & token optimization to connect context to cost. If you are just getting started with working alongside AI, take a look at what is vibe coding and how to avoid AI slop.

J

Jasmine

Author · Jasmine Daily

The writer behind Jasmine Daily - jotting down thoughts, experiences, and everyday moments. Honest, unhurried, imperfect.

Jasmine Daily

There's more waiting to be read.

If this piece spoke to you, browse a few more pages from the journal.

Read next

Related posts