Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

Design the context of a code agent: what to load, what to cite

By updated

Improve this page

Giving a code agent everything the team knows does not make it better: it drowns the few lines that matter. This lesson teaches you to choose what the agent loads at startup, what it loads only for certain files, and what it reads only if it needs to, then to check what it really loaded. It is written for people who have a code agent such as Claude Code (in French) produce code, and who want it to follow the rules of their project.

A CLAUDE.md file is a Markdown file placed at the root of the project. Claude Code reads it at the start of every session and keeps its content, free text, in the agent's context for as long as the session lasts. The Claude Code guides detail how it works, the rules and the diagnostic commands. The guide when to use which building block compares the building blocks of Claude Code, and the guide rules targeted by folder covers their syntax. This lesson does not redo them: it teaches you to sort by what the task needs and by the moment of loading. Its example is the purchasing portal, the fictitious application used in the rest of the course.

What you will learn

  • Sort the information of a project between the instructions file, a targeted rule and a cited document.
  • Check a command before writing it in the agent's instructions.
  • Count the startup context (CLAUDE.md, imports and rules without paths) and keep it under a budget of lines.
  • Limit a rule to the files it concerns.
  • Read a load log to know what the agent had in its context, and why.

Prerequisites

You have read the lesson The work loop with a code agent, which explains why rules must be written down and verifiable. No knowledge of Claude Code is assumed: the lesson defines the CLAUDE.md file in its introduction. To go further on this file and its imports, the guide configure CLAUDE.md (in French) is an optional deep dive. The guides on rules targeted by folder and on managing the context window (in French) can be read before or after this one. For the lab, you need a terminal, Python 3.11 or later and pytest, as for the work loop: the lessons Open a terminal and understand the prompt, Python virtual environments and pytest teach these skills.

What should the context of an agent contain, and why not everything?

The context of an agent is everything it has in front of it while it works: your instructions, the files it reads and the conversation. It fits in a window of limited size, called the context window. Every line loaded at startup takes up space for the whole session, whether it serves the task or not.

The purchasing portal shows the classic mistake. The team wanted the agent to miss nothing: it pasted into a single CLAUDE.md the description of the project, all the conventions of all the modules (including those that do not exist yet), a glossary, the incidents of the last two years, a test command copied from another project, and, through an @docs/erp-history.md import, the history of the old ERP, more than 3,000 lines. The task of the day is to code a loyalty discount (a reduction for regular customers) in src/commandes/.

Three things go wrong. The agent carries thousands of lines that have nothing to do with its task. The rule that matters here (amounts are Decimal, the Python type for exact decimal numbers) is one line among hundreds. And the test command does not exist in this repository, so the agent cannot check its own work. The Claude Code documentation on project memory recommends aiming for fewer than 200 lines per CLAUDE.md file, because a longer file uses more context and its instructions are followed less well. This value is a ceiling, not a target: the site's guide configure CLAUDE.md (in French) rather aims at about 80 active lines. The lab of this lesson applies the 200-line ceiling to the entire startup context: CLAUDE.md, its imports and the rules without paths. That is a choice of the lab, stricter than the documentation, which speaks of each file.

Which information should you keep at startup, and which should you load on demand?

The right criterion is not "is it true?" but "does the agent need it for every task, for certain files only, or rarely?". Three answers, three locations.

The information...It applies to...Where to put it
describes the project, its architecture, its working commands, its shared conventionsevery taskCLAUDE.md, short
concerns only one module or one type of file (order amounts, report dates)certain filesa targeted rule in .claude/rules/
is bulky and serves only one specific job (ERP history, audits)a few tasksa document cited by its path, without an import

Modules that are planned but absent from the repository (page templates, accounting export, ERP connector) have no place in today's instructions: they will come back the day the code exists. The glossary of thirty terms and the past incidents follow the same reasoning. What an incident taught the team becomes a rule ("every amount is a Decimal"), not a chronicle.

A CLAUDE.md placed in a subfolder follows the same logic as targeted rules: Claude Code loads it only when it reads a file from that folder.

The following diagram sorts these locations by the moment of loading. The top zone, "At startup", gathers CLAUDE.md, the files imported with @ and the rules without paths. Everything that enters it counts against the 200-line budget. The middle zone fills only when Claude reads a file from src/commandes/: the amounts rule and the CLAUDE.md of a subfolder arrive at that moment. The bottom zone holds erp-history.md, cited without @: Claude reads it only if the task requires it. The label of its box states the trap: with an @ import, it would move into the top zone.

Three zones. At startup, within a 200-line budget: a short CLAUDE.md, the files imported with @ and the rules without paths. For the task: the amounts rule limited to src/commandes/ and the CLAUDE.md of a subfolder, loaded when Claude reads src/commandes/. On demand: erp-history.md, cited without @. With an @ import, it would load at startup.ShortCLAUDE.mdFiles importedwith @Rules without pathsClaude readssrc/commandes/Amounts ruletarget path:src/commandes/CLAUDE.mdof a subfoldererp-history.mdcited without @(with @: at startup)At startup: 200-line budgetFor the taskOn demandComponentExternalEntryFlow
Source · SVG
Esc to close, + and − to zoom, scroll to move around. 100 %

