When working with complex LLM applications, it is common to need to track a traces across multiple services. Opik supports distributed tracing out of the box when integrating using function decorators using a mechanism that is similar to how OpenTelemetry implements distributed tracing.
For the purposes of this guide, we will assume that you have a simple LLM application that is made up of two services: a client and a server. We will assume that the client will create the trace and span, while the server will add a nested span. In order to do this, the trace_id and span_id will be passed in the headers of the request from the client to the server.
The Python SDK includes some helper functions to make it easier to fetch headers in the client and ingest them in the server:
On the server side, you can pass the headers to your decorated function:
The opik_distributed_trace_headers parameter is added by the track decorator to each function that is decorated
and is a dictionary with the keys opik_trace_id and opik_parent_span_id.
As an alternative to passing opik_distributed_trace_headers as a parameter, you can use the distributed_headers() context manager for more explicit control over distributed header handling. This approach provides automatic cleanup, error handling, and optional data flushing.
The distributed_headers() context manager accepts two parameters:
headers: A dictionary containing the distributed trace headers (opik_trace_id and opik_parent_span_id)flush (optional): Whether to flush the Opik client data after the root span is processed. Defaults to False. Set to True if you want to ensure immediate data transmission.The context manager automatically creates a root span with the provided headers, handles any errors that occur during execution, and cleans up the context when complete.
For more details and additional examples, see the distributed_headers context manager API reference.
When the downstream service is instrumented with the standard OpenTelemetry SDK (rather than the Opik SDK), Opik provides helpers to bridge the two systems so the OTel span produced by the remote service still appears under the correct Opik trace and parent span.
The bridge works through two HTTP headers carried from the client to the remote service:
opik_trace_id — the Opik trace the OTel span should be attached to.opik_parent_span_id — the Opik span to use as the parent (optional).On the receiving side, the helper translates these headers into two OpenTelemetry span attributes (opik.trace_id, opik.parent_span_id) recognized by the Opik OTLP ingest endpoint. Both values must be valid UUIDs; blank or malformed values are dropped with a warning so a misconfigured caller never silently corrupts the parent linkage.
The remote service creates a span with the OpenTelemetry SDK as usual and then calls the Opik bridging helper with the incoming HTTP headers. The helper sets the opik.trace_id / opik.parent_span_id / opik.span_id attributes on the boundary span only.
To make sure descendant OpenTelemetry spans (children created inside the boundary span via start_as_current_span / tracer.startSpan) also land under the original Opik trace and parent, register the OpikSpanProcessor on the same TracerProvider as your OTLP exporter. Without it, only the boundary span is linked and its descendants are orphaned in a synthetic Opik trace.
In Python, OpikSpanProcessor ships with the main opik package under opik.integrations.otel. In TypeScript it lives in a separate opik-otel package — install it alongside opik (npm install opik-otel @opentelemetry/api @opentelemetry/sdk-trace-base).
OpikSpanProcessor only mutates spans whose parent already carries the Opik attributes (set by attach_to_parent / attachToParent on the boundary, or inherited from upstream W3C baggage). Spans outside an attached subtree are left untouched, so today’s behaviour for unrelated OTel traces is unchanged.
The remote service must be configured with an OTLP exporter pointing at the Opik backend (/v1/private/otel/v1/traces). See the OpenTelemetry Python SDK integration guide for a full exporter configuration example; the same endpoint is used by the OpenTelemetry JS/Node SDK.