Skip to content

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

Terminal window
/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:

Terminal window
/loop 5m check if deployment finished and tell me what happened

Use when you want status updates without automated action.

The Fix Pattern

Detect and resolve issues:

Terminal window
/loop 10m check CI on PR #42. If failed:
1. Read the error logs
2. Identify the root cause
3. Create a minimal fix
4. Push and comment what was fixed

Use when issues have clear resolution paths.

The Alert Pattern

Watch for threshold conditions:

Terminal window
/loop 15m check staging error rate:
- If < 1%: log "healthy"
- If 1-3%: log "elevated"
- If > 3%: send Slack alert and log details

Use for monitoring that needs human attention at certain thresholds.

The Summarize Pattern

Aggregate information on a schedule:

Terminal window
/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:

Terminal window
/loop fix my PRs

What PRs? What issues? What fixes? Claude has no guidance.

Good:

Terminal window
/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:

Terminal window
/loop 5m watch the build

Claude watches but never tells you anything. Is it even working?

Good:

Terminal window
/loop 5m check the build status.
If status changed, report the new status.
If still running, log a heartbeat.

Unbounded Actions

Bad:

Terminal window
/loop fix all issues in the repo

This could run forever, making changes across the entire codebase.

Good:

Terminal window
/loop 10m check PR #42 only.
Fix CI failures in this PR.
Maximum 3 fixes per iteration.

Missing State Management

Bad:

Terminal window
/loop 10m check for new issues and fix them

Claude might fix the same issue multiple times or lose track of what’s done.

Good:

Terminal window
/loop 10m check for new GitHub issues:
1. Read .issue-state.json for last checked issue number
2. Get issues newer than that
3. For each new issue:
- Label based on content
- Assign if clear owner
- Comment with initial analysis
4. Update .issue-state.json with latest issue number
5. Summarize what was processed

Essential Prompt Elements

1. Scope

Define exactly what to check:

Terminal window
# Too broad
/loop monitor my services
# Properly scoped
/loop 15m monitor staging-api and staging-worker

2. Trigger

Specify when to act:

Terminal window
/loop 10m check PR #42:
# When to act
If CI status changed OR new comments arrived:
[take action]
Otherwise:
log "no changes"

3. Action

Detail what to do:

Terminal window
# Vague
fix it
# Specific
1. Read the TypeScript error
2. Identify the affected file and line
3. Add the missing type annotation
4. Run type-check to verify
5. Commit with message "fix: add missing type for X"

4. Feedback

Require output:

Terminal window
# No feedback
/loop 10m check PR #42
# With feedback
/loop 10m check PR #42
Output 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:

Terminal window
/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 amend

Advanced Techniques

Combining with Other Commands

Loop over skills and commands:

Terminal window
/loop 20m /review-pr 1234

Each iteration runs the review skill as if you typed it.

Using Natural Language Timing

Terminal window
/loop check the build every 2 hours
/loop remind me at 3pm to push the release branch
/loop every morning summarize overnight alerts

Claude parses these into proper schedules.

Conditional Logic

Terminal window
/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 ticket

Prompt Templates

PR Babysitting

Terminal window
/loop [interval] babysit PR #[number]:
1. Check CI status
2. If failed:
- Read error logs
- Identify root cause
- Create minimal fix
- Push with descriptive message
3. Check for new review comments
4. For each unresolved comment:
- Understand the feedback
- Make requested changes
- Push and reply
5. Update .pr-state.json
6. Output summary line

Deployment Monitor

Terminal window
/loop [interval] monitor [service]:
1. Check /health endpoint
2. Check metrics endpoint for error_rate
3. Apply threshold logic:
- Healthy: log status
- Degraded: log warning with details
- Critical: rollback and alert
4. Record in .monitor-log.json

Daily Summary

Terminal window
/loop daily at [time]:
1. Review [source] for the last 24 hours
2. Identify [topics of interest]
3. Summarize key items
4. Output to [destination]

Issue Triage

Terminal window
/loop [interval] check for new issues:
1. Get issues created since last check
2. For each new issue:
- Read title and body
- Apply appropriate labels
- Assign if clear owner
- Add initial comment with analysis
3. Record last checked issue ID
4. Summarize new issues processed

Testing Your Prompts

Before running a loop for hours, test it:

  1. Run once manually: Execute the core prompt without /loop
  2. Check output: Is it what you expected?
  3. Verify safety: Did it touch the right files only?
  4. Start short: Begin with a 5-minute interval
  5. Monitor closely: Watch the first few iterations
  6. 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