Human-in-the-Loop
Implement approval workflows — escalation, pausing, and async approval patterns.
Pattern 1: Synchronous Callback
For real-time approval:
const agent = defineAgent({
// ...
policy: {
tools: [
{ pattern: 'delete*', permission: 'escalate', reason: 'Destructive action' },
],
onEscalation: async (request) => {
const approved = await promptUser(
`Agent wants to call ${request.toolName}. Approve?`
);
return approved ? 'approve' : 'deny';
},
},
});Pattern 2: Async Approval (Pause/Resume)
For human review that takes time:
// 1. Run the agent
const result = await agent.run({ task: 'Process sensitive data' });
// 2. If paused, send for review
if (result.status === 'awaiting_approval') {
await notifyReviewer({
runId: result.runId,
toolName: result.trace.steps.at(-1)?.toolCall?.name,
reason: 'Requires human approval',
});
}
// 3. Later, when reviewer approves
const resumed = await agent.approve(result.runId, true);Pattern 3: Cost-Based Escalation
Pause when costs get high:
policy: {
costEscalationThreshold: 1.00,
onEscalation: async (request) => {
if (request.type === 'budget_warning') {
return 'pause'; // Let a human decide
}
return 'approve';
},
},Pattern 4: Step-Based Escalation
Pause after N steps:
policy: {
stepEscalationThreshold: 10,
onEscalation: async (request) => {
if (request.type === 'step_limit') {
console.log(`Agent has taken ${request.context.stepCount} steps`);
return 'pause';
}
return 'approve';
},
},Next Steps
- Autonomy Policy — policy configuration
- Escalation — escalation handling
- Resume — resuming paused runs