> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2-docs-scavio-google-v2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

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

```python opik_via_openinference.py theme={null}
"""
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.",
    debug_mode=True,
    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

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="Install dependencies">
    ```bash theme={null}
    uv pip install -U agno openai openinference-instrumentation-agno opentelemetry-exporter-otlp opentelemetry-sdk yfinance
    ```
  </Step>

  <Step title="Export environment variables">
    <CodeGroup>
      ```bash Mac/Linux theme={null}
      export OPENAI_API_KEY="your_openai_api_key_here"
      export OTEL_EXPORTER_OTLP_ENDPOINT="your_otel_exporter_otlp_endpoint_here"
      export OTEL_EXPORTER_OTLP_HEADERS="your_otel_exporter_otlp_headers_here"
      ```

      ```bash Windows theme={null}
      $Env:OPENAI_API_KEY="your_openai_api_key_here"
      $Env:OTEL_EXPORTER_OTLP_ENDPOINT="your_otel_exporter_otlp_endpoint_here"
      $Env:OTEL_EXPORTER_OTLP_HEADERS="your_otel_exporter_otlp_headers_here"
      ```
    </CodeGroup>
  </Step>

  <Step title="Run the example">
    Save the code above as `opik_via_openinference.py`, then run:

    ```bash theme={null}
    python opik_via_openinference.py
    ```
  </Step>
</Steps>

Full source: [cookbook/observability/opik\_via\_openinference.py](https://github.com/agno-agi/agno/blob/main/cookbook/observability/opik_via_openinference.py)
