Autonomous AI agent loop for Claude Code - Execute PRD user stories automatically
npm install claude-ralph


Autonomous AI Agent Loop for Claude Code




Run Claude Code repeatedly until all PRD items are complete. Each iteration is a fresh instance with clean context.
Based on Geoffrey Huntley's Ralph pattern ๐ฉ
---
| Feature | Ralph CLI | Claude Code Built-in |
|---------|-----------|---------------------|
| Context Management | โ
Fresh context each iteration | โ Context accumulates and degrades |
| Memory Persistence | โ
Git + progress.txt + prd.json | โ Lost between sessions |
| Multi-repo Support | โ
Native support for monorepos | โ Single repo at a time |
| Quality Gates | โ
Automated checks between stories | โ Manual verification |
| Progress Tracking | โ
Structured PRD with status | โ No built-in tracking |
| Branch Management | โ
Auto-create, fork, checkout | โ Manual git operations |
| Iteration Control | โ
Configurable max iterations | โ No automatic limits |
The key insight: Claude Code's context window fills up over long sessions, degrading output quality. Ralph solves this by spawning fresh instances for each story, using files as persistent memory instead of relying on in-context state.
> ๐ก "The 'Ralph' pattern is about context hygiene. Each iteration starts clean, with only the essential context loaded from files."
---
- ๐ค Why Ralph CLI?
- ๐ How It Works
- ๐ฆ Installation
- โก Quick Start
- ๐ป CLI Commands
- ๐ Prerequisites
- โ๏ธ Configuration Reference
- ๐ Story Guidelines
- ๐ Branch Forking
- ๐ Archiving
- ๐ ๏ธ Development
- ๐ References
- ๐ License
---
```
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ Ralph Workflow โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ 1. ralph init ๐ฏ Initialize configuration โ
โ โ โ
โ 2. ralph plan
โ โ โ
โ 3. ralph prd ๐ง Convert plan to prd.json โ
โ โ โ
โ 4. ralph run ๐ค Execute stories autonomously โ
โ โ โ
โ 5. Commits + PRD โ
Each story committed โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
---
`bash`
npm install -g claude-ralph
Or use npx without installing:
`bash`
npx claude-ralph
---
`bash`
cd your-project
ralph init
This creates ralph.config.json with your project settings.
Edit ralph.config.json:
`json`
{
"project": "MyProject",
"description": "My awesome project",
"repositories": {
"backend": {
"path": "./backend",
"checks": ["pytest", "mypy ."]
},
"frontend": {
"path": "./frontend",
"checks": ["npm run build", "npm run lint"]
}
}
}
Single repository? Just use one entry:
`json`
{
"project": "MyProject",
"repositories": {
"main": {
"path": ".",
"checks": ["npm run build", "npm test"]
}
}
}
`bash`
ralph plan "Add user authentication with OAuth support"
Claude will:
1. ๐ค Ask 3-5 clarifying questions
2. ๐ Generate plan.md with structured user stories
3. โ
Ask for validation
Review and edit plan.md if needed, then:
`bash`
ralph prd
This converts the plan to prd.json.
`bash`
ralph run
Ralph will:
1. ๐ฏ Pick the highest priority story with passes: falseprd.json
2. ๐ Navigate to the correct repository
3. ๐ฟ Create/checkout the feature branch
4. ๐ป Implement the story
5. ๐งช Run quality checks
6. โ
Commit if checks pass
7. ๐ Update and repeat
`bash`
ralph status
ralph status --verbose
---
| Command | Description |
|---------|-------------|
| ralph init | ๐ฏ Initialize Ralph configuration |ralph plan
| | ๐ Generate a structured implementation plan |ralph prd
| | ๐ง Convert plan.md to prd.json |ralph run
| | ๐ค Run the autonomous agent loop |ralph status
| | ๐ Show current progress and status |
Initialize Ralph configuration in the current directory.
`bash`
ralph init
ralph init --force # Overwrite existing config
Generate a structured implementation plan.
`bash`
ralph plan "Add a notification system with email and push support"
ralph plan "Refactor the auth module" --output custom-plan.md
Convert plan.md to prd.json.
`bash`
ralph prd
ralph prd --input custom-plan.md --output custom-prd.json
Run the autonomous agent loop.
`bash`
ralph run
ralph run --max-iterations 10 # Limit iterations
Show current progress and status.
`bash`
ralph status
ralph status --verbose # Show detailed information
---
- ๐ง Claude Code CLI installed and authenticated
- ๐ฆ Node.js 18 or later
---
`json`
{
"$schema": "https://raw.githubusercontent.com/anthropics/ralph/main/schema/ralph.config.schema.json",
"version": "1.0",
"project": "MyProject",
"description": "Optional project description",
"repositories": {
"repo-key": {
"path": "./relative/path",
"defaultBranch": "main",
"checks": [
"command1",
"command2"
]
}
},
"agent": {
"maxIterations": 50,
"timeout": 600
}
}
Configuration can also be stored in:
- .ralphrc.ralphrc.json
- .ralphrc.yaml
- package.json
- under the "ralph" key
`json`
{
"project": "Feature Name",
"description": "What this feature does",
"repositories": {
"backend": { "branchName": "feature/my-feature" },
"frontend": { "branchName": "feature/my-feature" }
},
"userStories": [
{
"id": "US-001",
"title": "Add user model",
"repo": "backend",
"description": "As a developer, I need a user model...",
"acceptanceCriteria": [
"User model created with email, password fields",
"Migration generated",
"Tests pass"
],
"priority": 1,
"passes": false,
"fork": false,
"notes": ""
}
]
}
---
---
For significant direction changes, use fork: true in a story:
`json`
{
"id": "US-005",
"title": "Refactor auth system",
"fork": true,
"passes": false
}
Ralph will create a new branch (e.g., feature/auth-2) from the current one.
---
Ralph automatically archives previous runs when you start a new project. Archives are saved in archive/YYYY-MM-DD-project-name/.
---
To build from source:
`bash``
git clone https://github.com/anthropics/ralph.git
cd ralph
npm install
npm run build
npm link # Makes 'ralph' command available globally
---
- ๐ฉ Geoffrey Huntley's Ralph article
- ๐ง Claude Code documentation
---
MIT License - see LICENSE
---
Made with ๐ by the community