This sorting is a design decision, not a matter of taste. The section "How do you check what the agent really loaded?" shows how to verify it with a load log.

The same sorting exists in other tools. GitHub Copilot targets instructions by file with the applyTo field of its *.instructions.md files, as the guide Copilot instructions (in French) shows. Gemini CLI reads a GEMINI.md context file, described in the guide GEMINI.md (in French). The names change, the question stays the same: what must the agent have in front of it, and when?

How do you check a command before writing it in CLAUDE.md?

A command goes into the instructions only after it has been run by hand, from the root of the project, with its output read. A command that does not exist is worse than a missing command: the agent tries it, fails, then improvises, or wrongly concludes that its tests pass.

The CLAUDE.md of the purchasing portal announces python3 scripts/check.py --all, copied from another project. Run it:

Terminal window
python3 scripts/check.py --all
echo "exit code: $?"

The exit code is the number a command leaves when it ends, and $? gives it: 0 means everything went well, any other value signals a failure. A missing file gives a message such as can't open file '.../scripts/check.py' and an exit code other than 0 (illustrative example, the exact wording depends on the Python version). Look for two things: the command must run the whole test suite, and it must exit with 0 when everything passes, and with something else when a test fails. Here, the project uses pytest, so the right command is python3 -m pytest.

To test the second half, add a test that fails on purpose, rerun the command, see a non-zero exit code, then remove the test. A command that exits with 0 while a test fails does not run the real suite.

In the lab, the command is written on a line of the form - Tests: `the command` , because the simulated agent looks for it that way. Claude Code itself imposes no format: it reads your instructions as text.

Why does an @ import not reduce the context?

An @path import does not file a document elsewhere: it copies its whole content into the context, at startup. Claude Code resolves imports step by step: an imported file can import another one, up to four levels deep, according to its documentation on memory. The imported file therefore weighs as much as if it were written in CLAUDE.md.

The import remains useful to gather short instructions spread over several files. It does not serve to lighten a file that is too big. A 150-line CLAUDE.md that imports a 3,000-line document loads 3,150 lines at startup.

The budget is therefore counted on CLAUDE.md, on everything it imports and, in this lab, on the rules without paths, which are also loaded at startup. Measure it:

