Writing Effective /loop Prompts for Claude Code Automation
Writing Effective /loop Prompts for Claude Code Automation
The /loop command is only as effective as the prompts you give it. A vague prompt produces unreliable automation. A well-crafted prompt transforms Claude into a dependable teammate.
This guide teaches prompt engineering specifically for scheduled tasks—the patterns that work, the mistakes to avoid, and the templates you can adapt.
The Anatomy of a /loop Prompt
Every effective loop prompt has four components:
/loop [interval] [action] [conditions] [output]- Interval: How often to check (5m, 1h, daily at 9am)
- Action: What to do
- Conditions: When to act vs. skip
- Output: What to report back
Example Breakdown
/loop 10m check CI on PR #42:If failed, read logs, identify the error, fix it, and push.Report what was fixed.- Interval: Every 10 minutes
- Action: Check CI, read logs, fix error, push
- Conditions: “If failed” (skip if passing)
- Output: “Report what was fixed”
Prompt Patterns That Work
The Monitor Pattern
Simple observation with reporting:
/loop 5m check if deployment finished and tell me what happenedUse when you want status updates without automated action.
The Fix Pattern
Detect and resolve issues:
/loop 10m check CI on PR #42. If failed:1. Read the error logs2. Identify the root cause3. Create a minimal fix4. Push and comment what was fixedUse when issues have clear resolution paths.
The Alert Pattern
Watch for threshold conditions:
/loop 15m check staging error rate:- If < 1%: log "healthy"- If 1-3%: log "elevated"- If > 3%: send Slack alert and log detailsUse for monitoring that needs human attention at certain thresholds.
The Summarize Pattern
Aggregate information on a schedule:
/loop daily at 9am:Review Slack channels I'm in for mentions of my name.Summarize key discussions that need my attention.Use for morning briefings, daily reports, or regular check-ins.
Common Prompt Mistakes
Too Vague
❌ Bad:
/loop fix my PRsWhat PRs? What issues? What fixes? Claude has no guidance.
✅ Good:
/loop 10m check all PRs I'm assigned to.If CI failed, read the error, create a fix, and push.Report what was fixed.No Feedback Loop
❌ Bad:
/loop 5m watch the buildClaude watches but never tells you anything. Is it even working?
✅ Good:
/loop 5m check the build status.If status changed, report the new status.If still running, log a heartbeat.Unbounded Actions
❌ Bad:
/loop fix all issues in the repoThis could run forever, making changes across the entire codebase.
✅ Good:
/loop 10m check PR #42 only.Fix CI failures in this PR.Maximum 3 fixes per iteration.Missing State Management
❌ Bad:
/loop 10m check for new issues and fix themClaude might fix the same issue multiple times or lose track of what’s done.
✅ Good:
/loop 10m check for new GitHub issues:1. Read .issue-state.json for last checked issue number2. Get issues newer than that3. For each new issue: - Label based on content - Assign if clear owner - Comment with initial analysis4. Update .issue-state.json with latest issue number5. Summarize what was processedEssential Prompt Elements
1. Scope
Define exactly what to check:
# Too broad/loop monitor my services
# Properly scoped/loop 15m monitor staging-api and staging-worker2. Trigger
Specify when to act:
/loop 10m check PR #42:# When to actIf CI status changed OR new comments arrived: [take action]Otherwise: log "no changes"3. Action
Detail what to do:
# Vaguefix it
# Specific1. Read the TypeScript error2. Identify the affected file and line3. Add the missing type annotation4. Run type-check to verify5. Commit with message "fix: add missing type for X"4. Feedback
Require output:
# No feedback/loop 10m check PR #42
# With feedback/loop 10m check PR #42Output ONE of:- "CI passing, no new comments"- "CI failing: [error summary]"- "New comment on line X: [summary]"- "Fixed: [what was fixed]"5. Safety
Set limits:
/loop 10m babysit PR #42:Safety limits:- Only modify files in src/auth/- Maximum 5 files changed per iteration- Never force push- Always create new commits, never amendAdvanced Techniques
Combining with Other Commands
Loop over skills and commands:
/loop 20m /review-pr 1234Each iteration runs the review skill as if you typed it.
Using Natural Language Timing
/loop check the build every 2 hours/loop remind me at 3pm to push the release branch/loop every morning summarize overnight alertsClaude parses these into proper schedules.
Conditional Logic
/loop 30m monitor staging deployment:Conditions:1. If health check returns 200 AND error_rate < 1%: - Log "healthy" with metrics
2. If health check fails: - Attempt automatic rollback - Alert #deployments team - Log incident details
3. If error_rate >= 1% but < 3%: - Log "degraded" with error sample - No automatic action
4. If error_rate >= 3%: - Rollback immediately - Alert #deployments - Create incident ticketPrompt Templates
PR Babysitting
/loop [interval] babysit PR #[number]:1. Check CI status2. If failed: - Read error logs - Identify root cause - Create minimal fix - Push with descriptive message3. Check for new review comments4. For each unresolved comment: - Understand the feedback - Make requested changes - Push and reply5. Update .pr-state.json6. Output summary lineDeployment Monitor
/loop [interval] monitor [service]:1. Check /health endpoint2. Check metrics endpoint for error_rate3. Apply threshold logic: - Healthy: log status - Degraded: log warning with details - Critical: rollback and alert4. Record in .monitor-log.jsonDaily Summary
/loop daily at [time]:1. Review [source] for the last 24 hours2. Identify [topics of interest]3. Summarize key items4. Output to [destination]Issue Triage
/loop [interval] check for new issues:1. Get issues created since last check2. For each new issue: - Read title and body - Apply appropriate labels - Assign if clear owner - Add initial comment with analysis3. Record last checked issue ID4. Summarize new issues processedTesting Your Prompts
Before running a loop for hours, test it:
- Run once manually: Execute the core prompt without
/loop - Check output: Is it what you expected?
- Verify safety: Did it touch the right files only?
- Start short: Begin with a 5-minute interval
- Monitor closely: Watch the first few iterations
- Adjust: Refine the prompt based on behavior
The Bottom Line
Effective /loop prompts are:
- Specific: Clear scope, triggers, and actions
- Stateless: Each iteration works independently
- Safe: Bounded actions with limits
- Observable: Always output status
A well-crafted prompt turns Claude into a reliable automation engine. A vague prompt creates unpredictable behavior. Take the time to write it right.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments