Context and memory: what your agent knows, and when
Where to put instructions so agents follow them: CLAUDE.md levels and imports, AGENTS.md across tools, auto memory and app memory, compaction and clearing, and a short team template you can test.
On this page
Every agent session starts with an empty head. What the agent knows about your project comes from four places: files it loads at the start, files it pulls in as it works, notes it wrote in earlier sessions, and the conversation itself. Put each piece of knowledge in the right place and the agent follows your conventions without reminders. Put it in the wrong place and it gets ignored, lost at compaction, or paid for on every request.
What it is#
The context window is everything the model reads on one request: Claude Code's system prompt, your instruction files, the memory index, tool names, skill descriptions, the conversation so far, every file it read and every tool result. It has a fixed size. When it fills up, Claude Code compacts: it replaces the older conversation with a summary and keeps going.
Memory is what survives between sessions. Claude Code has two kinds:
| Instruction files (CLAUDE.md, AGENTS.md, rules) | Auto memory | |
|---|---|---|
| Who writes it | You and your team | Claude |
| What goes in | Commands, conventions, layout, "always do X" | Your preferences, your corrections, project facts it cannot read from the code |
| Where it lives | In the repository or in ~/.claude/ | ~/.claude/projects/<project>/memory/ on your machine |
| What loads | The full files, every session | The first 200 lines or 25 KB of MEMORY.md, every session |
Both are context, not configuration. The agent reads them and tries to follow them, with no guarantee. A rule that must hold every time belongs in a permission rule or a hook, which the runtime enforces whatever the model decides.
In Claude Code, each mechanism enters the context at a different moment and costs a different amount:
| Mechanism | When it enters context | Cost |
|---|---|---|
CLAUDE.md or AGENTS.md in the working directory and above | Session start | Full text on every request |
CLAUDE.md in a subdirectory | When Claude reads or edits a file in that folder | From then on |
.claude/rules/*.md without paths | Session start | Every request |
.claude/rules/*.md with paths | When Claude touches a matching file | From then on |
Auto memory (MEMORY.md) | Session start, first 200 lines or 25 KB | Every request |
| Skills | Descriptions at start, full text when used | Low until used |
| MCP servers | Tool names at start, full schemas on demand | Low until used |
| Subagents | Their own, separate context window | Only their summary comes back |
| Hooks | They run outside the conversation | Zero, unless they return text |
When to use it#
Decide where a piece of knowledge goes by how often the agent needs it and whether it must be enforced:
| The knowledge is | Put it in | Example |
|---|---|---|
| Needed every session, and short | Project CLAUDE.md or AGENTS.md | "Run pnpm test before committing" |
| Needed only for one part of the codebase | A path-scoped rule in .claude/rules/ | "API handlers validate input with zod" |
| A multi-step procedure or a reference doc | A skill | Release checklist, API style guide |
| Your own preference | ~/.claude/CLAUDE.md or CLAUDE.local.md | "Explain a diff before applying it" |
| A correction the agent should learn | Auto memory: say "remember that..." | "The API tests need a local Redis" |
| A rule that must hold every time | A permission rule or a hook | Never read .env, never push to main |
| About this task only | The prompt | "Fix the timezone bug in dates.ts" |
| A large exploration | A subagent | "Find every caller of chargeCard" |
Add to CLAUDE.md when Claude makes the same mistake twice, when a code review catches something it should have known, or when you type the same correction you typed last session. Leave out what the agent can read from the code: directory trees, dependency lists and architecture overviews cost tokens on every request and go stale.
AGENTS.md is the cross-tool file. It is plain Markdown with no required fields, contributed by OpenAI and now stewarded by the Agentic AI Foundation under the Linux Foundation, next to MCP. In a monorepo the nearest file in the directory tree wins, and your own prompt overrides any file. Support today:
| Tool | Reads | Notes |
|---|---|---|
| Claude Code | CLAUDE.md, or AGENTS.md directly when no CLAUDE.md or CLAUDE.local.md exists in the working directory or above it | Needs v2.1.277 or later. An @AGENTS.md import works everywhere |
| Codex | AGENTS.md or AGENTS.override.md, global first, then from the repository root down to the working directory | Stops adding files at 32 KiB by default (project_doc_max_bytes) |
| GitHub Copilot | .github/copilot-instructions.md, .github/instructions/*.instructions.md, and AGENTS.md, CLAUDE.md or GEMINI.md | The nearest AGENTS.md takes precedence |
| Cursor | .cursor/rules/*.mdc, plus AGENTS.md in the root and in subdirectories | Rules apply always, by file glob, by relevance or by @ mention |
| Gemini CLI | GEMINI.md by default | Add AGENTS.md with the context.fileName setting |
App memory is a separate layer. Claude, ChatGPT and Codex can remember things about you across chats. That helps with personal preferences, but nobody on your team can review it, so team rules belong in files in the repository.
Set it up step by step
Members only below this line
Get the rest of this guide free
What you get on this page:
- 1 prompt
- 1 hook
- 1 routine
Also in the members part
- Recipes
- Patterns that work
- Mistakes to avoid
- Exercises
Sources
Checked against these on 13 Oct 2026.
- Claude Code: how Claude remembers your project (CLAUDE.md, AGENTS.md, auto memory) (opens in a new tab)code.claude.com
- Claude Code: explore the context window (opens in a new tab)code.claude.com
- Claude Code: extend Claude Code (context cost by feature) (opens in a new tab)code.claude.com
- Claude Code: how Claude Code uses prompt caching (opens in a new tab)code.claude.com
- Claude Code: commands reference (opens in a new tab)code.claude.com
- Claude Code: hooks guide (re-inject context after compaction) (opens in a new tab)code.claude.com
- Claude Code: routines (opens in a new tab)code.claude.com
- AGENTS.md (opens in a new tab)agents.md
- Linux Foundation announces the Agentic AI Foundation (opens in a new tab)linuxfoundation.org
- Codex: custom instructions with AGENTS.md (opens in a new tab)learn.chatgpt.com
- ChatGPT and Codex: memories (opens in a new tab)learn.chatgpt.com
- GitHub Docs: add repository custom instructions for Copilot (opens in a new tab)docs.github.com
- Cursor: rules (opens in a new tab)cursor.com
- Gemini CLI: provide context with GEMINI.md files (opens in a new tab)github.com
- Gemini CLI: configuration reference (opens in a new tab)github.com
- Claude Help Center: chat search and memory (opens in a new tab)support.claude.com
- Claude API: memory tool (opens in a new tab)platform.claude.com
- Claude API: compaction (opens in a new tab)platform.claude.com
- Claude API: context editing (opens in a new tab)platform.claude.com