Overview
Human-in-the-loop workflows in CopilotKit enable:- User approval before executing critical actions
- Action review with custom UI components
- Rejection handling to stop unwanted operations
- Interactive confirmations with visual feedback
- Multi-step approvals for complex operations
What You’ll Learn
This guide teaches you how to:- Implement
renderAndWaitfor approval workflows - Set up agent interrupts with LangGraph
- Build custom approval UI components
- Handle approval and rejection responses
- Create form-filling assistants with HITL
- Test and debug HITL workflows
Example: Form Filling Assistant
The Form Filling example demonstrates a security incident report form that uses AI to fill fields through conversation, with the user maintaining full control.
Live Demo: https://copilotkit.ai/examples/form-filling-copilot
Source Code: GitHub
Prerequisites
- Node.js 18 or higher
- npm, yarn, or pnpm
- Copilot Cloud Public API Key or OpenAI API Key
Quick Start
Open Application
Navigate to http://localhost:3000
Core Concepts
1. Basic HITL Pattern
The simplest human-in-the-loop implementation:renderdisplays UI and waits for user interactionrespond({ approved: true, data })approves and continues with datarespond({ approved: false })rejects and cancels the actionstatustracks completion state
2. Form Filling Implementation
File:components/IncidentReportForm.tsx
app/page.tsx
- User says: “I need to report a security incident from yesterday”
- AI reads user context (name, email) from
useAgentContext - AI calls
fillIncidentReportFormwith extracted information - Form fields are automatically populated
- User reviews and can edit before submitting
3. Trip Approval with Custom UI
From the Travel Planner example - more complex HITL with custom approval components: File:lib/hooks/use-trips.tsx
components/humanInTheLoop/EditTrips.tsx
4. Agent-Side Interrupts with LangGraph
For multi-agent systems, you need to configure interrupts on the agent side: File:agent/src/agent.py
- Agent executes until
trips_nodecompletes - Execution pauses (interrupt)
- Frontend receives tool call and shows approval UI via
render - User approves or rejects
- Response sent back to agent
- Agent continues execution or stops based on response
Advanced Patterns
Multi-Step Approval
For complex workflows with multiple approval steps:Conditional Approval
Only require approval for certain conditions:Approval with Modifications
Allow users to modify data before approval:Best Practices
1. Clear Visual Feedback
Always show clear approval/rejection states:2. Show Context
Display all relevant information for informed decisions:3. Handle Errors Gracefully
4. Provide Keyboard Shortcuts
Testing HITL Workflows
Unit Testing
Integration Testing
Test the full flow with Playwright:Troubleshooting
Approval UI Not Showing
Approval UI Not Showing
Check:
- You’re using
useHumanInTheLoopwith arenderfunction - The agent has
interrupt_afterconfigured (for LangGraph) - The action name matches between frontend and agent
- The checkpointer is enabled in your agent
Respond Function Not Working
Respond Function Not Working
Verify:
- You’re calling
respond({ approved: true, data })to approve - You’re calling
respond({ approved: false })to reject - The respond function is being called in response to user action
- You’re not calling respond during render (use onClick, etc.)
Status Not Updating
Status Not Updating
Ensure:
- You’re checking
status === ToolCallStatus.Completeto hide approval UI - The component re-renders when status changes
- You’re not preventing re-renders with React.memo incorrectly
Next Steps
Generative UI
Build custom UI components for approvals
Multi-Agent
Implement HITL in multi-agent systems
useHumanInTheLoop
Complete API reference
LangGraph Interrupts
LangGraph documentation on interrupts