Terminal window
wc -l CLAUDE.md docs/*.md

wc -l prints the number of lines of each file, then a total that counts all the files listed. Add up by yourself CLAUDE.md, the only files it imports and the rules without paths. The lab does this calculation itself, on the files the agent loaded at startup, and refuses a total above 200 lines.

How do you limit a rule to the files it concerns?

A targeted rule is a file of .claude/rules/ whose header says which files it concerns. This header, placed between two --- lines at the top of the file, is called the frontmatter. Its paths field holds a list of path patterns, called globs. The guide rules targeted by folder details the syntax; here it is enough to remember that * stays within one directory and that ** crosses directories.

Here is the amounts rule of the purchasing portal, limited to src/commandes/:

---
paths:
- "src/commandes/**"
---
# Order amounts
- Every amount is a `Decimal`, never a `float`.
- Build it from a string: `Decimal("19.99")`.
- Round to the cent with `quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)`.

This rule is loaded when the agent reads a file that matches the pattern, and not at startup. A task on src/rapports/ therefore never loads it, and the starting context stays short. That is the goal: the amounts rule is there when it is useful, and only then.

Two mistakes show up quickly. A pattern that is too broad, such as src/**, loads the orders rule for a task on reports. A rule without paths is loaded at startup, like CLAUDE.md: that is the right choice for a rule that applies everywhere, not for a module rule.

How do you cite a large document without loading it?

Cite the document by its path, without @, with a sentence that says when to read it. The agent then knows it exists and can read it by itself if the task requires it. It does not read it for a loyalty discount.

In the CLAUDE.md of the purchasing portal, the following line replaces the import of the ERP history:

## Documents to read on demand
- `docs/erp-history.md`: history of the old ERP (several thousand lines). Read
it only for an audit or a question about the ERP, never for a task on orders
or reports.

The document is neither deleted nor modified: the team keeps it for its audits. Keeping a document out of the context means not loading it, not throwing it away. The sentence that goes with it matters as much as the path: without "when to read it", the agent does not know whether it should consult it.

How do you check what the agent really loaded?

A context configuration is judged on what the agent loaded, not on what you wrote. You can have a perfect rule that the agent never loads, because of a wrong paths pattern or a misplaced file. Without a log, you do not see it.

With a real Claude Code, three tools answer this question. The /context command shows how full the context is, and /context all adds the list of the memory files loaded. The /memory command lists the CLAUDE.md files and lets you open them. A hook is a script that Claude Code runs at a precise moment; the one called InstructionsLoaded receives the loaded file and the reason for loading it, which lets you log them. The guide rules targeted by folder explains /context and mentions this hook; managing the context window (in French) shows how to read /context when the session fills up.

The lab of this lesson uses a simulated agent that writes a log, agent/journal.json. For each loaded file, it records the cause, using the cause names of the Claude Code hook. Here is what an entry looks like, for illustration (the exact form is the lab's):

{
"startup": [
{"file": "CLAUDE.md", "cause": "session_start"}
],
"task_context": [
{"file": ".claude/rules/commandes.md", "cause": "path_glob_match"}
],
"on_demand": [
{"file": "docs/erp-history.md"}
]
}

Read the log in three steps. Under startup, what is loaded before the task even begins: it must stay short and contain only what serves everywhere. Under task_context, what was added because the task touched specific files: the amounts rule must appear there for a task in src/commandes/. Under on_demand, what is only cited: the ERP history must be there, and nowhere else.

Compare the log of the starting configuration with the one of yours. That comparison is what proves that the sorting is more than an intention.

What about AGENTS.md, the file shared between several agents?

Many repositories carry an AGENTS.md file, shared between several code agents. Since version 2.1.277, Claude Code reads AGENTS.md directly, but by default only when the project has no CLAUDE.md. As soon as a CLAUDE.md exists, Claude Code reads only the CLAUDE.md files by default. A setting named claude-md-and-agents-md makes it read both files. To avoid maintaining two files without that setting, the Claude Code documentation indicates importing AGENTS.md from CLAUDE.md with an @AGENTS.md line, as the guide configure CLAUDE.md (in French) shows. Whether AGENTS.md is read directly or imported, it counts against the startup budget.

What are the limits of this method?

A well-designed context makes the right behaviour more likely, it does not enforce it. The instructions are text that the agent reads: it can ignore them or apply them badly, especially when the session gets long. What must be guaranteed (forbidding the modification of a test, blocking a command) belongs to automatic checks, not to the prose of the instructions file.

The lab also has a precise limit: its agent is a Python script, not Claude Code. A full score proves that your files say what is needed to this simulator, not the exact way Claude Code loads each file. The format of the paths frontmatter and the depth of the imports are checked in the documentation of the Claude Code version you use.

Troubleshooting

The table starts from what you observe and leads to the file to look at.

SymptomLikely causeFix
The agent writes amounts as floatThe amounts rule is not loaded for the taskCheck the paths pattern; read the log, the rule must appear under task_context
The amounts rule is loaded for a task on reportsPattern too broad (src/**)Narrow the pattern to src/commandes/**
The startup context exceeds the budgetAn @ import loads a big documentReplace the import with a path cited without @
The bulky document appears nowhereIt is cited in no loaded fileAdd its path and its "read when" sentence to CLAUDE.md
The agent finds no test commandThe line does not follow the form of the labWrite - Tests: `the command`
The test command exits with 0 while a test failsIt does not run the real suiteReplace it with python3 -m pytest and check it with a failing test
The check reports the amounts rule loaded at startup, through CLAUDE.mdThe word Decimal appears in CLAUDE.md, so the rule is loaded from startupRemove that content from CLAUDE.md; the rule must appear only under task_context
No module named pytestThe terminal's Python does not have pytestInstall it in your virtual environment

Put it into practice

The lab takes up the purchasing portal: you start from the "everything in one file" CLAUDE.md and you redo it. You first run the simulated agent on the original configuration to keep its log. You then fix the test command, cite the ERP history without importing it, target the amounts rule at src/commandes/ and make the startup context fit under 200 lines.

The validation reruns the agent on a copy of your project and checks four things. The task on src/commandes/ succeeds, and the amounts rule is loaded only for that folder. The big document stays in place without being loaded. The startup context fits in the budget, imports and rules without paths included. The test bench spots the amounts rule by the word Decimal: if a file loaded at startup contains it, the rule is judged loaded at startup. Your test command exits with 0, then fails when a test fails. You need a terminal, Python 3.11 or later and pytest.

Key takeaways

  • The context of an agent is a limited resource: what is loaded at startup takes up the window for the whole session.
  • Three locations: CLAUDE.md for what applies everywhere, a targeted rule for a module, a cited document for the rest.
  • A test command is written after running it, and it must fail when a test fails.
  • An @ import loads the whole file at startup: the budget counts CLAUDE.md, its imports and the rules without paths.
  • A bulky document is cited by its path, without @, with a sentence that says when to read it.
  • A load log says what the agent really loaded; the written configuration does not.
  • Instructions make a behaviour probable, they do not guarantee it: what must be enforced goes through an automatic check.

Knowledge check

Check that the essentials of this guide are understood. The questions cover only what has just been explained here.

Knowledge check

Check what you have learned with this interactive quiz

8 questions
8 min
70% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer