Ralph Wiggum Pattern
⎿ Implementation of the iterative, self-referential AI development loop
"I'm doing agentic engineering!" - Ralph
Figure 1: The Autonomous Feedback Loop
Ralph is an autonomous AI agent loop that runs Gemini CLI repeatedly until all PRD items are complete. Each iteration is a fresh Gemini instance with clean context. Memory persists via git history, progress.txt, and prd.json.
Based on Geoffrey Huntley's Ralph pattern.
- • Gemini CLI installed and authenticated
- •
jqinstalled (brew install jqon macOS) - • A git repository for your project
Option 1: Install as a Skill
Install the skill directly into your project or globally using the Gemini Templates CLI.
npx gemini-cli-templates --skill development/ralph-wiggum
Option 2: Manual Setup
Copy the ralph files into your project:
mkdir -p scripts/ralph
cp -r cli-tool/components/skills/development/ralph-wiggum/scripts/* scripts/ralph/
chmod +x scripts/ralph/ralph.sh
Configure Auto-Handoff (Recommended)
Add to ~/.gemini/settings.json:
"gemini.experimental.autoHandoff": { "context": 90 }
}
This enables automatic handoff when context fills up, allowing Ralph to handle large stories that exceed a single context window.
Create a PRD
Use a PRD skill or manually create a detailed requirements document. Save it to tasks/prd-[feature-name].md.
Convert PRD to Ralph Format
You need a prd.json file with user stories structured for autonomous execution.
"feature": "My Feature",
"branchName": "feat/my-feature",
"userStories": [
{
"id": "1",
"title": "Setup project structure",
"passes": false
}
]
}
Run Ralph
./scripts/ralph/ralph.sh 10
- Create a feature branch (from PRD
branchName) - Pick the highest priority story where
passes: false - Implement that single story
- Run quality checks (typecheck, tests)
- Commit if checks pass
- Update
prd.jsonto mark story aspasses: true - Append learnings to
progress.txt - Repeat until all stories pass or max iterations reached
| File | Purpose |
|---|---|
| ralph.sh | The bash loop that spawns fresh Gemini instances |
| prompt.md | Instructions given to each Gemini instance |
| prd.json | User stories with passes status (the task list) |
| prd.json.example | Example PRD format for reference |
| progress.txt | Append-only learnings for future iterations |
Each Iteration = Fresh Context
Each iteration spawns a new Gemini instance with clean context. The only memory between iterations is:
- Git history (commits from previous iterations)
progress.txt(learnings and context)prd.json(which stories are done)
Small Tasks
Each PRD item should be small enough to complete in one context window. If a task is too big, the LLM runs out of context before finishing and produces poor code.
- Add a database column and migration
- Add a UI component to an existing page
- Update a server action with new logic
- Add a filter dropdown to a list
- "Build the entire dashboard"
- "Add authentication"
- "Refactor the API"
AGENTS.md Updates Are Critical
After each iteration, Ralph updates the relevant AGENTS.md files with learnings. This is key because Gemini automatically reads these files, so future iterations (and future human developers) benefit from discovered patterns, gotchas, and conventions.
Feedback Loops
Ralph only works if there are feedback loops:
- Typecheck catches type errors
- Tests verify behavior
- CI must stay green (broken code compounds across iterations)
cat prd.json | jq '.userStories[] | {id, title, passes}'
# See learnings from previous iterations
cat progress.txt
# Check git history
git log --oneline -10
Original technique: https://ghuntley.com/ralph/
Upstream Repository: https://github.com/snarktank/ralph