Overview
Create a turn-based system where an AI agent posts status updates to a GitHub pull request and executes tasks based on PR review comments. This enables collaborative workflows where reviewers guide the agentâs work just by leaving comments.1
Set up the initial PR and post agent status
Create a pull request and post an initial status comment from the agent. This establishes the communication channel for turn-based interaction.
2
Monitor PR comments and process agent tasks
Set up a loop that monitors the PR for new comments, processes them as agent prompts, and posts progress updates. Use the
-r flag to resume the most recent Claude Code conversation across multiple PR comments.Named Shells: We use a named shell (
devbox.shell("agent-shell")) to maintain the working directory state across commands. After the initial cd ~/sample-todo-nextjs, all subsequent commands run in that directory without needing to cd each time. Learn more about named shells.Session Resumption: The -r flag without a session ID will resume the most recent Claude Code conversation. On the first call, it starts a new session, and on subsequent calls, it automatically resumes the previous conversation, maintaining context across multiple PR comments.Production Webhooks: In production, you should use GitHub webhooks to receive real-time notifications when comments are added, rather than polling. This polling approach is shown for simplicity, but webhooks are more efficient and responsive.3
Add error handling and status updates
Enhance the workflow with better error handling and more detailed status updates to keep reviewers informed.
4
Add command filtering and special instructions
Add support for special commands and filtering to make the interaction more controlled and useful.
Best Practices
- Use webhooks in production: Replace polling with GitHub webhooks for real-time notifications
- Rate limiting: Be mindful of GitHub API rate limits when polling frequently; authenticating with a GitHub token or GitHub App increases your available quota
- Error recovery: Implement retry logic for transient failures
- Security: Validate and sanitize user input and secrets before passing them to the agent
- Logging: Keep detailed logs of agent actions for debugging and auditing
- Session management: Use
claude -r -p "query"to resume the most recent conversation. The-rflag without a session ID automatically resumes the most recent session, maintaining context across multiple PR comments - Claude Code flags:
- Use
-pflag for print mode (non-interactive, SDK usage) - Use
-ror--resumewithout a session ID to resume the most recent session - Use
-r "<session-id>"or--resume "<session-id>"to resume a specific session by ID - Use
-cto continue the most recent conversation in the current directory - For more production examples of webhook-based workflows and secret management, see the Runloop tutorials and Account Secrets documentation.
- Use
Next Steps
- Learn how to share a live preview of your changes
- Explore suspend and resume workflows for long-running interactions
- Check out GitHub webhooks documentation for production-ready implementations
