Observability for Twilio Agent Connect with Opik
Twilio Agent Connect (TAC) is a framework for building AI agents that answer real phone calls. It handles the telephony, speech-to-text and text-to-speech, and calls your handler with the caller’s transcribed message.
This guide explains how to log your voice agent to Opik. You will trace every caller turn, group the turns of a call into a single conversation, and attach the call recording so you can listen to what the caller heard.
Account Setup
Comet provides a hosted version of the Opik platform, simply create an account and grab your API Key.
You can also run the Opik platform locally, see the installation guide for more information.
Getting Started
Installation
Install the opik and twilio-agent-connect packages:
Configuring Opik
Configure the Opik Python SDK for your deployment type. See the Python SDK Configuration guide for detailed instructions on:
- CLI configuration:
opik configure - Code configuration:
opik.configure() - Self-hosted vs Cloud vs Enterprise setup
- Configuration files and environment variables
Logging traces
Twilio Agent Connect calls your handler once for every caller turn. Add the @opik.track decorator to that handler and each turn becomes a trace.
Set thread_id to the conversation id so that every turn of the same phone call is grouped into a single Opik thread:
That is the whole tracing setup. One caller turn is one trace, and one phone call is one thread.
You can also log useful call details as metadata:
Adding spans to a turn
A voice agent usually does several things before it answers: it looks up the caller, searches a knowledge base, calls a few tools, then calls an LLM. Add @opik.track to each of those functions and they appear as nested spans under the turn’s trace, so you can see where the time went and what each step returned.
Use the type argument to tell Opik what kind of step it is. Opik shows llm and tool spans differently, and llm spans are where token usage and cost appear.
Adding detail to a span
Inside a tracked function, use update_current_span to record anything the arguments and return value do not already capture:
For the full set of options, including how to log spans without a decorator, see the Log traces guide.
Tracking LLM calls automatically
If you call an LLM provider directly, use the matching Opik integration instead of writing your own llm span. Token usage, cost and model parameters are then logged for you, and the spans still nest under the current turn:
See the OpenAI, Anthropic and Gemini integrations, or browse all integrations for the framework you use.
Adding guardrails
A phone call is a live, unattended channel, so it is a good place to check what the caller says and what the agent is about to say. Opik guardrails run in the same request, and each check appears on the turn’s trace with its own pass or fail result.
See the Guardrails overview for the available checks, and Custom guardrails if you need one trained on your own policy.
Logging call recordings
Your handler receives text, because Twilio performs the speech-to-text and text-to-speech. To listen to a conversation in Opik, record the call with Twilio and attach the audio to your thread. Opik plays audio attachments inline.
Step 1: Start the recording
Twilio Agent Connect lets you run code when an inbound call arrives. Use that hook to start a recording, and tell Twilio where to notify you when the audio is ready:
recording_channels="dual" keeps the caller and the agent on separate audio channels.
Step 2: Attach the recording
When the call ends, Twilio calls your webhook with a link to the audio. Download it and attach it to a trace in the same thread:
Twilio uses the call SID as the conversation id, so the recording lands in the same Opik thread as the turns of that call. Open the thread in Opik and you can read the transcript and play the audio side by side.
Call recording is subject to local laws on consent. Check the rules that apply where your callers are before you enable it.
What gets logged
With this setup, Opik records:
- Threads: one per phone call, containing every turn in order
- Traces: one per caller turn, with the caller’s message and the agent’s reply
- Spans: your retrieval, tools and LLM calls, including token usage and cost
- Guardrail results: the pass or fail result of each check you run on the turn
- Attachments: the call recording, playable in the Opik UI
Next steps
- Log traces — the full span and trace API
- Log media and attachments — attachment limits and supported types
- Guardrails — check inputs and outputs in the request path
- Online evaluation — score every call automatically once it is logged
- All integrations — the LLM provider and framework you use
Further improvements
If you have any questions or suggestions for improving the Twilio Agent Connect integration, please open an issue on our GitHub repository.