# Take a LiveKit voice agent to production

Answer a LiveKit phone number with your Amiril-checked agent, move it to the Production environment and reach its tools through the Amiril gateway.

This guide picks up where [Add Amiril to a LiveKit voice agent](/sdk/livekit) ends, with the payments line blocking bad calls in Development. You give it a phone number, move it to the Production environment and put its tools behind the Amiril gateway.

## Answer a phone number [#answer-a-phone-number]

Rent a phone number in LiveKit Cloud. A LiveKit SIP dispatch rule then sends each call to that number into a room of its own. It dispatches your agent there by the name in `rtc_session(agent_name=...)`. This script creates the rule with the LiveKit server API.

```py title="dispatch.py"
import asyncio
import os

from livekit import api
from livekit.protocol import agent_dispatch, room, sip

def payments_line_rule(number: str) -> sip.SIPDispatchRuleInfo:
    return sip.SIPDispatchRuleInfo(
        name="payments-line",
        numbers=[number],
        rule=sip.SIPDispatchRule(dispatch_rule_individual=sip.SIPDispatchRuleIndividual(room_prefix="call-")),
        room_config=room.RoomConfiguration(agents=[agent_dispatch.RoomAgentDispatch(agent_name="payments-line")]),
    )

async def main() -> None:
    async with api.LiveKitAPI() as livekit:
        request = sip.CreateSIPDispatchRuleRequest(dispatch_rule=payments_line_rule(os.environ["PHONE_NUMBER"]))
        created = await livekit.sip.create_dispatch_rule(request)
        print(f"Created dispatch rule {created.sip_dispatch_rule_id}")

if __name__ == "__main__":
    asyncio.run(main())
```

Run it once with `PHONE_NUMBER` set to your number. It reads `LIVEKIT_URL`, `LIVEKIT_API_KEY` and `LIVEKIT_API_SECRET` for your project. The caller's number reaches your entrypoint as the participant attribute `sip.phoneNumber`.

Call the number. The call shows on **Traces** under `payments_agent`, the same as a test call, because Amiril names the run after your `Agent` and not after the dispatch name.

## Move to production [#move-to-production]

Each environment keeps its own agents, keys and rule modes. Agents with the same name in Development and Production are two agents in Amiril.

1. Switch to **Production** with the environment menu and create a key on the **Data** page, as you did for Development.
2. Put that key's values in the deployed worker's environment. Start the worker with `python agent.py start`.
3. After the first production call, register `payments_agent` on the **Agents** page in Production too. The gateway grants tools only to a registered agent.
4. On each rule's page, switch **Mode in Production** to **Enforce** once its Shadow results there look right.

Keep a separate LiveKit project for development, with its own number. Workers that register the same dispatch name in one project share its calls, so a test worker could answer a real caller.

## Reach tools through the Amiril gateway [#reach-tools-through-the-amiril-gateway]

The Amiril gateway is an MCP server between your agent and the servers its tools live on. Each agent sees only the tools you grant it, and it never has a tool server's key. The SDK still checks each call's arguments in your process before the call leaves it.

LiveKit's MCP client comes with the `mcp` extra:

```bash
pip install "livekit-agents[mcp]"
```

1. Add the server that has `issue_payment` as a [tool server](/tool-servers). Here its slug is `payments`.
2. [Grant](/agents/access) `payments.issue_payment` to the registered agent on its **Access** tab.
3. Create a token on the agent's **Connect** tab. Copy the token and the gateway URL from its **Client configuration** into `AMIRIL_GATEWAY_TOKEN` and `AMIRIL_GATEWAY_URL`.

Then list the agent's tools from the gateway and wrap each one with the SDK:

```py title="gateway.py"
import os
from typing import Any

from amiril import plain
from livekit.agents import function_tool
from livekit.agents.llm import RawFunctionTool
from livekit.agents.llm.mcp import MCPServerHTTP

def checked(sdk_agent: plain.Agent, tool: RawFunctionTool) -> RawFunctionTool:
    """The same gateway tool, with the SDK's check in front of it."""
    schema = tool.info.raw_schema

    @sdk_agent.tool(name=schema["name"], description=schema.get("description"), schema=schema["parameters"])
    async def call(**arguments: Any) -> Any:
        return await tool(arguments)

    async def run(raw_arguments: dict[str, Any]) -> Any:
        return await call(**raw_arguments)

    return function_tool(run, raw_schema=schema)

async def gateway_tools(sdk_agent: plain.Agent) -> tuple[MCPServerHTTP, list[RawFunctionTool]]:
    gateway = MCPServerHTTP(
        url=os.environ["AMIRIL_GATEWAY_URL"],
        headers={"Authorization": f"Bearer {os.environ['AMIRIL_GATEWAY_TOKEN']}"},
    )
    await gateway.initialize()
    return gateway, [checked(sdk_agent, tool) for tool in await gateway.list_tools()]
```

In the entrypoint, call `gateway, tools = await gateway_tools(sdk_agent)` and pass `tools` to your `Agent`. Close the connection when the job ends with `ctx.add_shutdown_callback(gateway.aclose)`.

The gateway names each tool after its server's slug and the tool, joined by `__`. Your rules must name the tool the same way, so change `tool == "issue_payment"` to `tool == "payments__issue_payment"`. A call to a tool you revoked never reaches the tool server. The agent's **Calls** tab lists every gateway call and whether the gateway let it through.

## Next steps

- [Add Amiril to a LiveKit voice agent](https://docs.amiril.ai/sdk/livekit.md): Send each LiveKit call to Amiril as a run and check every tool call against your rules in Python. A blocked call never reaches your tool.
- [Grant tools to an agent](https://docs.amiril.ai/agents/access.md): Turn on the tools an agent may call, give it a gateway token and point its MCP client at the Amiril gateway. Then revoke the tools it stops using.
- [Set up environments and API keys](https://docs.amiril.ai/traces/environments-and-keys.md): Create an environment such as Staging and an API key that sends to it, so test runs stay out of production. Then put the key where each service runs.
- [Add a tool server](https://docs.amiril.ai/tool-servers.md): Register the MCP server where your agents' tools live and choose how Amiril signs in to it. Then grant its tools to agents on their Access tab.

## More detail

- [Setting up Amiril on a LiveKit Agents voice agent](https://docs.amiril.ai/assistant/livekit.md): LiveKit Agents (Python) with Amiril end to end: trace export, rules on function tools through amiril.plain, naming, turns, phone dispatch, environments, gateway tools, pitfalls.
- [Tool servers, the gateway and agent access](https://docs.amiril.ai/assistant/tool-servers.md): Everything about registering MCP tool servers, the four sign-in methods, the Amiril gateway, tool grants, gateway tokens, sync, the access log, access suggestions and their errors.
- [SDK behaviour in detail](https://docs.amiril.ai/assistant/sdk.md): The Amiril SDK past the setup guides: per-framework gaps, run ids and sessions, hook order and its errors, restarts, shutdown, offline behaviour, model messages, tested versions.
