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

# Plivo SMS and voice tools for LangChain

> Give a LangChain agent Plivo tools to send SMS and place voice calls.

[LangChain](https://docs.langchain.com) is an open-source framework for building AI agents. The `langchain-plivo-tools` package gives a LangChain agent two Plivo tools: one sends an SMS and one places a voice call. The agent's model decides when to use each tool.

This is a tool-use integration over Plivo REST APIs.

## Prerequisites

| Requirement | Description |
| - | - |
| Plivo account | Your Auth ID and Auth Token from the [Plivo console](https://cx.plivo.com) |
| Plivo number | A Plivo number in E.164 format, enabled for SMS, voice, or both |
| Python | 3.10 or newer |
| Model provider | An API key for the chat model the agent runs on. The examples use Anthropic |
| US SMS registration | To send SMS to US numbers, register the sending number: 10DLC for local numbers or toll-free verification for toll-free numbers. Not needed for other countries. |

## Quick start: send an SMS from an agent

Create a project and add the packages:

```bash theme={null}
uv init plivo-agent
cd plivo-agent
uv add langchain langchain-anthropic langchain-plivo-tools
```

Set your credentials:

```bash theme={null}
export ANTHROPIC_API_KEY="your-anthropic-key"
export PLIVO_AUTH_ID="your-auth-id"
export PLIVO_AUTH_TOKEN="your-auth-token"
export PLIVO_FROM_NUMBER="+14155550100"
```

Create `agent.py` and give the agent the Plivo tools:

```python theme={null}
from urllib.parse import urlencode

from langchain.agents import create_agent
from langchain_plivo_tools import PlivoToolkit

# Plivo-hosted answer URL: calls play the XML passed in `xml`.
xml = "<Speak>This is a call from your agent.</Speak>"
answer_url = "https://www.plivo.com/api/answer-url?" + urlencode({"xml": xml})

agent = create_agent(
    model="anthropic:claude-sonnet-5-5",
    tools=PlivoToolkit(answer_url=answer_url).get_tools(),
)

result = agent.invoke(
    {"messages": [{"role": "user", "content": "Text +14155550101 to say the deploy is done."}]}
)
print(result["messages"][-1].content)
```

Run it:

```bash theme={null}
uv run agent.py
```

The agent calls `plivo_send_message`, which sends the SMS and returns the Plivo message UUID. The message also shows in the [Plivo console logs](https://cx.plivo.com/logs?tab=messages). If it fails, look up the error in [Messaging error codes](https://plivo.com/docs/messaging/troubleshooting/error-codes.md).

## Available tools

| Tool | What it does |
| - | - |
| `plivo_send_message` | Sends an SMS from `PLIVO_FROM_NUMBER`. Takes `body` and `to`, and returns the message UUID |
| `plivo_make_call` | Places a call from `PLIVO_FROM_NUMBER` that plays the XML at `answer_url`. Takes `to`, and returns the request UUID |

Both tools also accept `auth_id`, `auth_token`, and `from_number` as arguments in place of the environment variables.

> [!WARNING]
>
> Both tools send billable messages and calls to numbers the model picks. The package has no allowlist or rate limit, so consider [human-in-the-loop review](https://docs.langchain.com/oss/python/langchain/human-in-the-loop).

## Send an SMS with `PlivoSendMessage`

Use the SMS tool on its own, without an agent:

```python theme={null}
from langchain_plivo_tools import PlivoSendMessage

tool = PlivoSendMessage()
tool.invoke({"body": "Your order has shipped.", "to": "+14155550101"})
```

## Place a call with `PlivoMakeCall`

When the call connects, Plivo fetches the Answer URL and runs the [Plivo XML](https://plivo.com/docs/voice/xml/overview.md) it returns. Pass the XML to the Plivo-hosted Answer URL in its `xml` query parameter, so you don't host anything:

```python theme={null}
from urllib.parse import urlencode

from langchain_plivo_tools import PlivoMakeCall

xml = "<Speak>Your order has shipped.</Speak>"
tool = PlivoMakeCall(answer_url="https://www.plivo.com/api/answer-url?" + urlencode({"xml": xml}))
tool.invoke({"to": "+14155550101"})
```

The tool returns the call's request UUID, and the phone rings and plays the message when answered. If the call fails, look up its [hangup cause](https://plivo.com/docs/voice/troubleshooting/hangup-causes.md). The Answer URL wraps the XML in `<Response>`. To use your own endpoint instead, pass its URL as `answer_url`. The call tool adds its `message` argument to it as a `message` query parameter, which the Plivo-hosted Answer URL ignores.

## Troubleshooting

| Issue | Solution |
| - | - |
| `Missing Plivo credentials` | Set `PLIVO_AUTH_ID`, `PLIVO_AUTH_TOKEN`, and `PLIVO_FROM_NUMBER`, or pass them to the tool. |
| `Plivo failed to send the message` | The text after it is the error from Plivo. Confirm the Auth ID and Auth Token, and that the sender number can send SMS to the destination country. See [Messaging error codes](https://plivo.com/docs/messaging/troubleshooting/error-codes.md). |
| Message shows as sent but never arrives (US) | Check that the sending number is registered for 10DLC or toll-free verification. |
| The agent has no call tool | Pass `answer_url` to `PlivoToolkit` as in the [Quick start](#quick-start-send-an-sms-from-an-agent). Without it, the toolkit leaves out `PlivoMakeCall` and warns `Some Plivo tools are missing from the toolkit`. |
| The call fails or ends early | Check the call's [hangup cause](https://plivo.com/docs/voice/troubleshooting/hangup-causes.md) in the [Plivo console logs](https://cx.plivo.com/logs?tab=voice). |

## Related

> [card] [langchain-plivo-tools](https://github.com/plivo-dev/langchain-plivo-tools)
>
> Source and release notes for the package.

> [card] [LangChain agents](https://docs.langchain.com/oss/python/langchain/agents)
>
> How to build and run an agent with tools.

> [card] [Plivo Messages API](https://plivo.com/docs/messaging/api/messages.md)
>
> Send an SMS with the Plivo API.

> [card] [Plivo Voice API](https://plivo.com/docs/voice/api/calls.md)
>
> Make a call with the Plivo API.
