Skip to content

Course · Claude Code · Rules & memory

Give the agent standing knowledge with rules and memory

Every Claude Code session starts without your project’s knowledge. The agent does not know your test command, how your team represents money, or which module is fragile, until something tells it. Two mechanisms carry that knowledge into a session without you retyping it:

  • Rules are instructions you write in CLAUDE.md files. Claude Code reads them at launch (subdirectory files load when the agent works in that directory) and places them in the model’s context, the information it can draw on when it decides what to do. One line of such a file is a rule.
  • Auto memory is notes Claude writes for itself from your corrections and from what it learns while working. You do not author it; you audit it.

Both are context. Neither is a check that stops a wrong edit. That distinction is the core teaching of this chapter, and one small example makes it concrete.

The example is a small TypeScript payments service (the example this course returns to; each page restates the facts it needs). Its CLAUDE.md at the repo root already exists, and near the top it carries two lines someone wrote on purpose:

- Use integer cents for all money calculations. Never use floating point for currency.
- Store amounts as `number` representing cents (e.g. $19.99 = 1999).

In src/payments/processor.ts, the fee on every payment comes out of floating-point arithmetic:

const FEE_RATE = 0.029;
const fee = req.amount * FEE_RATE;
const total = req.amount + fee;

The fee is amount * FEE_RATE. The total the caller is charged is the amount plus the fee, which is amount * 1.029. A 1999-cent charge returns a total of 2056.971, a fraction of a cent that integer arithmetic could never produce. The test suite accepts it: tests/payments.test.ts asserts that exact number, and all six tests pass on it. The cents rule loads into every session, and the code floats anyway.

That is not a defect in Claude Code. A rule is context the model reads and weighs. It is not a check that runs against the file. The agent will usually honor the rule in code it writes next, and it may point out the discrepancy if you ask, but nothing forces either outcome. When an outcome must not depend on the model deciding well, you need a gate on the action itself, and the hooks chapter builds one. This chapter builds the knowledge the gate protects.

Four lessons, on a repo whose rules file already exists, so the work is an audit rather than a blank page:

  • Improve an existing CLAUDE.md with /init: on a repo with a CLAUDE.md, /init suggests improvements instead of overwriting. You keep what is true and reject what is invented.
  • Write rules that earn their tokens: the file loads into every session, so every line costs context every time. Learn which lines pay for themselves.
  • Layer CLAUDE.md across scopes: managed policy, user, project, local, and subdirectory files concatenate into one context. Pick the scope that gives each rule the right audience.
  • Let Claude keep its own notes: where auto memory lives, how to inspect it with /memory and /context, and the fork between “remember X” and “add this to CLAUDE.md”.

In the course’s vocabulary this chapter is about context: what information is available when the agent decides, how fresh it is, and how much authority it carries. It touches cost as well, because a rule line is paid in context on every session whether or not the session needs it. And the gate it points forward to is the course’s control principle - what guides execution, enforces boundaries, or allows intervention. The Claude Code memory reference is the primary source for every mechanism described here.

Start with running /init on a repo that already has rules.