# 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.

- Canonical: https://guides-ai.pages.dev/guides/ai-coding-agent-context/
- Plate 04.02 · Topic: Workflows (https://guides-ai.pages.dev/topics/workflow/)
- Published: 02 Sept 2026 · Updated: 06 Sept 2026 · 3 min read
- Source site: guides-ai — https://guides-ai.pages.dev/

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.

```markdown
# 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:

```markdown
@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"

```text
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](/guides/claude-code-manage-context/) 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](/guides/create-claude-md-project-memory/) or
[write a Cursor rules file](/guides/cursor-rules-file/). Both are just structured
[system prompt](/glossary/#system-prompt) text, delivered automatically.

Source: [Claude Code memory documentation](https://code.claude.com/docs/en/memory).
