GuideAgentic ways of working · Beginner

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.

Checked 13 Oct 202615 min read1 prompt1 hook1 routine

On this page
  1. What it is
  2. When to use it
  3. Members only: Set it up step by step
  4. Members only: Recipes
  5. Members only: Patterns that work
  6. Members only: Mistakes to avoid
  7. Members only: Exercises

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 itYou and your teamClaude
What goes inCommands, conventions, layout, "always do X"Your preferences, your corrections, project facts it cannot read from the code
Where it livesIn the repository or in ~/.claude/~/.claude/projects/<project>/memory/ on your machine
What loadsThe full files, every sessionThe 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.

1. Your prompt goes to the model. 2. The model thinks and plans the next step. 3. It calls a tool: run npm test. 4. A PreToolUse hook checks the call and allows it with exit code 0. 5. The tool runs. 6. The result returns and is added to the model context. 7. The model thinks again; on turn 2 it decides the work is done. 8. The answer goes out.Prompt inyour taskModel thinksplans the next stepAnswer outwhen the work is doneHook checks itPreToolUse checkTool runsruns npm testResult returnsadded to contextBash · npm testLoop until done

In Claude Code, each mechanism enters the context at a different moment and costs a different amount:

MechanismWhen it enters contextCost
CLAUDE.md or AGENTS.md in the working directory and aboveSession startFull text on every request
CLAUDE.md in a subdirectoryWhen Claude reads or edits a file in that folderFrom then on
.claude/rules/*.md without pathsSession startEvery request
.claude/rules/*.md with pathsWhen Claude touches a matching fileFrom then on
Auto memory (MEMORY.md)Session start, first 200 lines or 25 KBEvery request
SkillsDescriptions at start, full text when usedLow until used
MCP serversTool names at start, full schemas on demandLow until used
SubagentsTheir own, separate context windowOnly their summary comes back
HooksThey run outside the conversationZero, 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 isPut it inExample
Needed every session, and shortProject CLAUDE.md or AGENTS.md"Run pnpm test before committing"
Needed only for one part of the codebaseA path-scoped rule in .claude/rules/"API handlers validate input with zod"
A multi-step procedure or a reference docA skillRelease 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 learnAuto memory: say "remember that...""The API tests need a local Redis"
A rule that must hold every timeA permission rule or a hookNever read .env, never push to main
About this task onlyThe prompt"Fix the timezone bug in dates.ts"
A large explorationA 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:

ToolReadsNotes
Claude CodeCLAUDE.md, or AGENTS.md directly when no CLAUDE.md or CLAUDE.local.md exists in the working directory or above itNeeds v2.1.277 or later. An @AGENTS.md import works everywhere
CodexAGENTS.md or AGENTS.override.md, global first, then from the repository root down to the working directoryStops 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.mdThe nearest AGENTS.md takes precedence
Cursor.cursor/rules/*.mdc, plus AGENTS.md in the root and in subdirectoriesRules apply always, by file glob, by relevance or by @ mention
Gemini CLIGEMINI.md by defaultAdd 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

1. Write the project file Run /init in Claude Code. It reads the codebase and writes a starting CLAUDE.md with the build commands, test instructions and conventions it finds, and folds in rules from .cursor/rules/ , .cursorrules and .github/copilot-instructions.md . If a CLAUDE.md exists, it ...

Members only below this line

Get the rest of this guide free

What you get on this page:

  • 1 prompt
  • 1 hook
  • 1 routine

Free. You also get Stratiflux Weekly: one email a week, unsubscribe anytime.

Also in the members part

  1. Recipes
  2. Patterns that work
  3. Mistakes to avoid
  4. Exercises

Sources

Checked against these on 13 Oct 2026.

  1. Claude Code: how Claude remembers your project (CLAUDE.md, AGENTS.md, auto memory) (opens in a new tab)code.claude.com
  2. Claude Code: explore the context window (opens in a new tab)code.claude.com
  3. Claude Code: extend Claude Code (context cost by feature) (opens in a new tab)code.claude.com
  4. Claude Code: how Claude Code uses prompt caching (opens in a new tab)code.claude.com
  5. Claude Code: commands reference (opens in a new tab)code.claude.com
  6. Claude Code: hooks guide (re-inject context after compaction) (opens in a new tab)code.claude.com
  7. Claude Code: routines (opens in a new tab)code.claude.com
  8. AGENTS.md (opens in a new tab)agents.md
  9. Linux Foundation announces the Agentic AI Foundation (opens in a new tab)linuxfoundation.org
  10. Codex: custom instructions with AGENTS.md (opens in a new tab)learn.chatgpt.com
  11. ChatGPT and Codex: memories (opens in a new tab)learn.chatgpt.com
  12. GitHub Docs: add repository custom instructions for Copilot (opens in a new tab)docs.github.com
  13. Cursor: rules (opens in a new tab)cursor.com
  14. Gemini CLI: provide context with GEMINI.md files (opens in a new tab)github.com
  15. Gemini CLI: configuration reference (opens in a new tab)github.com
  16. Claude Help Center: chat search and memory (opens in a new tab)support.claude.com
  17. Claude API: memory tool (opens in a new tab)platform.claude.com
  18. Claude API: compaction (opens in a new tab)platform.claude.com
  19. Claude API: context editing (opens in a new tab)platform.claude.com