Langtrace Integration

Auto-instrument a YFinance stock-price agent with langtrace.init() from the Langtrace Python SDK.

Demonstrates instrumenting an Agno agent with Langtrace.

This example initializes Langtrace after importing Agno model modules, so automatic instrumentation may not attach. Create a Langtrace project and generate an API key before running the example. See Langtrace Python SDK setup.

langtrace_op.py
"""
Langtrace Integration
=====================

Demonstrates instrumenting an Agno agent with Langtrace.
"""

# Must precede other imports
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.yfinance import YFinanceTools
from langtrace_python_sdk import langtrace  # type: ignore

# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
langtrace.init()


# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
    name="Stock Price Agent",
    model=OpenAIChat(id="gpt-5.2"),
    tools=[YFinanceTools()],
    instructions="You are a stock price agent. Answer questions in the style of a stock analyst.",
)


# ---------------------------------------------------------------------------
# Run Example
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    agent.print_response("What is the current price of Tesla?")

Run the Example

Set up your virtual environment

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

Install dependencies

uv pip install -U agno langtrace-python-sdk openai yfinance

Export your Langtrace and OpenAI API keys

export LANGTRACE_API_KEY="your_langtrace_api_key_here"
export OPENAI_API_KEY="your_openai_api_key_here"

Save and fix the initialization order

Save the code above as langtrace_op.py, then move the langtrace_python_sdk import and langtrace.init() before every Agno and OpenAI import.

Run the example

Run the corrected file:

python langtrace_op.py

Full source: cookbook/observability/langtrace_op.py