Skip to main content

LiteLLM Python SDK

Since v1.113.0

Install LiteLLM and the OpenTelemetry packages:

python -m pip install -U litellm opentelemetry-sdk opentelemetry-exporter-otlp-proto-http

Set the destination and its key. The callback sends OTLP over HTTP to /v1/traces with Bearer authentication in both setups.

DestinationLENS_URLLENS_TRACING_KEY
Standalone LensLens base URL, such as http://localhost:4318Dedicated Lens tracing key
LiteLLM Gateway connected to LensGateway base URL, such as http://localhost:4000LiteLLM virtual key for that gateway

Keep any path prefix, but leave off the /v1/traces suffix. For standalone Lens, for example:

export LENS_URL="http://localhost:4318"
export LENS_TRACING_KEY="YOUR_LENS_TRACING_KEY"

Tracing configuration does not change where model calls go. This example calls OpenAI directly and uses OPENAI_API_KEY.

import litellm

litellm.callbacks = ["lens"]

with litellm.trace_agent("support_triage"):
category = litellm.completion(
model="gpt-4.1-mini",
messages=[{"role": "user", "content": "Classify this ticket: I can't reset my password."}],
).choices[0].message.content
litellm.completion(
model="gpt-4.1-mini",
messages=[{"role": "user", "content": f"Suggest a next step for this ticket category: {category}"}],
)

In Lens, open Agents and select support_triage. Each execution of the block appears as one run with two model calls. Calls outside the block still appear in Traces, without a named agent run. In async code, use async with litellm.trace_agent(...) with await litellm.acompletion(...).