16 min read
Lesson 5 of 5
Human-in-the-Loop & Production Deployment
From the course AI Agents: Building Autonomous Systems
Production agents need human oversight, not just for safety but for trust, compliance, and continuous improvement. The right level of human involvement depends on the stakes.
Human-in-the-Loop Patterns
Approval gates: Agent pauses and requests human approval before high-stakes actions.
def execute_with_approval(action, risk_level):
if risk_level == "low":
return execute(action) # Auto-execute
elif risk_level == "medium":
notify_human(action) # Notify but proceed
return execute(action)
elif risk_level == "high":
approval = request_human_approval(action) # Wait for approval
if approval:
return execute(action)
return "Action cancelled by human reviewer"
Escalation triggers: Agent recognizes when it's out of its depth:
- Confidence below threshold
- User expresses frustration
- Task involves regulated domains (medical, legal, financial)
- Error count exceeds limit for current task
Collaborative mode: Human and agent work together:
- Agent proposes actions, human confirms or modifies
- Human provides guidance, agent handles execution details
- Agent drafts, human reviews and approves
Production Deployment Checklist
Infrastructure:
- ☐ Stateless agent service with database-backed state
- ☐ Message queue for async tool execution
- ☐ Timeout handling for every external call
- ☐ Graceful degradation when services are unavailable
- ☐ Auto-scaling based on agent workload
Safety:
- ☐ Maximum iteration limits (prevent runaway agents)
- ☐ Maximum cost per task (prevent budget overruns)
- ☐ Sandboxed code execution (if applicable)
- ☐ PII detection in inputs and outputs
- ☐ Content safety filters on all generated content
Observability:
- ☐ Full trace logging of every agent step
- ☐ Cost tracking per task and per user
- ☐ Latency monitoring (time to complete tasks)
- ☐ Success/failure rate tracking
- ☐ Human escalation rate monitoring
User Experience:
- ☐ Show agent's thinking process (builds trust)
- ☐ Allow users to interrupt and redirect
- ☐ Provide estimated completion time
- ☐ Show progress for multi-step tasks
- ☐ Clear error messages when tasks fail
Evaluation & Improvement
Measure agent performance with:
| Metric | Description | Target |
|---|---|---|
| Task completion rate | % of tasks fully completed | >80% |
| Accuracy | Correctness of completed tasks | >90% |
| Efficiency | Steps taken vs. optimal path | <2× optimal |
| Human escalation rate | % of tasks needing human help | <20% |
| Cost per task | Average API + compute cost | Within budget |
| User satisfaction | Post-task rating | >4/5 |
Continuous improvement loop:
- Review failed and escalated tasks weekly
- Identify common failure patterns
- Improve tools, prompts, or add new capabilities
- A/B test changes against the previous version
- Gradually increase autonomy as reliability improves
Key Takeaways
- •Match human oversight level to risk: auto-execute low-risk, require approval for high-risk
- •Agents should recognize when they're out of their depth and escalate proactively
- •Production deployment requires iteration limits, cost caps, sandboxing, and full observability
- •Gradually increase agent autonomy as reliability improves — measure before expanding scope
0 of 5 lessons marked complete
Progress is kept in this browser only.