🎤Micdrop

Agent Handoff in a Voice Call

A call can move from one agent to another: a receptionist that routes the caller, then a billing agent with its own prompt, tools and model, possibly from another provider. The next agent receives the conversation so far and answers right away, so the caller never repeats themselves.

Handing over from a tool

handoffTool() gives the model a tool named transfer_to_<name>:

import { endCallTool, handoffTool, MicdropServer } from '@micdrop/server'
import { MistralAgent } from '@micdrop/mistral'
import { OpenaiAgent } from '@micdrop/openai'
const billing = new MistralAgent({
apiKey: process.env.MISTRAL_API_KEY || '',
systemPrompt: 'You answer questions about invoices and payments.',
tools: [endCallTool(), handoffTool({ name: 'reception', to: () => reception })],
})
const reception = new OpenaiAgent({
apiKey: process.env.OPENAI_API_KEY || '',
systemPrompt: 'You greet the caller and find out what they need.',
tools: [
endCallTool(),
handoffTool({
name: 'billing',
to: billing,
description: 'Transfer the call for any question about an invoice',
}),
],
})
new MicdropServer(socket, { stt, tts, agent: reception })

to takes the agent, or a function returning it. A function lets two agents hand over to each other, as above, and can create a fresh agent at each handoff.

What happens during a handoff

  1. The model calls the tool, possibly after saying a few words such as “Let me transfer you”.
  2. The agent finishes its answer and says nothing more.
  3. The next agent takes the conversation behind its own system prompt: every message, tool call and tool result, the one of the handoff included.
  4. It answers the turn, and the next turns go to it.

server.agent returns the agent answering now. The conversation of the call is the one of that agent, and its events (Message, EndCall, ToolCall…) reach the client as before.

If the user speaks while the first agent is answering, the handoff still happens, and the next agent waits for the end of the turn.

Options

OptionTypeDescription
namestringNames the agent, and the tool transfer_to_<name>
toAgent | (() => Agent)The agent taking over
descriptionstringTells the model when to hand over, “Transfer the call to name” by default
history(conversation) => conversationWhat the next agent reads of the conversation, its system prompt aside, everything by default
answerbooleanWhether the next agent answers right away, true by default

history keeps the next agent focused when the conversation grows long:

handoffTool({
name: 'billing',
to: billing,
// The last ten items only
history: (conversation) => conversation.slice(-10),
})

Handing over from code

handoff() does the same from your application, with the same options:

server.agent.handoff(billing)

Inside a custom tool, call it on the agent argument, the agent running the tool:

agent.addTool({
name: 'verify_identity',
description: 'Check the date of birth the user gives',
inputSchema: z.object({ dateOfBirth: z.string() }),
execute: async ({ dateOfBirth }, agent) => {
if (!(await checkIdentity(dateOfBirth))) return { verified: false }
agent.handoff(accountAgent)
return { verified: true }
},
})

Called during an answer, the handoff waits for the answer to be over.

Realtime models

A realtime model holds its own session and voice, so it cannot hand the call over yet. Its handoff() throws, and the model reads the error as the result of the tool.