Lesson 12 of 40AI coding tools
Context Engineering
Decide what an AI tool sees, write a short project memory file that works, and keep sessions clean so answers stay sharp.
A model only knows two things: what it learned in training, and what is in its context right now. It does not remember yesterday's session, it has never seen your team's conventions, and it cannot guess that npm test is slow but npm run test:unit is fast. Everything it needs to know about your project has to arrive in the context window. Context engineering is the skill of choosing what goes in, what stays out, and when to start fresh. It matters more than clever prompting, because the best prompt in the world cannot fix a model that is missing the key fact, or drowning in irrelevant ones.
What the model actually sees
On every turn, a coding agent assembles a stack of text and sends it to the model. The Tokens and Context lesson covers the window itself; here is what fills it in a tool like Claude Code.
The tool supplies this part. You mostly do not control it.
More context is not better. As the window fills with noise (old attempts, giant files, unrelated logs), quality drops: the model is more likely to miss an instruction or latch onto something stale. That is called context rot. The goal is the smallest set of information that lets the model do the task well.
The project bible: CLAUDE.md and friends
Every major AI coding tool reads a Markdown file of project instructions automatically:
| Tool | File |
|---|---|
| Claude Code | CLAUDE.md (or .claude/CLAUDE.md) |
| GitHub Copilot | .github/copilot-instructions.md, and it also reads AGENTS.md and CLAUDE.md |
| Many other agents | AGENTS.md at the repo root |
The idea is the same everywhere: write down what a skilled new teammate would need on day one, once, so you never have to repeat it in a prompt. Run /init in Claude Code to generate a starter file from your codebase, then cut it down.
Put in:
- Commands: how to build, test one file, lint, typecheck. Exact, copy-pasteable.
- Conventions that differ from the defaults: "named exports only", "money is integer cents".
- Architecture in brief: where things live, and which folder owns what.
- Gotchas: the things that have bitten people. "The dev server needs
.env.local; copy from.env.example." - How to verify: what "done" means, so the agent checks its own work.
Leave out:
- Things the model can see by reading the code, like listing every file.
- Generic advice like "write clean code" or "be careful". It costs tokens and changes nothing.
- Long explanations, tutorials and history. Link to a doc instead.
- Anything that changes weekly, like current tasks or sprint goals.
- Secrets. Ever.
An example CLAUDE.md
Here is a strong project memory file for a small Next.js shop. Notice how short and specific every line is.
# Acme Shop
Next.js 16 (App Router) + TypeScript strict + Postgres via Prisma.
Payments through Stripe. Deployed on Vercel.
## Commands
- Dev server: `npm run dev` (needs `.env.local`; copy `.env.example`)
- Test one file: `npm test -- src/lib/cart.test.ts` (prefer this; the full suite takes 4 min)
- Full check before committing: `npm run check` (lint + typecheck + unit tests)
- DB schema change: edit `prisma/schema.prisma`, then `npx prisma migrate dev --name <change>`
## Architecture
- `src/app/` routes. Server components by default.
- `src/lib/` business logic. Pure functions, unit tested. No React here.
- `src/components/ui/` shared primitives. Check here before creating a new component.
- Payments: all Stripe calls go through `src/lib/payments.ts`. Never call Stripe elsewhere.
## Conventions
- Money is integer cents (`priceCents: number`). Never use floats for money.
- Named exports only. No default exports except Next.js pages and layouts.
- Validate all request bodies with the zod schemas in `src/lib/schemas.ts`.
- Tests sit next to the file: `cart.ts` and `cart.test.ts`.
## Gotchas
- `src/generated/` is generated by Prisma. Never edit it by hand.
- Webhook tests need `STRIPE_WEBHOOK_SECRET=test` in the environment.
- Dates are stored in UTC; format them only in components.
## Definition of done
- `npm run check` passes.
- New logic in `src/lib/` has tests, including empty and invalid inputs.
- No new dependency without asking first.
## More detail
- Data model and flows: @docs/architecture.mdEvery line passes one test: would the agent likely get this wrong without it? "Never use floats for money" passes. "Write readable code" does not.
The last line uses an import: @docs/architecture.md pulls that file into memory too. Use imports to keep the main file short while still pointing at deeper material. Be aware that imported files also load every session, so import only what is worth that cost.
Where memory files live
Claude Code loads memory from several places and combines them.
- ~/.claude/
- CLAUDE.mdyou, every project
- my-app/
- CLAUDE.mdteam rules, committed
- CLAUDE.local.mdyou, this project, gitignored
- docs/
- architecture.mdimported with @docs/architecture.md
- src/
- payments/
- CLAUDE.mdloads when Claude works in this folder
- .claude/
- settings.jsonpermissions, not instructions
~/.claude/CLAUDE.md: your personal preferences for every project, like "explain your plan before editing"../CLAUDE.md(or./.claude/CLAUDE.md): the team's file. Commit it and review changes to it like code../CLAUDE.local.md: your personal notes for this one project. Gitignore it.- Subfolder CLAUDE.md files load when Claude works in that folder. Put payment-specific rules in
src/payments/CLAUDE.mdand they only cost tokens when they are relevant.
Edit any of them with /memory, or just say "add to CLAUDE.md that we use pnpm". Claude Code also keeps its own auto memory of things it learns about the project. Organizations can set a managed policy file that applies to everyone.
Match each instruction to the best place for it.
Every line costs, every turn
Memory files are loaded into context every session, and resent on every turn, because the model is stateless. A bloated file is not a one-time cost. Run this to see the difference over a 40-turn session (it uses a rough rule of about four characters per token):
Press Run (⌘ Enter) to see what happens.Caching can reduce what you pay for repeated text, but the space in the context window is used either way, and so is the model's attention. A short file whose every line matters beats a long one the model has to sift through.
Managing context during a session
Your memory file is the stable part. The conversation is the part that drifts. Four habits keep it clean:
- Point, do not paste. Say "look at
src/lib/cart.tsand its test" instead of pasting both files. The agent reads what it needs, and a large file read by a subagent can stay out of your main context entirely (the Subagents and Orchestrators lesson covers this). - One task per session. Finished the login bug and moving on to the export feature? Run
/clear. The login transcript is now noise. - Compact long sessions. If a task genuinely needs a long session,
/compactsummarises the conversation so far. Give it a focus, like/compact keep the failing test names and the plan, so the important details survive. - Look before you guess.
/contextshows what is filling the window. If tool output is taking up most of it, you know why answers are getting worse.
Pointing at the right files
"Point, do not paste" sounds simple, but where you point matters. Compare two prompts for the same bug:
Weak: The checkout total is wrong sometimes. Fix it.
Strong: The checkout total is off by one cent for some carts.
The total is calculated in src/lib/cart.ts (calculateTotal).
Discounts are applied in src/lib/discounts.ts.
Reproduce it with a test in src/lib/cart.test.ts first,
using a cart of three items at 3.33 with a 10% discount.The strong prompt does not paste any code. It names the two files that matter, the function, a reproduction, and where the test should go. The agent reads less, guesses less, and has a way to check its fix. The weak prompt makes it search the whole codebase, reading files that then sit in context for the rest of the session.
One file for several tools
If your team uses more than one AI tool, you do not want three instruction files drifting apart. Copilot reads AGENTS.md and CLAUDE.md. Claude Code reads CLAUDE.md, which can import other files. So one option is to keep the real content in AGENTS.md and make CLAUDE.md a single line, @AGENTS.md, plus any Claude-specific notes. Check each tool's docs for what it reads, since this changes.
Put a well-run task session in order.
- Tap the steps below in the right order.
- context engineering
- Choosing what information a model sees for a task, and keeping out what it does not need.
- context rot
- The drop in answer quality as a context window fills with irrelevant or stale text.
- project memory
- An instructions file like CLAUDE.md or AGENTS.md that an AI tool loads at the start of every session.
- import (CLAUDE.md)
- An @path/to/file.md line that pulls another file into Claude Code's memory.
- CLAUDE.local.md
- A gitignored memory file for your personal notes on one project.
- /compact
- A Claude Code command that summarises the conversation to free up context space.
- /clear
- A Claude Code command that starts a fresh conversation, keeping project memory.