Escalate to a human agent
This guide shows how to transfer a conversation from your AI agent to a human agent using the TAC SDK. You'll configure a Studio Flow for routing, create the built-in handoff tool, and register it with your LLM.
Before you begin, you need:
- A working TAC setup with a Voice or SMS channel configured (see Add TAC to your agent)
- A Twilio Studio Flow that routes to your target system (for example, Twilio Flex)
- The Studio Flow SID (
FW...) for that flow
The handoff flow differs by channel type:
Voice: Your agent's LLM calls the handoff tool. The tool builds a HandoffPayload with conversation context and stores it on the session. After the LLM's final response plays to the caller, the Voice channel sends a WebSocket end message with the payload. Twilio routes the call to your Studio Flow via the <Connect action> URL.
Digital (SMS, chat): Your agent's LLM calls the handoff tool. The tool POSTs the HandoffPayload directly to the Studio Flow Executions API, which starts a new flow execution for the customer.
In both cases, the HandoffPayload includes the conversation ID, memory store ID, profile ID, and any custom attributes you define. Your Studio Flow can reference these values to route the conversation.
Set the TWILIO_STUDIO_HANDOFF_FLOW_SID environment variable in your .env file:
TWILIO_STUDIO_HANDOFF_FLOW_SID=FWxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
The SDK uses this SID to derive both the Studio Executions URL (for digital handoff) and the <Connect action> webhook URL (for voice handoff).
Info
If your Conversation Configuration includes Voice captureRules, remove them before using the handoff tool. When TAC uses Conversation Relay, it hydrates voice conversations automatically. Having Voice captureRules active at the same time causes duplicate messages in the conversation thread after handoff.
The create_studio_handoff_tool function returns a tool your LLM can call to trigger a handoff. It takes the TAC instance, the current conversation session, and optional routing attributes.
from tac.tools import create_studio_handoff_tool
In your message handler, create the tool and pass it to your LLM:
1HANDOFF_ATTRIBUTES = {2"department": "support",3"priority": "normal",4}56async def handle_message_ready(7message: str,8context: ConversationSession,9memory: TACMemoryResponse | None,10) -> str:11handoff_tool = create_studio_handoff_tool(12tac, context, attributes=HANDOFF_ATTRIBUTES13)1415# Add to your LLM's tools alongside other tools16tools = [handoff_tool, knowledge_tool]1718# Your LLM can now call the handoff tool when needed1920tac.on_message_ready(handle_message_ready)
The attributes dictionary is optional. Use it to pass routing metadata your downstream system expects. For Flex, these surface as TaskRouter task attributes.
When the LLM calls this tool, it accepts a reason parameter (a string explaining why the conversation needs a human). The tool builds a HandoffPayload and delivers it to your Studio Flow.
Configure your agent's system prompt to recognize common escalation scenarios:
1You are a helpful customer service agent. Escalate to a human agent when:2- The customer explicitly asks to speak with a human3- You cannot resolve the customer's issue after two attempts4- The conversation involves billing disputes or account security5- The customer expresses frustration or dissatisfaction
Your LLM uses this guidance along with the handoff tool to decide when to hand off.
- Start your TAC server with the handoff tool configured and
TWILIO_STUDIO_HANDOFF_FLOW_SIDset. - Call your Twilio phone number (or send an SMS) and ask to speak with a human agent.
- Verify that your agent calls the handoff tool.
- For voice: confirm the caller hears the LLM's final response, then the call routes to your Studio Flow.
- For digital: confirm the Studio Flow execution starts with the
HandoffPayloadin the flow data. - Check that the
HandoffPayloadincludes your custom attributes, conversation ID, and profile ID.
- AI to human handoff: Set up the full handoff flow from TAC to Flex through Studio.
- Add TAC to your agent: Add custom tools and knowledge search alongside handoff.
- Channels: Learn more about channel configuration and routing.