Send traces from LiveKit Agents
Give LiveKit Agents a tracer provider and send each voice session to Amiril over OpenTelemetry as one run, with every turn and the tool calls made in it.
LiveKit Agents traces each session with OpenTelemetry once you hand it a tracer provider. Each session then reaches Amiril as one run, split into its turns.
Install
pip install opentelemetry-sdk opentelemetry-exporter-otlp-proto-httpTurn on trace export
Save this next to your agent. It gives LiveKit a tracer provider and sends each job's spans as soon as the job ends.
from livekit.agents.telemetry import set_tracer_provider
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import ReadableSpan, SpanProcessor, TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
class FlushWhenJobEnds(SpanProcessor):
"""Sends a job's spans when its root span ends, the last span the job records."""
def __init__(self, provider: TracerProvider) -> None:
self.provider = provider
def on_end(self, span: ReadableSpan) -> None:
if span.name == "job_entrypoint":
self.provider.force_flush()
def start_tracing() -> TracerProvider:
provider = TracerProvider()
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
provider.add_span_processor(FlushWhenJobEnds(provider))
set_tracer_provider(provider)
return providerCall start_tracing() from the setup_fnc you pass to AgentServer. It runs in each job process before the job starts. LiveKit opens the job's root span as the job starts, so the tracer provider must be set by then.
Set the endpoint and key
In Amiril, create an API key and copy the endpoint:
- Switch to the environment your runs belong to, with the menu at the top of the page.
- Open Data and select Set up a new installation. It opens a panel that connects an agent to Amiril.
- In the panel, enter a Key name under Create a new key and select Create key.
- Copy the key and the OpenTelemetry traces endpoint.
Then set these variables where your agent runs:
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="<OpenTelemetry traces endpoint>"
export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL="http/protobuf"
export OTEL_EXPORTER_OTLP_TRACES_HEADERS="Authorization=Bearer <your API key>"Check that traces arrived
Run the agent and open Traces. LiveKit names its agent after your Agent class in snake case, so a PaymentsAgent class shows as payments_agent. A dispatched job shows under that name too, not under the agent name it was dispatched to. In the run, each turn shows its model calls and its tool calls with their arguments and results.
A new run shows a Scoring badge until Amiril has ranked it. If no run appears, read what the exporter logged.
Next steps
Add Amiril to a LiveKit voice agent
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.
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.
Find a run and see why it was flagged
Open a run from an issue, the Anomalies page or the Traces page and see why Amiril flagged it. Then review it to teach Amiril which rarity factors are noise.
Set up environments and API keys
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.
Last updated on