> ## Documentation Index
> Fetch the complete documentation index at: https://docs.poly.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Advanced handoffs

> Hand off from function code, pass context to your live agents, and choose between handoffs and transfer_call.

Most agents only need [handoff reasons and destinations](https://docs.poly.ai/voice-channel/handoffs.md), set up without code. Use this page when the handoff needs logic.

## Handing off from function code

[Functions](https://docs.poly.ai/tools/classes/conv-object.md) can send a caller to a destination by name:

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
conv.call_handoff(destination="Pharmacy")
```

Use a function instead of a reason when:

* the destination depends on the conversation, for example opening hours or which site the caller is calling about;
* something has to happen before the transfer, such as writing a ticket or setting SIP headers from the conversation;
* the conversation is a chat. Connect your live-chat platform in **Messaging**, then ask Wren to add the handoff to a function.

The `destination` must match the name of a destination on the [Handoffs](https://docs.poly.ai/voice-channel/handoffs.md) page.

### Handoff reason and utterance

The built-in **handoff** template and the [`conv.call_handoff()`](https://docs.poly.ai/tools/classes/conv-object) helper accept two optional, structured fields. When the agent hands off by itself through [handoff reasons](https://docs.poly.ai/voice-channel/handoffs.md#reasons), the reason's key is recorded as `reason`.

| Field | Purpose | Example |
| - | - | - |
| `reason` | Machine-readable code explaining *why* the call is being escalated (e.g. `policy_violation`, `needs_human`, `no_availability`). Surfaces in Conversation Review and the Conversations API. | `policy_violation` |
| `utterance` | A short message the agent delivers *before* transfer begins (spoken for voice, displayed for webchat). Logged alongside the handoff for QA review. | "Let me transfer you to a specialist who can help." |

> [!WARNING]
>
> When using SIP REFER, the utterance may not play before the transfer completes because the REFER fires at the same time as function execution. If you need the utterance to be spoken reliably before transfer, use SIP INVITE instead.

**Where it shows up**

* **Flows & KB actions** – Selecting **builtin-handoff** displays *Reason* and *Pre-handoff utterance* fields.
* **Functions** – Call [`conv.call_handoff(destination="...", reason="...", utterance="...")`](https://docs.poly.ai/tools/classes/conv-object) to escalate programmatically.
* **Conversation Review** – Both fields appear in the metadata panel for quick troubleshooting.
* **Conversations API** – Returned inside the `handoff` object for BI dashboards or CRM routing.

**Benefits**

* Removes guesswork when diagnosing handoffs – no more relying on LLM summaries alone.
* Enables fine-grained routing rules in telephony or CRM systems.
* Gives QA teams full visibility into the exact wording customers heard.

## Handoff context handover

When the agent transfers a caller to a human, the human-side system needs context about what already happened. PolyAI exposes that context through three complementary channels — pick whichever your contact-center platform supports.

| Channel | Best for | What you get |
| - | - | - |
| **SIP headers** | Lightweight metadata embedded in the transfer signal itself; supported by most SBCs and Twilio Flex. | Custom `X-` headers (e.g. `X-Customer-ID`, `X-Reason`, `X-Caller-ID`) populated from `conv.state` variables at transfer time. See [Configuring SIP headers](https://docs.poly.ai/voice-channel/handoffs.md#configuring-sip-headers). |
| **[Handoff API](https://docs.poly.ai/api-reference/handoff/introduction.md)** | Larger structured payloads that don't fit in headers, or when the destination needs a "screen pop" with full context. | Selected `conv.state` fields, retrievable by `id` or `shared_id`. Which fields populate the `data` object must be configured for your project before they're exposed via the API — this isn't self-serve yet, so contact your PolyAI representative to set it up. See [Before you start](https://docs.poly.ai/api-reference/handoff/introduction.md#before-you-start). |
| **[Conversations API](https://docs.poly.ai/api-reference/conversations/introduction.md)** | Post-handoff CRM enrichment, BI, and QA workflows. | Full conversation including the `handoff` object with `destination`, `reason`, `utterance`, plus the entire transcript, [`conv.log`](https://docs.poly.ai/tools/classes/conv-log) entries, and any [custom metrics](https://docs.poly.ai/tools/classes/conv-object.md#write_metric) the agent recorded. |

The data the agent writes (and the human therefore sees) is whatever you choose to store. Common fields:

> [!NOTE]
>
> For the Handoff API specifically, the fields below must be configured for your project before they're exposed via `GET /handoff_state` — contact your PolyAI representative to set this up. See [Before you start](https://docs.poly.ai/api-reference/handoff/introduction.md#before-you-start).

* **Identifiers** – `customer_id`, `account_number`, `shared_id` linking the PolyAI conversation to a record in your CRM
* **Verification status** – `successfully_identified`, `auth_method`, `verification_attempts` so the agent doesn't ask the caller to re-verify
* **Reason for escalation** – machine-readable `reason` (e.g. `policy_violation`, `complaint_escalation`, `customer_refund`); see [Handoff reason and utterance](#handoff-reason-and-utterance)
* **Pre-handoff utterance** – the exact wording the agent used before transferring, logged for QA
* **Caller intent and slots** – any entities collected during the AI portion of the call (order ID, refund amount, preferred callback time)

> [!TIP]
>
> Choose **one** channel as the source of truth and keep the others consistent with it. SIP headers must be set explicitly on each handoff destination or via `conv.call_handoff(sip_headers={...})`; the Handoff API draws on the `conv.state` fields configured for your project, so keep those two in sync if you use both.

### Writing context from a function

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
def transfer_to_billing(conv):
    return conv.call_handoff(
        destination="billing",
        reason="refund_request",
        utterance="Let me transfer you to a billing specialist who can help with that refund.",
        sip_headers={
            "X-Customer-ID": conv.state.customer_id,
            "X-Verified": "true",
            "X-Reason": "refund_request",
        },
    )
```

The `destination` parameter must match a handoff configured under **Integrations** > **Telephony** > **Handoffs**. The `reason` and `utterance` surface in [Conversation Review](https://docs.poly.ai/analytics/conversations/review.md) and the [Conversations API](https://docs.poly.ai/api-reference/conversations/introduction.md); the `sip_headers` are merged with any headers configured on the destination, with values passed here taking precedence.

### Webchat and SMS handoffs

Webchat and SMS use the same conceptual model but signal the handoff over the [Chat API](https://docs.poly.ai/api-reference/chat/introduction.md): the `chat/respond` response includes a `handoff` object with `destination` and `reason`, and `end_conversation` is set to `true`. Your widget or SMS connector then routes the session to your live-chat platform and retrieves the full context via the Handoff API. See [Webchat and SMS handoff via the Chat API](https://docs.poly.ai/call-data/conversations-api/handoff-states.md#webchat-and-sms-handoff-via-the-chat-api) for the integration steps.

### Feedback loops from the human side

Once a human has handled the escalation, you can close the loop in two ways:

1. **Tag the conversation in Conversation Review.** Live agents (or QA reviewers) can apply [annotations](https://docs.poly.ai/analytics/conversations/annotations.md) such as *Escalated unnecessarily* or *Wrong topic* directly on the PolyAI conversation. Tagged calls surface in [QA workflows](https://docs.poly.ai/learn/maintain/qa-analytics.md) and in the conversations Wren analyzes.
2. **Push outcomes back via the Conversations API.** If your CRM captures a resolution code on the human-handled portion, attach it back to the conversation by `shared_id`. This pairs the agent's `reason` field with the human's actual outcome, which is what [Wren](https://docs.poly.ai/wren/analyze.md) needs to answer questions like *"What share of complaint escalations were resolved on first contact?"*.

## Comparison: Call Handoff and the `transfer_call` function

These two methods serve similar purposes – routing the user to another endpoint – but are mutually exclusive and differ in flexibility and implementation.

In the table below,  means the feature is supported and **–** means it is not supported.

| Feature | `Call Handoff` (UI-based) | `transfer_call` (code-based) |
| - | - | - |
| Ease of setup |  UI form | – Requires Python editing |
| Works in flow builder | | |
| Works in Function Editor | – | |
| Dynamic routing logic | – |  Full control |
| Supports custom metrics | – | |
| Supports soft-handoff | – | |
| Best for static SIP integration | | – |
| Best for dynamic integrations | – |  (e.g. Zendesk) |

> [!WARNING]
>
> The two methods can't be combined in one step — `transfer_call` overrides any UI-configured Call Handoff. If you use it, keep destination mappings in sync manually; UI changes don't propagate to function-based transfers.

The UI-based Call Handoff does not support custom metrics. Use `transfer_call` with [`conv.write_metric()`](https://docs.poly.ai/tools/classes/conv-object) if you need handoff reason codes for analytics.

### When to use each method

Use `Call Handoff` if:

* You want a quick setup through Dialog Studio with minimal code.
* Your routing needs are straightforward and based on static values.

Use `transfer_call` if:

* You need to pass dynamic SIP headers (e.g., customer metadata).
* You want to use soft handoffs or log custom handoff metrics.
* You're integrating with a platform that does not support SIP REFER (e.g. Zendesk).

***

***

## Voicemail detection

PolyAI can detect voicemail in certain scenarios, but the behavior depends on the call direction and handoff method.

### Outbound calls

For outbound calls, PolyAI supports project-level voicemail detection. A detection flow at the start of the call classifies what the agent hears before the conversation begins. Common classifications include human, IVR, voicemail, and number not in service – though the exact categories depend on how the project is configured.

Based on the classification, the agent typically routes into the appropriate path. For example:

* **Human** – proceed to the greeting and main conversation flow
* **IVR** – enter an IVR traversal flow (DTMF navigation, hold loops)
* **Voicemail** – leave a message or hang up, depending on project requirements
* **Number not in service** – end the call

Detection uses barge-in on the first turn, since voicemail systems and IVRs do not wait for the agent to finish speaking. This is a project-level configuration – contact your PolyAI representative to set it up.

See [Outbound calling](https://docs.poly.ai/voice-channel/numbers/outbound-calling.md) for more on outbound call configuration.

### Inbound SIP handoffs

When PolyAI transfers an inbound call to an agent (via SIP REFER, SIP INVITE, or SIP BYE), voicemail detection on the *destination side* is **not available by default**. The behavior depends on the handoff method:

* **SIP REFER / SIP BYE** – PolyAI drops from the call after initiating the transfer, so it has no visibility into whether the destination answers or goes to voicemail.
* **SIP INVITE** – PolyAI bridges both call legs, which provides more visibility during the transfer. However, automated voicemail detection on the destination side is not a standard feature.

If you need voicemail detection as part of your inbound handoff workflow, contact your PolyAI representative to discuss what's possible for your specific setup.

***
