home
›
cheatsheets
›
ai_assisted_development
›
context_engineering
Free to read Every card and diagram, and Quiz me
2
Free account Star cards. Your Quiz me marks are kept, so next time you drill just what you missed
3
Pro All 49 answers on this sheet, My sheet across every topic, and PDFs
A free account keeps your stars and Quiz me marks. Pro adds all 49 answers on this sheet, My sheet across every topic, and PDFs.
/
Read
Quiz me
Comfortable
Compact
My misses 0
Click any code to copy it
Done 0 /0
To review 0
On this sheet
The model
01 The context window02 Rules & prompts03 Rules files by tool04 Long sessions & subagents05 Finding the right code06 Docs, MCP & skills07 AI-legible code08 Rules for teams & monorepos09 Secrets & prompt injection10 Cost, caching & evals
The model A model knows only what's in its context window for this request. Rules, tool definitions, chat history, files and tool output all share that space. The window is a budget . Every token costs money and attention, and accuracy drops as the window fills, even before it hits the limit. Standing context like rules files and tool definitions goes out with every request, so keep it short and scoped. Load everything else when the task needs it .Long sessions fill up with stale context. Compaction swaps the history for a lossy summary, so keep the plan and decisions in a file outside the chat. Treat rules files as shared code that you review and prune. Treat fetched text as untrusted input , and keep secrets out of all of it. The agent resends everything it has read on every turn. Subagents keep their reading out of the window, and a task file survives compaction.
The context window what the model sees, and what fills it What context engineering is Say how it differs from prompt engineering It shapes everything the model sees in a task. The prompt is one part of that.
Say what a context window holds Rules, history, files, tool output and the model's own reply.
Say what happens as the window fills The tool drops or summarizes old turns, and details get lost.
Trap A better prompt can't fix missing context. If the model can't see the current interface, it guesses.
What uses up tokens Name what's sent with every request The system prompt, rules files and tool definitions.
Name what fills the window fastest Big test logs, generated files and broad search results.
Keep only the end of a long test log npm test 2>&1 | tail -n 40
Tip You pay for repeated content on every request, unless the prompt cache covers it.
Why bigger windows aren't enough Define context rot Accuracy and recall drop as the token count grows.
Decide what a big window should hold A chosen working set, not the whole repo.
Trap More tokens cost more and often add latency. Stale or conflicting content still causes wrong decisions.
Rules & prompts what the agent loads each session, and what you add per task What goes in a rules file # acme-api ## Commands - Test one file: npm test -- <path> - Finish with: npm run lint && npm test ## Architecture - routes/ -> services/ -> models/ ## Ask first - New dependencies, migrations, infra/
Say what problem a rules file solves It saves you repeating stable facts in every prompt.
List what belongs in a rules file Commands, key folders, boundaries, naming rules, approved libraries and risky actions.
List what stays out of it Secrets, customer data, one-off notes, long tutorials and advice like "write good code".
Tip Link to longer docs instead of copying them in. Aim for a map and a policy guide, not a handbook.
Keep rules lean Say why a long rules file hurts It costs context on many requests and buries the key rules.
Handle a rule a linter already enforces Delete it, unless the agent needs it to choose an approach.
Know when to revisit the file After the agent repeats a mistake or the architecture changes.
Trap Conflicting rules can make the agent follow the wrong one, or waste turns reconciling them.
Rules file, docs or prompt Place a test command every task uses The rules file. It's stable and needed on every task.
Place the reason behind a subsystem boundary An architecture doc, linked from the rules.
Place a limit for this task only The prompt, with the task's goals and acceptance criteria.
Tip Ask how long it stays true, how often it's needed and who needs it.
Trap Don't copy guidance into two layers. The copies drift apart.
Show, don't tell Get new code in your project's style Attach one good handler or test and say "follow this pattern".
Stop the agent copying values blindly Say how the new case differs from the example.
Tip Use prose for hard rules and the example for structure.
Trap A poor example spreads its flaws. Pick a current one that shows the standard.
Long sessions & subagents keep a long task on track as the window fills Start fresh or keep going Know when to start a fresh session The goal changed, an approach failed, or the agent cites stale context.
Clear the conversation in Claude Code /clear
Write a handoff before you restart State, decisions, changed files, test results, next steps and risks.
Tip Put the handoff in the new prompt or a task file. The agent can't recall details a summary dropped.
Compaction and long tasks Say what compaction does It swaps a long history for a short summary to free space.
Compact now and say what to keep /compact keep the failing test names
Name what a summary can drop Exact errors, subtle constraints, and why an approach was rejected.
Check the agent after compaction Have it restate the plan, check it against the spec and re-read key files.
Keep a long task coherent Keep the goal, decisions and next step in a file outside the chat.
Trap If a summary drops why an approach failed, the agent tries it again. Write the reason down.
Subagents and handoffs Say how subagents save context They explore in their own windows and return only short findings.
Define a Claude Code subagent .claude/agents/tester.md
Brief an agent you hand work to A bounded task, its inputs, what it may do and the output format.
Say what a handoff should return Findings with file references, tests run, assumptions and risks.
Trap Parallel subagents can duplicate work or assume different things. The main agent compares them and owns the result.
Context poisoning Define context poisoning Wrong, stale or malicious content that steers later actions.
Spot a poisoned session It reuses a removed API or cites a decision nobody made.
Fix a poisoned session Remove or correct the bad source. Start fresh if the error has spread.
Tip Prefer authoritative sources, label untrusted content and check key claims in code or docs.
Finding the right code how the agent decides what to read Grep or semantic search Exact Fuzzy Fresh
rg -n "retryPayment" Find a known name, route or error text
Semantic search over an index. Find code by what it does, not its name
yes no the index can lag the code
Tip Use semantic search to orient and exact search for names, then read the source.
Trap Search results guide you. They don't prove what the code does now.
Repo maps Say what a repo map holds Files, key symbols and signatures, but no function bodies.
Give Aider's repo map more room aider --map-tokens 2048 The default is 1k tokens.
Say how Aider fits the map in its budget It ranks files by their dependency graph and sends the top ones.
Trap A map is only an index. The agent still reads the exact source before it edits.
Semantic index tradeoffs Name what it costs Upkeep, stale or near-miss results, and code sent to another service.
Set one up safely Set exclusions and access first, then test it with known queries.
Tip It finds related code when the names differ, and saves sending whole folders.
Trap Chunking can split a definition from the context it needs.
Explore an unfamiliar repo Orient before searching Read the docs, folder tree and build files, then list likely modules.
Narrow to one code path Its interface, code, nearest tests and one caller per boundary.
Settle a clash between sources Current source and tests beat comments and old design notes.
Tip Before the first edit, have the agent sum up its evidence, assumptions and test plan.
AI-legible code a codebase the agent can read and check AI-legible repos Say what makes a repo AI-legible Clear names, small modules, typed interfaces, current docs, reliable tests and standard commands.
Name what makes an agent guess Hidden setup, duplicate patterns, vague names and stale docs.
Say what a README gives the agent A starting map of the project, its layout and how to run checks.
Tip A README tells the agent where to look. It doesn't replace reading the source.
Types, names and tests Say how types help an agent They spell out the contract, and the compiler flags a breaking change.
Say why small modules help They fit the window, and search finds the owning file.
Name things so search works Use the same domain terms in code, tests and docs.
Write tests that teach an agent Focused, clearly named, deterministic and tied to public behavior.
Trap Static types don't check incoming data. Validate untrusted input at runtime.
Rules for teams & monorepos keep shared rules accurate as teams and code grow Keep shared rules accurate Say how rules files go stale The code, commands or paths change and the file doesn't.
Keep a rule in step with the code Change it in the same PR as the workflow it describes.
Catch a stale command automatically Run the documented commands in CI and check the links.
Decide what goes in Git Commit shared rules, but not secrets or personal preferences.
Review a rule change Say why it's needed, which tools read it and how you tested it.
Tip Delete an obsolete rule. Don't add exceptions around it.
Trap Agents follow these files confidently, so one stale rule can break every task that uses it.
One source for many tools Pick the shared source AGENTS.md Cursor and Copilot read it, and CLAUDE.md can import it.
Serve a tool that can't read it Generate its file, or add a thin file that points to the source.
Fail CI when a generated copy drifts git diff --exit-code Run it after regenerating the files.
Trap The same words can act differently in each tool. Test sample tasks in every one.
Monorepos and many teams Start a task in one package Load its public interfaces, its tests and one approved example.
Test one package, not the whole repo pnpm --filter api test
Govern context across teams Central policy for tools and secrets. Teams own their folders' rules.
Tip Check links, commands and forbidden content in CI, and give teams templates and size limits.
Trap A team's rule must never weaken a central control.
Secrets & prompt injection keep keys out, and treat outside text as hostile Keep secrets out of context Say how a secret in a rules file leaks Through Git history, logs, chat records, indexes or a provider breach.
Show the agent a credential safely Name the env var and show a placeholder, never the value.
Keep a file out of Cursor's AI features .cursorignore
Give an agent credentials Short-lived and least-privilege. The model never sees the value.
Handle a secret that reached the context Rotate it first. Then find where it may be stored.
Trap Deleting the line doesn't delete the secret. Copies stay in history, logs and chat records.
Prompt injection Name where injected text comes from Rules files, retrieved docs and web pages, and MCP tool results.
Enforce security outside the prompt Least-privilege tools, a sandbox and limited network access.
Gate risky actions Require approval for writes and data export.
Tip The lethal trifecta is private data, untrusted input and a way to send data out. Never give one agent all three.
Trap Assume the model may follow hostile text. Text must never grant itself permission.
Cost, caching & evals spend tokens well, and prove a change helps Audit and budget tokens Audit where a session's tokens went Split them into standing context, conversation and work output.
See what fills Claude Code's window /context
Get exact counts from the API Read the usage field on each response.
Trap A big token count isn't waste if the work comes out right. Judge cost next to success and retries.
Prompt caching Order context for the cache Stable parts first, like tools and rules. The task goes last.
Say what breaks the cache Any change. Everything after it is processed again.
Recall Anthropic's cache numbers Entries last 5 minutes by default. Cached reads cost a tenth of the base input price.
Trap A cache isn't a privacy boundary. Sensitive data needs the same protection as before.
Prove a context change helps Build the test set Versioned tasks from real work, with a start state and expected result.
Compare fairly A baseline against one change at a time, several runs each.
Measure each run Success, retries, stray edits, tokens, latency and review time.
Pick what to fix first The biggest repeated failures, like setup, test commands and docs.
Tip One study found AGENTS.md files didn't generally raise success, but added over 20% to cost.
Trap Add an index, MCP server or skill only for a measured gap, with an owner to maintain it.
Nothing matches. Try a shorter word, or clear the search.
Practice Guided practice One question at a time, with the ones you miss coming back.
Practice 49 interview questions Full answers, from beginner to expert.
Found this useful? Pass it on.
Share on LinkedIn
Share on X
Copy link
This sheet stays free. With Pro , you can:
Every answer on this sheet. Read the full answer to all 49 questions it links, not just the 7 free ones.
My sheet. The cards you star and the ones you miss in Quiz me, from every sheet, on one page to quiz and print.
PDFs. Download this sheet and My sheet, laid out for A4, with every question a link to its answer.
The rest of Pro. Every answer in the question library, and 1,000 AI credits a month.