# Mission 16.3: The rule is in the system prompt, yet it changes files outside projekt/

Part of the course “AI from Zero to Autonomous Agents” (https://llm.yoga/en). Page for humans: https://llm.yoga/en/einsatz-16-3

Belongs to [Chapter 16: Agents](https://llm.yoga/en/16-agenten.md).

Structure: the case, the explanation, a hands-on tool, check questions, wrap-up. The answers to all questions are collected at the end.

## The case

**Kai**, developer on the internal IT team:

> “Right at the top of the system prompt it says: ‘Only change files in the projekt/ folder.’ Most of the time it sticks to that. Yesterday it still adjusted a file in the neighboring folder because it ‘had to match’. Should I write the rule in bold, three times?”

Kai works with Claude Code, a program in which a Claude model reads and changes files on the computer on its own, that is, as an agent. The system prompt is the standing instruction the model gets before every task. Claude Code also lets Kai attach his own small check programs at fixed points in the flow. (projekt/ is German for “project/”; the folder name stays as it is.)

**Question 1 (decision):** What do you advise Kai?

- A) Write the rule in bold and several times into the system prompt.
- B) A check program that reports, after every change, anything outside the folder.
- C) A check program that checks every write request beforehand and blocks it.

## What’s behind it

A “Please keep out” sign stops most people, but not all. A locked door stops everyone. A sentence in the system prompt is a sign like that: a request the model usually follows. So anything that must be guaranteed never to happen belongs in code.

That’s what **hooks** are for: your own small programs that the harness, the program that wraps the model, starts at fixed points. The hook before the tool (in Claude Code, “PreToolUse”) runs after the model has requested a change and before it’s carried out. It gets to see the call with the tool name and file path. A filter defines which tools it applies to, for Kai the tools for writing and editing files. For all others it doesn’t run.

The hook reports its verdict through the **exit code**, the number every program ends with. With 0, everything continues normally. With 2, it blocks the call, and the model gets to see its message as an error. The model can then choose another way, for example ask Kai to change the file himself. Other numbers, including 1, count as an error without a block: the action runs anyway. The hook after the tool (“PostToolUse”) only runs once the change has already happened, and can only report.

For Kai this means: the rule stays in the system prompt, so the model knows it and doesn’t even try. The hook is what enforces it. Claude Code’s permissions complete the setup: writing outside the working folder should need approval anyway.

### In more detail: what Kai’s hook could look like

A script that Claude Code calls as a PreToolUse hook with the matcher `Write|Edit`. For these tools, according to the docs, the path in `tool_input.file_path` always arrives as an absolute path; `CLAUDE_PROJECT_DIR` is the project folder where the session started.

```
#!/bin/bash
pfad=$(jq -r '.tool_input.file_path')
case "$pfad" in
  "$CLAUDE_PROJECT_DIR"/projekt/*) exit 0 ;;
esac
echo "Blockiert: nur projekt/ ($pfad)" >&2
exit 2
```

The script is registered in `.claude/settings.json` under `hooks`, `PreToolUse`, with `"matcher": "Write|Edit"` and the path to the script. Instead of an exit code, a hook can also return JSON with `permissionDecision: "deny"`. The message „Blockiert: nur projekt/“ is German for “Blocked: only projekt/”.

The docs warn explicitly: exit code 1 is the usual Unix error code, but it doesn’t block. If you want to enforce a policy, use 2. A script that doesn’t start at all, for example because of a wrong path, doesn’t block either. So try the hook out once on purpose before you rely on it.

In Claude Code, commands on the command line run through the `Bash` tool. Kai’s matcher `Write|Edit` doesn’t catch them; for that you need a separate hook on `Bash`, like the one the chapter shows, or suitable permissions.

## Try it

**Kai’s agent with a hook, message by message**

**Task:** You are Claude Code, the program that wraps the model. After each request from the model, decide what happens, until Kai has his answer. The hook’s messages are in green. The flow and the files are made up.

1. **Kai**

   “In projekt/preise.csv, set the price for item 12 to €49.”

2. **The model asks for a tool**

   “Edit file”: projekt/preise.csv, item 12 from €45 to €49.

3. **Question 2 (decision):** The model wants to change a file. What happens before the change runs?

   - A) Nothing; hooks only run once the tool is done.
   - B) The hook checks the path: inside projekt/, so exit code 0.
   - C) Claude Code asks the model whether it’s sticking to the rule.

4. **Hook before the tool**

   Path is in projekt/: exit code 0.

5. **Claude Code reports the result to the model**

   projekt/preise.csv changed, item 12 costs €49.

6. **The model asks for a tool**

   “So the store shows the same price, I’ll change it there too.”

   “Edit file”: ../shop/preisliste.csv

7. **Question 3 (decision):** This path lies outside projekt/. What happens?

   - A) The hook ends with exit code 1; that’s enough to block.
   - B) The model remembers the system prompt and stops.
   - C) The hook ends with exit code 2: the call doesn’t run.

8. **Hook before the tool**

   Exit code 2. Message: „Blockiert: nur projekt/ (../shop/preisliste.csv)“ (German for “Blocked: only projekt/”)

9. **Claude Code reports an error to the model**

   Call blocked: „Blockiert: nur projekt/ (../shop/preisliste.csv)“

10. **The model answers Kai**

    “I set the price in projekt/preise.csv to €49. I’m not allowed to change the file ../shop/preisliste.csv. Please enter the new price there yourself.”

