Need more detail? Connect your coding agent to the full docs.

Skip to Content
AMIRILDocs

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-http

Turn 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.

tracing.py
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 provider

Call 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:

  1. Switch to the environment your runs belong to, with the menu at the top of the page.
  2. Open Data and select Set up a new installation. It opens a panel that connects an agent to Amiril.
  3. In the panel, enter a Key name under Create a new key and select Create key.
  4. 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

Last updated on