# Human-in-the-loop
Your agents pause for human approval and resume the moment any client responds. AI Transport carries the pending request in the durable session, so the user approves from any device, on any timeline.
Human-in-the-loop uses the tool-calling primitives to create approval gates. The agent requests approval, the turn pauses, and any connected client approves or rejects. Because the session is durable, the approval request reaches the user even after a reconnect or device switch.
## How it works
The pattern builds on [tool calling](https://ably.com/docs/ai-transport/features/tool-calling.md?source=llms.txt). The agent defines a tool that requires human approval. When the LLM invokes that tool, the turn ends with a pending tool call. The client presents the approval request to the user. When the user approves or rejects, the client submits the result via `view.update()`, which triggers a continuation turn.
The flow:
1. The agent streams a response that includes a tool call requiring approval.
2. The turn ends. The pending tool call is published to the channel.
3. Any connected client renders the pending approval.
4. The user approves or rejects. The client calls `view.update()` with the result.
5. A continuation turn starts. The agent receives the approval result and proceeds.
## Define an approval tool
On the server, define a tool without an `execute` function. The tool's description tells the LLM when to request approval:
### Javascript
```
const result = streamText({
model: anthropic('claude-sonnet-4-20250514'),
messages: conversationHistory,
tools: {
requestApproval: {
description: 'Request user approval before executing a sensitive action',
inputSchema: z.object({
action: z.string().describe('Description of the action to approve'),
details: z.string().describe('Additional context for the user'),
}),
// No execute function: requires client-side approval.
},
executeTransfer: {
description: 'Execute a bank transfer',
inputSchema: z.object({ amount: z.number(), recipient: z.string() }),
execute: async ({ amount, recipient }) => {
return await processTransfer(amount, recipient);
},
},
},
abortSignal: turn.abortSignal,
});
const { reason } = await turn.streamResponse(result.toUIMessageStream());
await turn.end(reason);
```
When the LLM decides an action needs approval, it invokes `requestApproval`. The turn ends with the tool call pending.
## Handle approval on the client
On the client, detect pending approval requests and present them to the user:
### Javascript
```
const { nodes } = useView({ transport });
const pendingNode = nodes.find((n) =>
n.message.parts?.some(
(p) => p.type === 'dynamic-tool' && p.toolName === 'requestApproval' && p.state === 'input-available',
),
);
const pendingApproval = pendingNode?.message.parts?.find(
(p) => p.type === 'dynamic-tool' && p.state === 'input-available',
);
if (pendingNode && pendingApproval) {
const { action, details } = pendingApproval.args;
return (
view.update(pendingNode.id, [{
type: 'tool-output-available',
toolCallId: pendingApproval.toolCallId,
output: { approved: true },
}])
}
onReject={() =>
view.update(pendingNode.id, [{
type: 'tool-output-available',
toolCallId: pendingApproval.toolCallId,
output: { approved: false, reason: 'User declined' },
}])
}
/>
);
}
```
`view.update()` submits the result and triggers a continuation turn. The agent receives `{ approved: true }` as the tool result and proceeds, or receives the rejection and adjusts.
## Approve from any device
The session is a shared Ably channel, so the approval request is visible on every connected device. Any device submits the approval; the first response wins.
A user starts a conversation on a laptop, steps away, and approves the request on a phone. The agent does not know or care which device approved it. The continuation turn starts as soon as any client submits the result.
## Durable approval requests
Approval requests survive disconnections. If the user is offline when the agent requests approval, the pending tool call persists in the channel history. On reconnect, the view loads the conversation including the pending request, and the approval UI appears.
The agent's turn has already ended, so no connection or timeout is at risk. The continuation turn starts only when the user submits their response, minutes, hours, or days later.
## Edge cases and unhappy paths
- Two devices submitting at the same time race. The first `view.update()` wins; the second submits to an already-resolved tool call and the agent ignores it. Guard against double-submit at the application layer if both devices need to see a consistent decision.
- A user who rejects must trigger an agent path that handles rejection. The LLM only sees the output you supply; an empty rejection result is ambiguous.
- A pending approval that never receives a response stays pending forever. Add an explicit timeout in your application if you need one; AI Transport does not impose one.
- The continuation turn runs as a fresh agent invocation. Make sure your server endpoint hydrates the conversation history correctly so the LLM sees the approval result in context.
## FAQ
### How is this different from a regular tool call?
A regular client-executed tool runs as soon as the client receives the call. Human-in-the-loop blocks until a human submits the result. The mechanics are the same; the user experience is different.
### Can the agent see who approved it?
Yes. Each Ably message carries the publisher's `clientId`. Pass approver identity in the output payload if the LLM needs it inline.
### What if the user closes the app before approving?
The pending approval stays on the channel. The user sees it when they next open the app on any device, within the channel's history retention window.
### How do I escalate an unanswered approval?
Set a server-side timer or scheduled job that checks for stale pending tool calls and sends a notification. Use [push notifications](https://ably.com/docs/ai-transport/features/push-notifications.md?source=llms.txt) for app-level escalation.
### Can a non-human submit the approval?
Yes. Any client with publish capability can submit. The mechanism is generic; "human-in-the-loop" is the common use case.
## Related features
- [Tool calling](https://ably.com/docs/ai-transport/features/tool-calling.md?source=llms.txt): the underlying mechanism for human-in-the-loop.
- [Multi-device sessions](https://ably.com/docs/ai-transport/features/multi-device.md?source=llms.txt): approval from any connected device.
- [Reconnection and recovery](https://ably.com/docs/ai-transport/features/reconnection-and-recovery.md?source=llms.txt): approval requests survive disconnections.
## Related Topics
- [Token streaming](https://ably.com/docs/ai-transport/features/token-streaming.md?source=llms.txt): Stream AI-generated tokens to clients in realtime using AI Transport, with support for message-per-response and message-per-token patterns.
- [Cancellation](https://ably.com/docs/ai-transport/features/cancellation.md?source=llms.txt): Cancel AI responses mid-stream with Ably AI Transport. Scoped cancel signals, server-side authorization, and graceful abort handling.
- [Reconnection and recovery](https://ably.com/docs/ai-transport/features/reconnection-and-recovery.md?source=llms.txt): AI Transport streams survive connection drops automatically. Clients reconnect and resume from where they left off with no lost tokens.
- [Multi-device sessions](https://ably.com/docs/ai-transport/features/multi-device.md?source=llms.txt): Share AI conversations across tabs, phones, and laptops with Ably AI Transport. All devices see the same session in real time.
- [History and replay](https://ably.com/docs/ai-transport/features/history.md?source=llms.txt): Load conversation history from Ably channels with AI Transport. Paginated history, gapless continuity, and scroll-back patterns.
- [Conversation branching](https://ably.com/docs/ai-transport/features/branching.md?source=llms.txt): Edit user messages, regenerate AI responses, and navigate branches with Ably AI Transport. The full history is preserved as a tree.
- [Interruption](https://ably.com/docs/ai-transport/features/interruption.md?source=llms.txt): Let users interrupt AI agents mid-stream with Ably AI Transport. Cancel-then-send and send-alongside patterns for responsive AI interactions.
- [Concurrent turns](https://ably.com/docs/ai-transport/features/concurrent-turns.md?source=llms.txt): Run multiple AI turns simultaneously with Ably AI Transport. Independent streams, scoped cancellation, and multi-agent support.
- [Tool calling](https://ably.com/docs/ai-transport/features/tool-calling.md?source=llms.txt): Stream tool invocations and results through Ably AI Transport. Server-executed and client-executed tools with persistent state.
- [Optimistic updates](https://ably.com/docs/ai-transport/features/optimistic-updates.md?source=llms.txt): User messages appear instantly in Ably AI Transport. Optimistic insertion with automatic reconciliation when the server confirms.
- [Agent presence](https://ably.com/docs/ai-transport/features/agent-presence.md?source=llms.txt): Show agent status in your AI application with Ably Presence. Display streaming, thinking, idle, and offline states in real time.
- [Push notifications](https://ably.com/docs/ai-transport/features/push-notifications.md?source=llms.txt): Notify users when AI agents complete background tasks with Ably Push Notifications. Reach users even when they're offline.
- [Chain of thought](https://ably.com/docs/ai-transport/features/chain-of-thought.md?source=llms.txt): Stream reasoning and thinking content alongside responses with Ably AI Transport. Display chain-of-thought in real time.
- [Double texting](https://ably.com/docs/ai-transport/features/double-texting.md?source=llms.txt): Handle users sending multiple messages while the AI is streaming with Ably AI Transport. Queue or run messages concurrently.
## Documentation Index
To discover additional Ably documentation:
1. Fetch [llms.txt](https://ably.com/llms.txt?source=llms.txt) for the canonical list of available pages.
2. Identify relevant URLs from that index.
3. Fetch target pages as needed.
Avoid using assumed or outdated documentation paths.