Skip to main content
Human-in-the-loop (HITL) patterns allow agents to pause execution and wait for human approval, confirmation, or input before proceeding. This is essential for compliance, safety, and oversight in agentic systems.

Overview

Why Human in the Loop?

  • Compliance: Regulatory requirements may mandate human approval for certain actions
  • Safety: High-stakes operations (payments, deletions, external communications) need oversight
  • Quality: Human review catches errors AI might miss
  • Trust: Users feel more confident when they can approve critical actions

Common Use Cases

Choosing an Approach

Agents SDK supports multiple human-in-the-loop patterns. Choose based on your use case:

Decision Guide

Workflow-Based Approval

For durable, multi-step processes, use Cloudflare Workflows with the waitForApproval() helper. The workflow pauses until a human approves or rejects.

Basic Pattern

Agent Methods for Approval

The agent provides methods to approve or reject waiting workflows:

Timeout Handling

Set timeouts to prevent workflows from waiting indefinitely:
If the timeout expires, the workflow continues without approval data. Handle this case:

AI Tool Approval with needsApproval

When building AI chat agents, you often want humans to approve certain tool calls before execution. The AI SDK’s needsApproval option pauses tool execution until the user approves or rejects.

Server

Define tools with needsApproval to require human confirmation:

Client

Handle approval requests with addToolApprovalResponse:

Custom denial messages with addToolOutput

When a user rejects a tool, addToolApprovalResponse({ id, approved: false }) sets the tool state to output-denied with a generic “Tool execution denied.” message. If you need to give the LLM a more specific reason for the denial, use addToolOutput with state: "output-error" instead:
This sends a tool_result to the LLM with your custom error text, so it can respond appropriately (e.g. suggest an alternative, ask clarifying questions). The addToolOutput function also works for tools in approval-requested or approval-responded states, not just input-available.

Client-Side Tool Execution with onToolCall

For tools that need browser APIs (geolocation, camera, clipboard) or user interaction, define the tool on the server without an execute function and handle it on the client with onToolCall:

Server

Client

The server receives the tool output via CF_AGENT_TOOL_RESULT and can auto-continue the conversation (with maxSteps > 1), letting the LLM respond to the location data in the same turn.

State Patterns for Approvals

Track pending approvals in agent state for UI rendering and persistence:

Multi-Approver Patterns

For sensitive operations requiring multiple approvers:

Timeouts and Escalation

Setting Approval Timeouts

Escalation with Scheduling

Use schedule() to set up escalation reminders: