Skip to main content

LlamaIndex

Send LlamaIndex 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. A Lens worker is required for investigations; viewing traces does not require one.

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/llamaindex
cp .env.example .env

If you already cloned the repository, run the remaining commands from llamaindex/. 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 key
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 LITELLM_GATEWAY_URL/v1/traces with the LiteLLM 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 FunctionAgent answers one question.

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

See simple/main.py for the implementation.

Agent swarm​

An AgentWorkflow hands off from research_agent to search_agent, then writer_agent.

uv run --env-file .env --package lens-llamaindex-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 the run associated with research_agent. 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​

The LlamaIndex instrumentor is enabled before importing the framework. The examples label the process as research_agent and disable framework streaming; specialist names appear in workflow and handoff data.

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 tracing is enabled on the same gateway. A model call succeeding does not confirm that its trace export succeeded.

Multiple model-related spans can represent one model call. Specialist names and model-call spend depend on the attributes exported by the framework and how the gateway normalizes them.

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