Skip to main content
This tutorial creates a real-time voice agent that responds to phone calls via Twilio. The implementation supports any LLM or Text-to-Speech (TTS) model, making it ideal for voice applications like customer support bots and receptionists. The example uses PipeCat to handle component integration, user interruptions, and audio data processing. You can find the final version of the code here

Cerebrium setup

Set up Cerebrium:
  1. Sign up here
  2. Follow installation docs here
  3. Create a starter project:
    This creates:
    • main.py: Entrypoint file
    • cerebrium.toml: Build and environment configuration
Add these pip packages to your cerebrium.toml:
Set up a FastAPI server to handle Twilio calls and upgrade to WebSocket connections for real-time communication. Add this code to main.py:
Create a templates folder with stream.xml inside. This XML response tells Twilio to upgrade to a WebSocket connection:
Replace the stream URL with the deployment’s base endpoint, using the correct project ID. Configure Cerebrium to run the FastAPI server by adding this to cerebrium.toml:
You can read more about run custom web servers here.

Twilio setup

Twilio provides cloud communications APIs for messaging, voice, video, and authentication. Other providers work as well. Sign up for a free account here. Purchase a local number (not toll-free) from the phone numbers page. Then set up a webhook to connect calls to your agent. Buy a number Setup Webhook Save the changes and proceed to set up the AI agent.

AI Agent Setup

Create bot.py to set up the AI agent using PipeCat for component integration, interruption handling, and audio processing:
The code:
  1. Connects to WebSocket transport for audio I/O
  2. Sets up services:
  3. Uses Secrets for authentication
  4. Creates a customizable PipelineTask supporting:
    • Image and Vision use cases (docs)
    • Built-in interruption handling
    • Easy model swapping
  5. Handles call events (join/leave) via webhooks
For lower latency (~500ms end-to-end), run parts or all of the pipeline locally. See the voice agents guide and RAG voice agent blog post.

Deploy to Cerebrium

Deploy to Cerebrium by running cerebrium deploy in the terminal. A successful deployment looks like this: Cerebrium Deployment Test by calling the Twilio number — the agent responds automatically.

Scaling Pipecat

For scaling PipeCat on Cerebrium:
  1. Use large CPU instances (10 CPUs, 8GB memory) for Twilio’s less than 1s response requirement
  2. Run concurrent PipeCat processes:
    • Each process uses ~0.5 CPUs
    • 10 CPU instance handles 20 concurrent calls
    • Adjust based on traffic needs
For scaling criteria, use Cerebrium’s replica_concurrency setting to spawn new containers based on utilization, preventing cold starts for subsequent calls. Update cerebrium.toml with the following:

Conclusion

This tutorial provides a foundation for implementing voice features and expanding into image and vision capabilities. PipeCat is an extensible, open-source framework for building voice-enabled apps. Cerebrium handles deployment and autoscaling with pay-as-you-go compute. Tag @cerebriumai to showcase work and join the Discord community for questions and feedback.