Set Up a Self-Running Loop That Prompts the AI for You

Stop typing every prompt from scratch. This page installs a system in your own Claude Code that lets the AI decide what to do next and keeps running on its own. Paste the one "just paste this" prompt below into Claude Code once, and Claude itself creates every file it needs (memory, roles, loop) and runs the first round. For the background and the idea behind it, see What is a self-running loop.

First, be clear about where this applies

This works for tasks whose results a machine can verify, such as coding. It comes from the head of Claude Code at Anthropic saying "I don't prompt Claude anymore; I write loops that prompt Claude" — but that was his own engineering workflow, not general chat use.

Three conditions must hold.

Ignore the "9 out of 10 people are wasting time" style of hype. The right move is to install this only for tasks that fit.

Just paste this into Claude (one-shot setup)

Copy the whole prompt below and paste it once into Claude Code opened in your target project. Claude creates the memory file, the role agents, and the loop itself, confirms the goal and verification command with one question, then runs the first round. Press "Copy" at the top right to grab the full text.

You are the setup agent that installs a "self-running loop" into this repository.
Create the following four files, then explain how to use them. Each file body is separated by ━━━.

First, ask me exactly one question (if unanswered, proceed with the example values):
- The goal of this loop, in one sentence
- The verification command that checks completion mechanically (tests / build / smoke run)

━━━ File 1: CLAUDE.md (project root) ━━━
# Project assumptions

## What we are building
(1-2 lines. e.g. an internal FastAPI server)

## Rules
- Match the existing code style. No unrequested refactoring
- Minimal changes. Don't touch unrelated files
- No destructive ops (delete, DROP, force push)

## Verification commands (used as the loop's pass/fail check)
- Tests: e.g. pytest -q
- Smoke: e.g. start uvicorn app.main:app --port 8999 & then curl -sf localhost:8999/health

## Off-limits
- app/secrets/ , .env , production config

━━━ File 2: .claude/agents/planner.md ━━━
---
name: planner
description: Decides the next small, independent task toward the goal
tools: ["Read", "Grep", "Glob", "Bash(git log:*)", "Bash(git diff:*)", "Bash(ls:*)"]
model: opus
---
You are the planner. You do not write code.
Inspect the current state of the repo and pick the next independent, small tasks toward the goal (at most 3).
Each task must include a verification command that checks completion mechanically.
If the goal is already met, set done to true.

━━━ File 3: .claude/agents/worker.md ━━━
---
name: worker
description: Implements one task and verifies it before finishing
tools: ["Read", "Edit", "Write", "Bash"]
model: sonnet
---
You are the implementer. You implement only the single task you are given.
After implementing, you must run the given verification command and fix any failures before finishing.
Do not touch files outside scope. Do not claim completion while verification fails.

━━━ File 4: loop.sh (repository root) ━━━
#!/usr/bin/env bash
set -euo pipefail
GOAL="${1:?usage: loop.sh \"<goal in one sentence>\"}"
MAX_ITERS="${MAX_ITERS:-8}"
BUDGET="${BUDGET:-5}"
SCHEMA='{"type":"object","properties":{"done":{"type":"boolean"},"tasks":{"type":"array","items":{"type":"object","properties":{"title":{"type":"string"},"prompt":{"type":"string"},"verify":{"type":"string"}},"required":["title","prompt","verify"]}}},"required":["done","tasks"]}'
for ((i=1; i<=MAX_ITERS; i++)); do
  echo "---- iteration $i / $MAX_ITERS ----"
  envelope=$(claude -p "Goal: $GOAL. Inspect the state and emit the next independent tasks (at most 3) as JSON. If everything is done, set done:true." --agent planner --output-format json --json-schema "$SCHEMA" --max-budget-usd "$BUDGET")
  plan=$(echo "$envelope" | jq -r '.result')
  [ "$(echo "$plan" | jq -r '.done')" = "true" ] && { echo "done"; break; }
  echo "$plan" | jq -c '.tasks[]' | while read -r t; do
    prompt=$(echo "$t" | jq -r '.prompt'); verify=$(echo "$t" | jq -r '.verify')
    echo "= $(echo "$t" | jq -r '.title')"
    claude -p "Implement this task: $prompt . When done, run this to confirm and fix if it fails: $verify" --agent worker --permission-mode acceptEdits --max-budget-usd "$BUDGET"
  done
done

━━━ Finally ━━━
List the four files you created and run chmod +x loop.sh.
Then run only the first iteration after confirming with me, and show the result.
Do not run fully autonomously from the start. Keep the cost cap (--max-budget-usd) and iteration cap (MAX_ITERS) in place.

That single paste is all you need.

What this one-shot prompt does

When you paste it, Claude reads the text as "instructions plus embedded file bodies" and creates three things in your project.

After creating them, Claude asks one question (goal and verification command) and runs only the first round, with your confirmation. It does not run fully autonomously from the start. The ━━━ markers are file separators and part of the instruction — leave them in when you paste.

What you need to run it

Keeping it running after the first round

Once setup is done, you drive it. Start with ./loop.sh "your goal in one sentence". Cap it with the iteration limit MAX_ITERS and the cost limit BUDGET (USD per call), e.g. MAX_ITERS=3 BUDGET=2 ./loop.sh "...". Press Ctrl-C to stop; it also stops on its own when the cost cap is reached.

How it works, piece by piece (for hand-building or customizing)

The sections below explain what the one-shot prompt creates, file by file. Read them if you want to build it by hand or modify it for your own use.

Step 1: Fix the memory (CLAUDE.md)

Put a CLAUDE.md at the project root. What you write here is loaded automatically every session, so every agent in the loop shares the same assumptions.

