Give an AI Coding Agent the Right Context
CLAUDE.md, AGENTS.md and .cursor/rules: four habits that fix most bad AI code — a rules file, @file references, one example, a clean context.
On this page5 sections
Most bad output from a coding agent is missing context, not a weak model. Four habits fix nearly all of it, and none of them take more than a minute.
1. Write a rules file the agent loads every session
Every agent reads a project file at startup, but they disagree on the name. Claude Code reads
CLAUDE.md (at the repo root or .claude/CLAUDE.md) and not AGENTS.md; Cursor reads
.cursor/rules/*.mdc files and AGENTS.md. The plain .cursorrules file that older guides
recommend is no longer in Cursor’s documented formats.
# Project: my-app
## Commands
- Install: `pnpm i`
- Test: `pnpm test`
## Conventions
- TypeScript strict. No `any`.
- API calls go through `src/lib/api.ts` — never call `fetch` directly.
## Don't
- Don't touch `src/generated/`.
What it does: gives the agent your commands, your conventions and your no-go zones before it reads a single file. Keep it under 200 lines — Anthropic’s own guidance is that longer files consume more context and reduce how reliably Claude follows them.
Already have an AGENTS.md for another tool? Import it instead of duplicating it:
@AGENTS.md
## Claude Code
Use plan mode for changes under `src/billing/`.
What it does: loads the shared file at session start, then appends the Claude-specific rules.
2. Point at exact files, not “the codebase”
Fix the bug in @src/auth/session.ts using the pattern in @src/auth/token.ts.
What it does: @ triggers file-path autocomplete and hands the agent the real code, so it
reads instead of guessing where login lives.
3. Show one real example to copy
“Match the style of @src/components/Button.tsx” beats a paragraph describing your
conventions. Agents copy patterns far more reliably than they follow adjectives.
4. Clear context between unrelated tasks
A long, drifting conversation makes an agent slower and less accurate. Reset between unrelated jobs — see manage context in Claude Code for the exact commands.
Verify it worked
Run /context in Claude Code and check the Memory files list. If your rules file isn’t
there, the agent never read it and everything above is theatre. Full walkthrough:
set up a CLAUDE.md or
write a Cursor rules file. Both are just structured
system prompt text, delivered automatically.
Source: Claude Code memory documentation.