Opik Via OpenInference

Exports agent, model, and tool spans with custom trace attributes to Opik via an OTLP HTTP span processor.

Demonstrates instrumenting Agno with OpenTelemetry and exporting traces to Opik.

opik_via_openinference.py
"""
Opik Via OpenInference
======================

Demonstrates instrumenting Agno with OpenTelemetry and exporting traces to Opik.
"""

from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.yfinance import YFinanceTools
from openinference.instrumentation.agno import AgnoInstrumentor
from opentelemetry import trace as trace_api
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
# Configure OpenTelemetry to export spans to Opik
tracer_provider = TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter()))
trace_api.set_tracer_provider(tracer_provider)

# Enable automatic instrumentation for Agno
AgnoInstrumentor().instrument()


# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
    name="Stock Price Agent",
    model=OpenAIChat(id="gpt-5.2"),
    tools=[YFinanceTools()],
    instructions="You are a stock price analyst. Answer with concise, well-sourced updates.",
    trace_attributes={
        "session.id": "demo-session-001",
        "environment": "development",
    },
)


# ---------------------------------------------------------------------------
# Run Example
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    # The span hierarchy (agent -> model -> tool) will appear in Opik for every request
    agent.print_response(
        "What is the current price of Apple and how did it move today?"
    )

Run the Example

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno openai openinference-instrumentation-agno opentelemetry-exporter-otlp opentelemetry-sdk yfinance

Export environment variables

These values target Opik Cloud. Replace the API key, project and workspace placeholders; use your instance URL for self-hosting. See Opik OTLP setup.

export OPENAI_API_KEY="your_openai_api_key_here"
export OTEL_EXPORTER_OTLP_ENDPOINT="https://www.comet.com/opik/api/v1/private/otel"
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=your_opik_api_key,projectName=your_project,Comet-Workspace=your_workspace"

Run the example

Save the code above as opik_via_openinference.py, then run:

python opik_via_openinference.py

Full source: cookbook/observability/opik_via_openinference.py