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

# Put an Agent on Your Number

> Move a number you use with the Telephony APIs to an Unpod agent that answers and places calls on it

The Telephony APIs give you a number your app controls. This page puts an Unpod agent on
that number instead: the agent answers inbound calls and places outbound ones, and you
deploy nothing. In the [call chain](https://docs.unpod.ai/home/how-it-fits.md#the-call-chain) this is stage 2,
Connect: attaching a number to an agent.

> [!NOTE]
>
> No number yet? The Agent SDK [Quickstart](https://docs.unpod.ai/agent-sdk/agent-quickstart.md) starts from a free
> number and covers the same steps.

## One number, one mode

A number is answered one way at a time. You pick the mode when you attach it.

| Mode | Who handles the calls | Attached with |
| - | - | - |
| Your app | Your code, through the Telephony APIs | `telephony.numbers.attach(number, attach_type="human_call")` |
| An agent | An Unpod agent | `agents.numbers.attach(agent_id, number)` |
| A SIP trunk | Your carrier or voice platform | [SIP Trunking](https://docs.unpod.ai/telephony/quickstart.md) |

If you followed the [Telephony Quick Start](https://docs.unpod.ai/telephony/apis/quickstart.md), your number is in
the first mode. Moving it to an agent changes three things:

* Inbound calls go to the agent.
* The number's inbound config (its answer URL and callbacks) is cleared. You can
  [set it again](#let-your-app-decide-per-call).
* Agent calls use the number as their caller ID.

## Before you start

* A number attached for the Telephony APIs. The
  [Quick Start](https://docs.unpod.ai/telephony/apis/quickstart.md#make-your-first-call) leaves you with one.
* `UNPOD_BASE_URL`, `UNPOD_PLATFORM_TOKEN` and `UNPOD_ORG_HANDLE` exported, as in the
  [Introduction](https://docs.unpod.ai/telephony/introduction.md#credentials).
* Python 3.12+ and `pip install unpod`.
* Room in your plan for the number. The attach checks your plan's number limit after the
  detach has already gone through, so an org at or over its limit loses the number to the
  [`402` below](#move-the-number-to-the-agent).

## Create the agent

An agent is a brain plus a voice, under an `agent_id` you choose. `Prompt` is a brain that
Unpod runs, so there is no process of yours to deploy.

```python theme={null}
import asyncio
from unpod import AsyncClient, Prompt

async def main() -> None:
    async with AsyncClient() as client:
        voice = await client.agents.voice.create(
            agent_id="my-support",   # you choose this; every later step takes it
            name="My Support Line",  # label shown in dashboards
            brain=Prompt(
                "You are Riya, a support agent. Greet the caller, ask what they "
                "need, and keep every reply under two sentences."
            ),
            voice_profile="Riya",
            recording=True,
        )
        print(voice.agent_id, voice.voice_profile_name)

asyncio.run(main())
```

`voice_profile` takes a name from `client.voice_profiles.list()`. The Agent SDK
[Quickstart](https://docs.unpod.ai/agent-sdk/agent-quickstart.md) explains voice profiles and the `Playbook` brain.

## Move the number to the agent

A number in your-app mode can't be reassigned in place, so detach it, then attach it to
the agent.

> [!WARNING]
>
> Detach removes the number's [inbound config](https://docs.unpod.ai/telephony/apis/incoming-calls.md): its answer
> URL and callbacks. A number your org owns stays yours, but a number you took from the
> shared pool goes back to the pool, where another org can take it. Run both calls
> together, as below.

```python theme={null}
import asyncio
import httpx
from unpod import AsyncClient

NUMBER = "+919000000001"   # the number your app uses today
AGENT_ID = "my-support"

async def main() -> None:
    async with AsyncClient() as client:
        try:
            released = await client.telephony.numbers.detach(NUMBER)
        except httpx.HTTPStatusError as e:  # nothing detached: the body says why
            raise SystemExit(e.response.text)
        if not released.numbers:  # 206: nothing detached
            raise SystemExit("Nothing detached: check UNPOD_ORG_HANDLE")
        failed = [r.error for r in released.numbers if not r.ok]
        if failed:  # stop here: do not attach a number that is still in use
            raise SystemExit(f"Not detached: {failed}")

        result = await client.agents.numbers.attach(AGENT_ID, NUMBER)
        print(result.get("number"), result.get("agent_id"), result.get("status"))

asyncio.run(main())
```

The attach prints `status=assigned`, and the script stops before attaching if the detach
did not go through. Check the `agent_id` spelling before you run it: the attach does not
verify that the agent exists, and a number attached to a missing agent goes unanswered.
To confirm the routing, read the number's row from `client.telephony.overview()`: see
[Check the Lifecycle](https://docs.unpod.ai/platform/dev-platform/attach-number-to-agent.md#check-the-lifecycle).

| You see | Why | What to do |
| - | - | - |
| `400` `No valid unassigned numbers found.` | You called `agents.numbers.attach` while the number is still attached for your app | Detach first, as above |
| `409` `This number terminates on a SIP trunk; detach it first.` (`code: unsupported_termination`) | You tried to reassign the number in place, with `numbers.agent.change` or `POST …/numbers/reassign/`. A number in your-app mode gets this message too | Detach, then attach, as above |
| `400` `No numbers could be detached.` | The number isn't attached to your org | Check the number and `UNPOD_ORG_HANDLE` |
| `402` `number_entitlement_exceeded` | The detach went through, but your org is at its plan's number limit, so the attach was refused | Free a number or raise the plan limit, then attach again. A pool number may be gone by then |

## Call the number

Call the number from any phone. The agent answers with the voice and prompt you gave it.
You host nothing: no answer URL, no WebSocket server, no Runner.

If nothing answers, check the number's `connection_state`: see
[Number states](https://docs.unpod.ai/telephony/apis/numbers.md#number-states).

## Place a call with the agent

```python theme={null}
import asyncio
from unpod import AsyncClient

async def main() -> None:
    async with AsyncClient() as client:
        call = await client.calls.create(
            agent_id="my-support",
            to_number="+919000000002",   # the phone to ring
            data={"customer_name": "Asha", "order_id": "ORD-10482"},
        )
        print(call.call_id, call.status, call.from_number)

asyncio.run(main())
```

* You pass no `from_number`. Caller ID is the number attached to the agent. An agent with
  several numbers takes turns between them, least recently used first.
* `data` is per-call context the brain can read.
* The call is queued: `status` starts as `pending`. See
  [Outbound Calls](https://docs.unpod.ai/agent-sdk/outbound-calls.md) for status, callbacks and transcripts.

## Let your app decide per call

A number on an agent can still have an answer URL. When it does, Unpod asks your answer
URL on each inbound call, and the verbs you reply with run instead of the agent. Reply
with an empty body or `<Response/>` and the agent answers.

Detach cleared the number's inbound config, so set the answer URL again: see
[Point your number at your app](https://docs.unpod.ai/telephony/apis/incoming-calls.md#point-your-number-at-your-app).
If your server times out or returns an error, the call goes to the agent by default: see
[When your server is down](https://docs.unpod.ai/telephony/apis/incoming-calls.md#when-your-server-is-down).

## Which attach call

| Call | Use it for |
| - | - |
| `client.agents.numbers.attach(agent_id, number)` | An agent made with `agents.voice.create`, as on this page |
| `client.agents.update(agent_id, number=…)` | The same, and moving a number between your agents with no detach |
| `client.numbers.attach(number_id, agent_id)` | The first row, keyed by number id instead of the E.164 number |
| `client.telephony.numbers.attach(numbers, attach_type="human_call")` | Your app, with the Telephony APIs |
| `client.telephony.numbers.attach(numbers, agent_id=…)` | An agent built on unpod.ai, by its handle |

[Attach a Number to an Agent](https://docs.unpod.ai/platform/dev-platform/attach-number-to-agent.md) has the result
fields and every error for the `telephony.numbers` calls.

## Two `calls.create`

| Call | What it places |
| - | - |
| `client.telephony.calls.create(from_, to, …)` | A call your app controls: legs, answer URL, `add_participant` |
| `client.calls.create(agent_id=…, to_number=…)` | A call the agent holds |

The Telephony APIs are documented for numbers in your-app mode. For calls your app
controls, keep a number in that mode, or [move this one back](#move-the-number-back).

## Move the number back

Release the number from the agent, then attach it for your app:

```python theme={null}
await client.telephony.numbers.detach(NUMBER)
await client.telephony.numbers.attach(NUMBER, attach_type="human_call")
```

The same warnings apply: a pool number is free for another org to take between the two
calls, and the attach can return the `402` above. Once the attach returns, the number
works as `from` in `client.telephony.calls.create`.

## Next

> [card] [Run Your Own Brain](https://docs.unpod.ai/agent-sdk/quickstart.md)
>
> Swap `Prompt` for `Runner()` and answer turns from your own process.

> [card] [Brain options](https://docs.unpod.ai/speech-stack/agents.md)
>
> Prompt, Playbook, Runner and Endpoint, and what each one deploys.

> [card] [Outbound Calls](https://docs.unpod.ai/agent-sdk/outbound-calls.md)
>
> Campaigns, callbacks and reading results.

> [card] [Attach a Number to an Agent](https://docs.unpod.ai/platform/dev-platform/attach-number-to-agent.md)
>
> The full attach, detach and reassign reference.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.