Use Ollama from your phone with Pacerelle

Read guideView documentation

Your model already answers on your computer. You want to ask it a question from your phone, including away from home Wi-Fi. Pacerelle can connect the two: a Python agent receives your message, queries Ollama on the computer and replies to the original conversation.

What this first agent does

The example handles text questions without conversation memory or access to your files. Start with this narrow task so that you can verify the connection before adding tools. Your computer must remain awake, online and able to run the chosen model.

  • Install Ollama and confirm that ollama run MODEL_NAME answers locally.
  • Use Python 3.12 for the current Pacerelle alpha wheels. Install the SDK and HTTP client below.
  • Create an agent in Pacerelle and set PACERELLE_AGENT_ID and PACERELLE_AGENT_TOKEN in your terminal environment. Never commit the token.
  • Set OLLAMA_MODEL to an exact name from ollama list. The quickstart covers creating and connecting the agent.
bash
python -m pip install --pre pacerelle httpx

Connect Ollama to the conversation

Save this as ollama_phone.py. Choosing a local model keeps inference on your computer; choosing a cloud tag sends inference to its provider. Keep the loopback endpoint private rather than opening its port on your router.

python
import asyncio
import os
import httpx
from pacerelle import AgentGatewayClient

client = AgentGatewayClient(
    token=os.environ["PACERELLE_AGENT_TOKEN"],
    agent_id=os.environ["PACERELLE_AGENT_ID"],
    base_url="https://api.pacerelle.com",
    e2ee=True,
    store_root=".pacerelle-ollama",
)
model = os.environ["OLLAMA_MODEL"]

async def handle(message, agent):
    if message.widget_response or not message.text:
        return
    if len(message.text) > 8000:
        await agent.reply(message, "Please send fewer than 8,000 characters.")
        return
    try:
        async with httpx.AsyncClient(timeout=180) as http:
            response = await http.post(
                "http://127.0.0.1:11434/api/generate",
                json={"model": model, "prompt": message.text, "stream": False},
            )
            response.raise_for_status()
            answer = response.json()["response"].strip()
    except (httpx.HTTPError, ValueError, KeyError):
        await agent.reply(message, "Ollama did not return a usable answer. Check the local service and model.")
        return
    await agent.reply(message, answer or "The model returned an empty answer.")

client.on_message(handle)
asyncio.run(client.connect())

Run python ollama_phone.py, then ask the agent to explain RAM versus storage in three sentences. The Ollama API returns a complete response when stream is false. agent.reply routes the answer back to the source conversation.

Test the complete mobile journey

Get one reply while your phone is on Wi-Fi, then disable Wi-Fi and send a new question over mobile data. A connected badge alone is insufficient: check that this agent returns an answer to the new question. Stop Ollama and confirm the explicit failure response, then restart it and try again.

If no reply arrives, check the Python process, agent credentials, local service and model name in that order. A slow first response can include loading the model. Test a shorter prompt before increasing the timeout; larger limits cannot fix a model that does not fit in memory.

Make the setup durable

Retain the same agent ID and state directory across restarts, with only one process using that directory. The Python store contains private keys and pending plaintext, so protect its permissions and backups. For daily operation, supervise the process and reconnect after failures as described in the production guide.

This example does not implement persistent jobs or conversation memory. For questions about your own documents, continue with the local RAG guide. For ordinary chat, measure answer quality and latency on your own questions before choosing a larger model.

Connect your first agent

Create the agent, copy its configuration and verify a reply before leaving your computer.

Follow the quickstart

Recommended reading