# 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 [#install]

```bash
pip install opentelemetry-sdk opentelemetry-exporter-otlp-proto-http
```

## Turn on trace export [#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.

```py title="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 [#set-the-endpoint-and-key]

In Amiril, create an API key and copy the endpoint:

1. Switch to the [environment](/traces/environments-and-keys) 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:

```bash
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 [#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](/traces/opentelemetry#check-that-traces-arrived).

## Next steps

- [Add Amiril to a LiveKit voice agent](https://docs.amiril.ai/sdk/livekit.md): 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](https://docs.amiril.ai/sdk/livekit-production.md): 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](https://docs.amiril.ai/monitor/runs.md): 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](https://docs.amiril.ai/traces/environments-and-keys.md): 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.

## More detail

- [Trace attributes Amiril reads](https://docs.amiril.ai/assistant/traces/attributes.md): Every span attribute and Langfuse field Amiril reads, per dialect: what makes a run, tool call or generation, how agent and session are found, the OTLP receiver, and what is ignored.
- [Troubleshooting](https://docs.amiril.ai/assistant/troubleshooting.md): Symptom, cause, how to confirm and fix for common Amiril problems: missing runs, plan limits, wrong agent or environment, rules that never fire or never block, SDK, key, import and MCP failures.
