Claude Code

The agentguardrails template

The 4 rules I give an agent before I let it run on its own: a scorecard, tiny steps, a fence and an undo button. One file to fill in, plus the setup in Claude Code.

  • ToolClaude Code
  • Time30 min
  • You needPaid Claude plan, Claude Code, git
  • Reel keywordGUARDRAILS

These are the 4 rules I give any agent before I let it run on its own: it scores its own work, it moves in tiny steps, it stays inside a fence, and every change it makes can be undone. Below is the template file, then how to set it up in Claude Code so the rules are actually enforced, not just politely asked for.

It’s the same shape as the loop Andrej Karpathy left running for 2 days on a small model’s training code. It ran about 700 experiments and kept 20 improvements. The agent could change one file, every run was judged on one number, and anything that didn’t beat the last best got thrown away.

About 30 minutes the first time.

Before you start

Claude Code on a paid plan. Pro, Max, Team or Enterprise. If you haven’t installed it, the first section of the grunt work setup has the one-line install.

git. Open your terminal and type git --version. If you get a version number, you’re set. If not, install it from git-scm.com. git is the undo button.

Something with a score. This is the part people skip. The loop only works if a command can print one number that says better or worse: how long a script takes, how many tests pass, how many items out of 20 a prompt gets right. If you can’t score it, an agent can’t improve it on its own. Start with that.

Step 1: Put your project in its own folder

Copy the thing you want improved into a fresh folder, so the agent can never reach your originals. Open a terminal there and start Claude Code:

cd /path/to/your-folder
claude

Step 2: Fill in the template

Copy the template at the top of this page into a file called guardrails.md in that folder. Fill in the 4 square-bracket bits: the goal, the score command, which direction is better, and the one file it’s allowed to change.

If you don’t have a score command yet, paste this and let Claude write one:

Write a script called score.py that prints exactly one number measuring [what you care about, e.g. how many seconds report.py takes to run, averaged over 3 runs, and print 999 if its output changes]. Don't edit any other file. Run it once and show me the number.

You’ll know it worked when running the score command prints a single number, and running it twice gives roughly the same number.

Step 3: Build the fence and the undo button

The template asks the agent to stay inside the fence. This step makes Claude Code enforce it. Paste this, swapping in your file names:

Do exactly these steps and nothing else. 1. Run git init, add every file in this folder, and commit with the message "starting point". 2. Create results.md containing the line: # Results 3. Create .claude/settings.json with exactly the text between START and END. Don't change anything. 4. Commit again with the message "guardrails in place".

START
{
  "permissions": {
    "deny": ["Edit(/guardrails.md)", "Edit(/score.py)", "Edit(/.claude/**)", "Bash(git push:*)"]
  }
}
END

The deny rules stop Claude’s file tools from editing your rules file, your score script or its own settings, and block it from pushing anywhere. If your score command lives in a different file, put that file name in instead of score.py.

Then quit Claude Code with /exit and start it again with claude, because new settings only load when a session starts.

You’ll know it worked when git log shows your 2 commits, and asking Claude to edit guardrails.md gets refused.

Step 4: Let it run

Read guardrails.md and follow it exactly. Start by running the score command and recording the starting score in results.md, then begin round 1.

Now you can walk away. Every round leaves a commit if it helped and nothing if it didn’t, plus one line in results.md.

You’ll know it worked when results.md fills up with rounds marked kept or undone, and git log only shows the kept ones.

Step 5: Check it yourself before you trust it

When it stops, don’t take its word for it. Run the score command yourself in a second terminal window. Then open the file it was allowed to change and read the diff:

git diff HEAD~5

That shows the last 5 kept changes. If you don’t like something, git reset --hard back to any earlier commit and it’s gone.

A score that keeps going up can still mean the agent found a way to game the score instead of doing the job. If the number looks too good, the score is usually the problem, so tighten the score and run it again.

When it goes wrong

It edits files outside the fence. The deny rules cover Claude’s file tools, but a shell command can still write to a file. Your git history is the backstop: git status shows anything changed that shouldn’t have been, and git reset --hard puts it back.

Every round gets undone. Either the steps are too big or the score is too noisy. Run the score 3 times in a row. If it jumps around, average it over more runs.

It stops after one round. Tell it again: “Keep going, follow the stop condition in guardrails.md.”

The score went up but the result is worse. The score measures the wrong thing. Fix the score, not the agent.

The checklist

  • The project is in its own folder, not your originals
  • The score command prints one steady number
  • guardrails.md has your goal, score, direction and the one file it can change
  • .claude/settings.json has the deny rules, and Claude Code was restarted
  • git log shows a starting point commit
  • You ran the score yourself and read the diff before trusting the result
Copy this

guardrails.md

# Guardrails

You are running an improvement loop on your own. I am not watching. Follow these rules exactly, every round, until you hit the stop condition.

## The goal
[ONE sentence. What are you making better? Example: make report.py run faster without changing its output.]

## Rule 1: Score every change
The score command is: [EXAMPLE: python3 score.py]
It prints one number. [Higher or lower] is better.
Run it once before you change anything and record that as the starting score. After every change, run it again. If the score got better, keep the change. If it got worse or stayed the same, undo it. Never judge a change by how it looks, only by the score.

## Rule 2: Tiny steps
Make one small change per round. One idea, tested on its own. Never bundle several ideas into one round, because then you can't tell which one helped. If a round needs more than about 20 changed lines, split it.

## Rule 3: Stay inside the fence
You may only edit: [EXAMPLE: report.py]
Never edit, move, rename or delete anything else in this folder, with any tool, including shell commands. That includes this file, the score command's files, the .claude folder and results.md. Never install new packages. If you think the fix needs something outside the fence, write it in results.md as a suggestion for me and move on.

## Rule 4: Every change can be undone
Before each change, commit the current state with git. After scoring, if the change is kept, commit it with a message that says what you tried and the new score. If it is not kept, run git reset --hard to the last kept commit. Never use git push, never rewrite history beyond the last commit, never delete branches.

## The log
After every round, add one line to results.md: round number, what you tried, the score, kept or undone.

## Stop condition
Stop after [20] rounds, or if 5 rounds in a row are undone, or if the score command errors twice in a row. When you stop, tell me the starting score, the best score, and the 3 changes that helped most.

How to use it: paste it into Claude, ChatGPT or Gemini and send. To keep it, add the skill file in your AI's settings or paste it into a Project.

The weekly email

New guides, first.

One short email a week: the best AI tip I found, and what happened in the ANZ startup scene. Five minutes, no fluff.

By joining, you agree to get emails from jack gpt (Jack Ryan) and to the Privacy policy and Terms. Unsubscribe any time. You must be 18 or over.