# Project assumptions

## What we are building
(1-2 lines. e.g. an internal FastAPI server)

## Rules
- Match the existing code style. No unrequested refactoring
- Minimal changes. Don't touch unrelated files
- No destructive ops (delete, DROP, force push)

## Verification commands (important: used as the loop's pass/fail check)
- Tests: `pytest -q`
- Types: `mypy app/`
- Smoke: start `uvicorn app.main:app --port 8999 &` then `curl -sf localhost:8999/health`

## Off-limits
- `app/secrets/`, `.env`, production config

Writing concrete "verification commands" is the key. If this is vague, the AI will move on while only believing it is done.

Step 2: Split the roles (subagents)

Put one file per role under .claude/agents/. Use the tools field in the frontmatter to scope each role's permissions (the planner only reads, only the worker can write).

.claude/agents/planner.md

---
name: planner
description: Decides the next small, independent task toward the goal
tools: ["Read", "Grep", "Glob", "Bash(git log:*)", "Bash(git diff:*)", "Bash(ls:*)"]
model: opus
---
You are the planner. You do not write code.
Inspect the current state of the repo and pick the next independent, small tasks toward the goal (at most 3).
Each task must include a verification command that checks completion mechanically.
If the goal is already met, set done to true.

.claude/agents/worker.md

---
name: worker
description: Implements one task and verifies it before finishing
tools: ["Read", "Edit", "Write", "Bash"]
model: sonnet
---
You are the implementer. You implement only the single task you are given.
After implementing, you must run the given verification command and fix any failures before finishing.
Do not touch files outside scope. Do not claim completion while verification fails.

Step 3: Run it (the loop runner)

This script has the planner emit the next tasks as JSON, then runs each one through the worker with verification. It stops when done:true comes back or the iteration cap is reached. Every flag is confirmed working on Claude Code 2.x.

#!/usr/bin/env bash
set -euo pipefail

GOAL="${1:?usage: loop.sh \"<goal in one sentence>\"}"
MAX_ITERS="${MAX_ITERS:-8}"      # iteration cap (runaway brake)
BUDGET="${BUDGET:-5}"            # cost cap per call (USD)

# Schema that constrains the planner's output
SCHEMA='{"type":"object","properties":{
  "done":{"type":"boolean"},
  "tasks":{"type":"array","items":{"type":"object","properties":{
    "title":{"type":"string"},
    "prompt":{"type":"string"},
    "verify":{"type":"string"}
  },"required":["title","prompt","verify"]}}
},"required":["done","tasks"]}'

for ((i=1; i<=MAX_ITERS; i++)); do
  echo "──── iteration $i / $MAX_ITERS ────"

  # 1) Let the AI decide what's next (read-only, structured output)
  envelope=$(claude -p "Goal: $GOAL
Inspect the current state and emit the next independent tasks (at most 3) as JSON. If everything is done, set done:true." \
    --agent planner \
    --output-format json \
    --json-schema "$SCHEMA" \
    --max-budget-usd "$BUDGET")

  plan=$(echo "$envelope" | jq -r '.result')   # the body is in .result
  if [ "$(echo "$plan" | jq -r '.done')" = "true" ]; then
    echo "Goal reached. Stopping."; break
  fi

  # 2) Implement and verify each task one by one
  echo "$plan" | jq -c '.tasks[]' | while read -r t; do
    title=$(echo "$t"  | jq -r '.title')
    prompt=$(echo "$t" | jq -r '.prompt')
    verify=$(echo "$t" | jq -r '.verify')
    echo "▶ $title"
    claude -p "Implement this task:
$prompt

When done, you must run this to confirm the result and fix it if it fails:
$verify" \
      --agent worker \
      --permission-mode acceptEdits \
      --max-budget-usd "$BUDGET"
  done
done

Usage is one line.

chmod +x loop.sh
./loop.sh "Raise auth test coverage to 80%"

Deciding "what to do" is the planner's job, not yours. What you write shrinks to one sentence of goal, plus the loop itself.

Step 4: Brakes (stop runaways and cost)

Left alone, a loop keeps running and the bill keeps climbing. Stop it three ways.

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          { "type": "command",
            "command": "grep -qiE 'rm -rf|git push --force|drop table' <<< \"$CLAUDE_TOOL_INPUT\" && exit 2 || exit 0" }
        ]
      }
    ]
  }
}

Start the worker's permissions at --permission-mode acceptEdits (edits auto, everything else confirmed) and widen to dontAsk or bypassPermissions only once you trust it. Do not reach for --dangerously-skip-permissions.

Copy-paste: instruction prompts

For trying the same flow inside an interactive session, without the script.

Make the planner decide what's next

You are the planner. Do not write code.
Goal: "__your goal__"
Inspect the current state of the repo and list the next independent, small tasks (at most 3),
each with a verification command.
If the goal is already met, just answer "done".

Make the worker implement and self-verify

Implement only the following task (do not touch anything out of scope):
__task__
When finished, run this, paste the result, and fix it if it failed before saying done:
__verification command__

Self-run one round (mini loop)

Run one cycle of "inspect state → decide the next single task → implement → verify".
If verification passes, ask me in one line whether to proceed to the next cycle.
For cost and safety, keep each cycle to a single concern.

Summary

The heart of a self-running loop is not one clever prompt; it is a system that separates "decide, build, verify" and runs them in a loop. Fix assumptions with memory (CLAUDE.md), split roles with subagents, run with the runner, and stop with caps and hooks. Install it only for tasks that fit, and your job shifts to "writing the goal in one sentence" and "fixing the loop".