Working with OpenAI Agents SDK
Trace OpenAI Agents SDK workflows end-to-end with LangDB—monitor model calls, tool invocations, and runner sessions via one-line init().
LangDB helps you add full tracing and observability to your OpenAI Agents SDK workflows—without changing your core logic. With a one-line initialization, LangDB captures model calls, tool invocations, and intermediate steps, giving you a complete view of how your agent operates.
Installation
Enable end-to-end tracing for your OpenAI Agents SDK agents by installing the pylangdb
client with the openai
feature flag:
pip install pylangdb[openai]
Quick Start
Export Environment Variables
Set your LangDB credentials:
export LANGDB_API_KEY="<your_langdb_api_key>"
export LANGDB_PROJECT_ID="<your_langdb_project_id>"
Initialize Tracing
Import and run the initialize before configuring your OpenAI client:
from pylangdb.openai import init
# Initialise LangDB
init()
Configure OpenAI Client and Agent Runner
# Agent SDK imports
from agents import (
Agent,
Runner,
set_default_openai_client,
RunConfig,
ModelProvider,
Model,
OpenAIChatCompletionsModel
)
from openai import AsyncOpenAI
# Configure the OpenAI client with LangDB headers
client = AsyncOpenAI(
api_key=os.environ["LANGDB_API_KEY"],
base_url=os.environ["LANGDB_API_BASE_URL"],
default_headers={"x-project-id": os.environ["LANGDB_PROJECT_ID"]}
)
set_default_openai_client(client)
# Create a custom model provider for advanced routing
class CustomModelProvider(ModelProvider):
def get_model(self, model_name: str | None) -> Model:
return OpenAIChatCompletionsModel(model=model_name, openai_client=client)
# Register your custom model provider to route model calls through LangDB
CUSTOM_MODEL_PROVIDER = CustomModelProvider()
# Assign a unique group_id to link all steps in this session trace
group_id = str(uuid.uuid4())
response = await Runner.run(
agent,
input="Hello, world!",
run_config=RunConfig(
model_provider=CUSTOM_MODEL_PROVIDER, # Inject custom model provider
group_id=group_id # Link all steps to the same trace
)
)
Once executed, LangDB links all steps—model calls, intermediate tool usage, and runner orchestration—into a single session trace.
Last updated
Was this helpful?