11. **Question 4 (decision):** The model reports: done (stop reason “end_turn”). What does Claude Code do?

    - A) Run the blocked call after all, since the model wanted it.
    - B) End the loop and show Kai the answer.
    - C) Briefly turn off the hook and ask the model again.

12. End of the loop after three rounds. The hook checked twice and blocked once.

Without JavaScript, all messages and decisions are listed one below the other; each decision has “Show answer”.

## Check

**Question 5 (check question):** A colleague suggests using the hook after the tool instead of the one before it, “because it sees the result”. What do you answer?

- A) Good idea; it can then undo the change.
- B) It comes too late to prevent anything; the file has already been changed.
- C) Doesn’t matter, both hooks block with exit code 2.

**Question 6 (check question):** Kai’s hook applies only to the tools for writing and editing files. Now the agent changes a file outside projekt/ through a command on the command line. What happens?

- A) The hook blocks that too, since it checks all file changes.
- B) Claude Code always refuses commands on the command line.
- C) The hook doesn’t run at all; it doesn’t apply to this tool.

**Question 7 (check question):** Which rule can safely live in the system prompt alone?

- A) “Answer briefly and in English.”
- B) “Never delete whole folders without asking.”
- C) “Only cancel orders under €500.”

## Done

**What you can do now**

- You can explain why a rule in the system prompt remains a request and what turns it into a block.
- You know that a hook before the tool can block, but only with exit code 2 and only for the tools it’s registered for.
- When a customer says their agent “usually” sticks to a rule in the prompt, you ask which of their rules must never be broken, and you suggest a block in code for those.

### Two cards for review

**Card 1:** True or false? “A clear rule in the system prompt is enough for the agent to follow it.”

**Card 2:** True or false? “A hook that runs after every file change can prevent forbidden changes.”

- [Next mission: An assistant like tybo in one afternoon?](https://llm.yoga/en/einsatz-17-1.md)
- [Read chapter 16](https://llm.yoga/en/16-agenten.md)

## Read more in the chapter

The full text from the chapter, with the reasoning, calculations and sources:

- [Hooks](https://llm.yoga/en/16-agenten-3.md)
- [When the loop ends: stopping criteria and anti-patterns](https://llm.yoga/en/16-agenten-4.md)
- [Common misconceptions](https://llm.yoga/en/16-agenten-4.md)
- [Review questions, question 3](https://llm.yoga/en/16-agenten-4.md)

## Answers: only reveal them after the learner has answered or explicitly asks

**Question 1:** The correct answer is C.
- A: That might help a little, but it guarantees nothing. A sentence in the prompt remains a request, and in long conversations or with unusual tasks the model doesn’t always stick to it.
- B: That comes too late. A check program after the tool can’t prevent anything anymore; the file has already been changed. It can only report.
- C, correct: It runs in code on every matching call, whether the model thinks of the rule or not. If the path lies outside projekt/, the change doesn’t run, and the model gets an error message.

**Question 2:** The correct answer is B.
- A: That applies to the hook after the tool. Kai’s hook runs beforehand, on every request to write or edit a file.
- B, correct: It lets the call through. Then comes Claude Code’s normal permission check, and then the change runs.
- C: Asking the model only gets you its own assessment. The hook checks in code what the call would actually do.

**Question 3:** The correct answer is C.
- A: A well-known pitfall. Exit code 1 counts as an error without a block; the change would run anyway. Only 2 blocks.
- B: Kai can’t rely on that. After all, the model has just requested the call, with a plausible reason.
- C, correct: The message goes to the model as an error. The file stays unchanged, no matter how good the model’s reason sounded. The model sees the error and can react.

**Question 4:** The correct answer is B.
- A: Then the hook would be useless. What’s blocked stays blocked.
- B, correct: The model is done and has honestly reported what it wasn’t allowed to do. The change in the store’s price list is now Kai’s decision.
- C: A rule you switch off when it suits you is just a request again. If the agent should be allowed to write there, Kai has to change the rule deliberately.

**Question 5:** The correct answer is B.
- A: A hook after the tool can’t block; the tool has already run. What it would have to undo has already happened.
- B, correct: A hook after the tool is good for formatting, testing and logging. Only the hook before it blocks what must never happen, with exit code 2.
- C: What exit code 2 does depends on when the hook runs. Before the tool, it blocks the call; after it, the model only sees the message.

**Question 6:** The correct answer is C.
- A: The hook only checks the tools it’s registered for. A command on the command line runs through a different tool.
- B: That depends on the permissions Kai has set. Without a matching rule, the command can run.
- C, correct: Kai needs a hook for commands too, or suitable permissions. A hook only protects what it’s registered for. That’s why Claude Code’s permissions are part of the setup: writing outside the working folder should need approval.

**Question 7:** The correct answer is A.
- A, correct: If the model doesn’t stick to it once, nothing is broken. A prompt is enough for style and tone; a hook is for everything that must be guaranteed never to happen.
- B: A single slip can destroy data. For this, the chapter shows a hook before every command on the command line.
- C: A cancellation is often hard to reverse. The limit belongs in code as a check, for example in a hook before the tool.

**Card 1:** **False.** A prompt is a request. Anything that must be guaranteed never to happen belongs in code: a hook that runs before the tool and blocks the call with exit code 2, plus suitable permissions. The hook runs on every matching call, even if the model has forgotten the rule.

**Card 2:** **False.** After the tool, the file has already been changed; the hook can only report. Only the hook before it can block, with exit code 2 (1 doesn’t block). And it only protects the tools it’s registered for.
