Skip to main content

Framework examples through LiteLLM

Use these examples if your agents call models through a LiteLLM gateway. First create a tracing key and copy the full Lens trace endpoint. The gateway sends model requests; Lens receives traces.

For a standalone Lens installation with a direct provider, use the first-trace example or the direct-provider template in your framework's integration guide.

Configure the exporter​

Run these exports in the terminal where you start your agent. Paste the endpoint and key from Lens tracing setup:

export LENS_TRACING_KEY="<paste your Lens tracing key>"
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="<paste the full Traces endpoint>"
export OTEL_EXPORTER_OTLP_TRACES_HEADERS="Authorization=Bearer $LENS_TRACING_KEY"
export OTEL_EXPORTER_OTLP_PROTOCOL="http/protobuf"
export OTEL_METRICS_EXPORTER="none"
export OTEL_LOGS_EXPORTER="none"
export OTEL_SERVICE_NAME="research_agent"

Keep your existing model configuration if you are adding tracing to an application. Initialize instrumentation before creating the agent. If your app already has an OpenTelemetry tracer provider, keep it and update its exporter instead of creating a second one.

The framework examples below use LiteLLM for model calls. For a direct provider connection, use the OpenAI Agents SDK or OpenTelemetry direct-provider template. Both send telemetry directly to Lens.

For the gateway examples below, also set the model connection:

export LITELLM_GATEWAY_URL="<your gateway base URL without a trailing slash or /v1>"
export LITELLM_API_KEY="<your key with model access>"
export LITELLM_MODEL="<your configured model alias>"

Get a model key from your administrator or Virtual Keys in the dashboard, and copy the model alias from Models. The local Docker gateway uses http://localhost:4000. The model key and the tracing key serve different purposes.

Run your agent​

Choose your framework. Each tab includes dependencies, a complete example, and the run command. The agent is named research_agent using the framework's name, role, or tracing setting. Change it to your own agent's name.

Use a model that supports tool calls for agent frameworks. For Python examples, use Python 3.13 and a separate virtual environment:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install opentelemetry-distro \
opentelemetry-exporter-otlp-proto-http \
langgraph openinference-instrumentation-langchain langchain-openai

Save this as agent.py:

agent.py
import os

AGENT_NAME = "research_agent"

from opentelemetry.instrumentation.auto_instrumentation import initialize

initialize()

from langgraph.graph import END, START, MessagesState, StateGraph
from langchain_openai import ChatOpenAI

model = ChatOpenAI(
model=os.environ["LITELLM_MODEL"],
base_url=f"{os.environ['LITELLM_GATEWAY_URL']}/v1",
api_key=os.environ["LITELLM_API_KEY"],
)
graph = StateGraph(MessagesState)
graph.add_node("answer", lambda state: {"messages": [model.invoke(state["messages"])]})
graph.add_edge(START, "answer")
graph.add_edge("answer", END)
agent = graph.compile(name=AGENT_NAME)
result = agent.invoke({"messages": [{"role": "user", "content": "What is an agent trace?"}]})
print(result["messages"][-1].content)
python agent.py

Full LangGraph guide and examples

For complete projects and multi-agent examples, use the Integrations guides in the sidebar or the examples repository. Those projects use LENS_URL for the ingestion base URL, without /v1/traces; their exporters append that path. The dashboard shows framework snippets directly in the tracing setup section.

For a working example, use DeepLite. Set LITELLM_DEV_BASE=https://<your-lens-ingestion-host>/v1/traces and LITELLM_DEV_KEY=<your-lens-tracing-key> in its .env file, then run the agent.

To record personal coding sessions, follow the Claude Code and Codex setup.

4. View your first trace​

Open Lens > Traces. Select a time range that includes your run, then open it. For the examples above, look for research_agent. The same name is available under Agent when creating an investigation. Select a step to read its input, output, and attributes.

A research_agent trace with its question, model call, and final answer.

Check that you can see the task, tool results, and final answer. If these are missing, update your agent's instrumentation before running an investigation.

Troubleshooting​

What you seeWhat to check
A model answer appears, but no traceSet the exporter variables in the same terminal as your agent. Initialize instrumentation before creating the agent. Check the terminal for exporter errors.
401 from the trace endpointUse a dedicated Lens tracing key. Model keys cannot upload traces.
404 or 410 from the trace endpointCopy the full endpoint from the dashboard. Keep /lens-ingest when present and include /v1/traces once.
429 just after creating a keyAllow up to 30 seconds for credential sync and retry.
Setup asks for LITELLM_LENS_PUBLIC_URL, or an upload returns 503Ask the administrator to check the service connection.
The model request failsCheck the gateway URL, model key, and model alias separately from the tracing settings.
Traces have no input or outputCheck the framework's content-capture settings in its integration guide.
LiteLLM Enterprise
SSO/SAML, audit logs, spend tracking, multi-team management, and guardrails, built for production.
Learn more →