Recipes
Human approval for AI agents
Pause an AI agent before a risky action, call a person to approve or reject, and continue with the answer.
Your agent wants to do something important — refund money, deploy, delete data. Before acting, it calls a human and waits.
1. The approval function
const CALLHUMAN = "https://api.callhuman.es/v1/calls";
const headers = {
Authorization: `Bearer ${process.env.CALLHUMAN_API_KEY}`,
"Content-Type": "application/json",
};
/** Calls the approver and resolves to true (approved) or false. */
export async function askForApproval({ phone, action }) {
const created = await fetch(CALLHUMAN, {
method: "POST",
headers,
body: JSON.stringify({
to: phone,
message: `Your agent wants to ${action}.`,
options: [
{ label: "Approve", reply: "Approved. Going ahead." },
{ label: "Reject", reply: "Rejected. Nothing will happen." },
],
}),
}).then((r) => r.json());
// Poll until the call ends (or use a webhook in production).
for (;;) {
await new Promise((r) => setTimeout(r, 5000));
const call = await fetch(`${CALLHUMAN}/${created.id}`, { headers }).then((r) => r.json());
if (["answered", "not_answered", "failed"].includes(call.status)) {
return call.status === "answered" && call.option_label === "Approve";
}
}
}2. Use it as a tool
const approved = await askForApproval({
phone: "+34600333444",
action: "refund 1,240 euros to Acme Ltd for invoice 2231",
});
if (!approved) throw new Error("Refund not approved by a human");
await refund(invoice);CallHuman: Your agent wants to refund 1,240 euros to Acme Ltd for invoice 2231.
CallHuman: Press 1 to approve. Press 2 to reject.
Person: 1pressed
CallHuman: Approved. Going ahead.
Tips
- Not answered means no. Treat
not_answeredas a rejection, or escalate to a second approver. - Say exactly what will happen, with amounts and names: the person decides only from what they hear.
- For agents that support MCP, a native server is coming soon.