Skip to main content

OpenAI Agents SDK

Send OpenAI Agents SDK traces to LiteLLM Lens using the runnable examples in this repository.

Prerequisites​

You need a LiteLLM gateway with tracing enabled, a LiteLLM key, and a configured model alias. The swarm example needs a model that supports tool calls. The Lens service receives and stores traces separately from the gateway and runs investigations. Generate a dedicated tracing key in Lens > Traces > Set up tracing.

Install uv. It uses the checked-in Python version and resolves each example’s dependencies from its uv workspace.

Configuration​

For a fresh checkout:

git clone https://github.com/BerriAI/litellm-lens-example.git
cd litellm-lens-example/openai-agents
cp .env.example .env

If you already cloned the repository, run the remaining commands from openai-agents/. Copy .env.example to .env if it does not exist, then set:

VariableValue
LITELLM_GATEWAY_URLYour gateway’s base URL without a trailing slash or /v1, for example http://localhost:4002
LITELLM_API_KEYYour LiteLLM model key
LENS_URLThe ingestion URL from Lens tracing setup, for example http://localhost:4318 or https://gateway.example/lens-ingest
LENS_TRACING_KEYThe dedicated tracing key from Lens tracing setup
LITELLM_MODELA model alias configured on your gateway

The checked-in values target a local development gateway. Replace them for your deployment. Keep the exporter settings from .env.example; the examples configure their trace exporters in code. They send traces to LENS_URL/v1/traces with the tracing key as a bearer token.

Leave MOCK_LITELLM_GATEWAY_URL unset unless you intend to send an additional trace copy to the local recorder.

Run an example​

Simple agent​

A research_agent answers one question inside research_workflow.

uv run --env-file .env --package lens-openai-agents-simple simple/main.py

See simple/main.py for the implementation.

Agent swarm​

A coordinator invokes search_agent and writer_agent through agents-as-tools.

uv run --env-file .env --package lens-openai-agents-swarm swarm/main.py

See swarm/main.py for the implementation.

Verify the trace​

After the example prints its answer, open Lens > Traces on your gateway and select the new run. Look for research_workflow and its research_agent span. Inspect the input, output, and model spans. For the swarm, inspect the specialist activity described above; its exact span layout depends on the framework.

How tracing works​

OpenInference exports SDK agent and Responses API spans to LiteLLM. The shared gateway transport records each physical model request and its gateway call ID, including retries.

See the shared gateway transport for request-attempt and spend-correlation details.

Troubleshooting​

If model calls fail, check the gateway URL, key, and model alias. If an answer appears but the trace is missing, check the terminal for exporter errors and confirm the Lens ingestion service is reachable with your tracing key. A model call succeeding does not confirm that its trace export succeeded.

Use a gateway model alias that supports the Responses API and tool calls.

LiteLLM Enterprise
SSO/SAML, audit logs, spend tracking, multi-team management, and guardrails, built for production.
Learn more →