AI Bot to Human Handoff
An AI bot can answer a customer first and hand the same conversation to a person when it needs one. With Stream Chat, both happen in one channel: the customer and the bot talk there, someone from your support team joins it later, and they read everything that was said before they joined. Nothing is copied to a second inbox, and the customer stays in the same chat.
I want an AI support bot to answer first, then hand the same conversation over to a human when needed. What should I use?
Use one Stream Chat channel per conversation, and keep who owns it in the channel's custom data:
- Your backend creates an
agentchannel with the customer and your bot as members, and setshandofftobot. - The bot answers each new message while
handoffisbot. - When the conversation needs a person, the bot or your backend sets
handofftowaiting, and the bot stops answering. - Your support agents' inbox lists the channels where
handoffiswaiting. - An agent takes one: your backend adds the agent as a member, removes the bot and sets
handofftohuman. The agent sees the full history, the bot's answers included.
sequenceDiagram
participant C as Customer
participant B as AI bot
participant S as Your backend
participant A as Support agent
C->>B: Asks a question
B->>C: Answers (handoff: bot)
C->>B: Asks for a person
B->>S: Hands off (handoff: waiting)
A->>S: Takes the conversation
S->>A: Adds the agent, removes the bot (handoff: human)
A->>C: Replies in the same channelThe bot can be one you run yourself, as the sections below show, or one built with the Stream Chat AI SDK.
Start the conversation
Create the channel from your backend when the customer opens the support chat. handoff is a custom field: name it and its values as you like. This page uses bot, waiting and human.
require 'getstream_ruby'
Models = GetStream::Generated::Models
client.chat.get_or_create_channel('agent', 'support-anya-1', Models::ChannelGetOrCreateRequest.new(
data: Models::ChannelInput.new(
created_by_id: 'anya',
members: [
Models::ChannelMemberRequest.new(user_id: 'anya'),
Models::ChannelMemberRequest.new(user_id: 'support-bot'),
],
custom: { handoff: 'bot' },
),
))Let the bot answer only while it owns the conversation
Your backend gets each new message from the message.new webhook, including the messages the bot sends, so skip those first. The webhook carries the message but not the channel's custom data, so read handoff before the bot answers. AI Message Streaming shows how to post the answer as the bot.
# channel_type and channel_id come from the message.new webhook
channel = client.chat.get_channel(channel_type, channel_id, false).channel
if channel.custom.handoff == 'bot'
# Ask your LLM, then post its answer as the bot
endHand the conversation to a person
Decide in your backend when a person should take over. Common reasons:
- The customer asks for a person, by tapping a "Talk to a person" button that calls your backend, or in their own words.
- The bot can't solve the problem, for example after two answers that didn't help.
- The conversation is sensitive, such as a refund dispute or a cancellation, or the customer is on a plan with priority support.
To let the LLM decide, give it a tool such as hand_off_to_human and run this code when it calls the tool. It sets handoff to waiting, which stops the bot at the check above, and tells the customer what happens next:
client.chat.update_channel_partial('agent', 'support-anya-1', Models::UpdateChannelPartialRequest.new(
set: { handoff: 'waiting', handoff_reason: 'Customer asked for a person' },
))
client.chat.send_message('agent', 'support-anya-1', Models::SendMessageRequest.new(
message: Models::MessageRequest.new(text: "I'm connecting you with someone from our team.", user_id: 'support-bot'),
))The partial update changes only the fields you set. Every member's app gets a channel.updated event, so your UI can show that a person is on the way.
Show waiting conversations to your agents
Custom fields work in query filters, so one query lists the conversations waiting for a person, the longest wait first:
response = client.chat.query_channels(Models::QueryChannelsRequest.new(
filter_conditions: { type: 'agent', handoff: 'waiting' },
sort: [Models::SortParamRequest.new(field: 'updated_at', direction: 1)],
limit: 30,
))Keep anything else your inbox needs, such as handoff_reason or a priority, in the same custom data, and filter or sort on it the same way.
Let an agent take over
When an agent picks a conversation, add them as a member and remove the bot in one update. The update can also post a message, which members see as a system message. Then set handoff to human:
client.chat.update_channel('agent', 'support-anya-1', Models::UpdateChannelRequest.new(
add_members: [Models::ChannelMemberRequest.new(user_id: 'maria')],
remove_members: ['support-bot'],
message: Models::MessageRequest.new(text: 'Maria joined the conversation.', user_id: 'maria'),
))
client.chat.update_channel_partial('agent', 'support-anya-1', Models::UpdateChannelPartialRequest.new(
set: { handoff: 'human', assignee_id: 'maria', config_overrides: { typing_events: true } },
))- The agent sees the whole conversation. A member added later reads the full channel history, the bot's answers included, unless you hide it.
- Typing indicators turn on. The
agentchannel type has them off, since a bot doesn't type.config_overridesturns them on for this channel now that two people talk in it. - Only one agent takes it. If two agents can pick the same conversation at once, assign it in your own database first, or read
handoffand go ahead only while it is stillwaiting.
To give the conversation back to the bot, add the bot again and set handoff back to bot.
With the Stream Chat AI SDK
The Stream Chat AI SDK (@stream-io/chat-ai-sdk) runs the bot for you in Node.js: AgentManager.startAgent adds a bot user to the channel and answers its messages with the LLM you pick. Two additions give it the handoff:
- A server tool the LLM calls to hand off, which sets
handofftowaiting. - A
userMessageHandlerthat keeps the bot quiet oncehandoffisn'tbot. Returningtrueskips the LLM for that message.
import {
AgentManager,
AgentPlatform,
type AgentTool,
} from "@stream-io/chat-ai-sdk";
import { z } from "zod";
declare module "stream-chat" {
interface CustomChannelData {
handoff?: "bot" | "waiting" | "human";
handoff_reason?: string;
}
}
const handOffToHuman: AgentTool = {
name: "handOffToHuman",
description: "Hand the conversation to a person from the support team.",
instructions:
"Call this when the customer asks for a person, or when you can't solve their problem.",
parameters: z.object({
reason: z.string().describe("Why the conversation needs a person"),
}),
async execute(args, { channel }) {
const { reason } = args as { reason: string };
await channel.updatePartial({
set: { handoff: "waiting", handoff_reason: reason },
});
return "Tell the customer that someone from the team will join this conversation.";
},
};
const botIdFor = (channelId: string) => `support-bot-${channelId}`;
const manager = new AgentManager({
serverToolsFactory: () => [handOffToHuman],
agentIdResolver: botIdFor,
// The bot stays quiet once the conversation isn't its own.
userMessageHandler: ({ channel }) =>
(channel.data?.handoff ?? "bot") !== "bot",
});
await manager.startAgent({
userId: botIdFor("support-anya-1"),
channelId: "support-anya-1",
channelType: "agent",
platform: AgentPlatform.OPENAI,
instructions:
"You are the first line of support. Hand off to a person when needed.",
});When an agent takes the conversation, call manager.stopAgent(botIdFor("support-anya-1")) instead of removing the bot yourself: it disconnects the bot and removes it from the channel. Then add the agent and set handoff to human as shown above.
Going further
- The
agentchannel type: the defaults for conversations with an AI bot. - AI Message Streaming: stream the bot's answer into a message as the LLM writes it.
- Channel Members: add, remove and hide history for members.
- Webhook Events: every event your backend can receive.