LiteLLM (tracing)

LiteLLM provides a unified Python interface for calling 100+ LLM providers using the OpenAI format. Respan’s LiteLLM instrumentation registers a native LiteLLM callback and sends completion spans through respan-tracing, with optional gateway routing through the OpenAI-compatible Respan endpoint.

Create an account at platform.respan.ai and grab an API key.

Run npx @respan/cli setup to set up with your coding agent.

See LiteLLM gateway setup to route this integration through the Respan gateway.

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-litellm litellm
2

Set environment variables

$export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

OPENAI_API_KEY (or any provider key) is used for LLM requests. RESPAN_API_KEY is used to export traces to Respan.

3

Initialize and run

Initialize Respan with the LiteLLM instrumentor. Requests go directly to providers; the callback emits canonical chat spans to Respan.

1import os
2
3import litellm
4from respan import Respan
5from respan_instrumentation_litellm import LiteLLMInstrumentor
6
7respan = Respan(
8 api_key=os.environ["RESPAN_API_KEY"],
9 app_name="litellm-service",
10 instrumentations=[LiteLLMInstrumentor()],
11)
12
13response = litellm.completion(
14 model="gpt-4.1-nano",
15 messages=[{"role": "user", "content": "Say hello in three languages."}],
16)
17print(response.choices[0].message.content)
18respan.flush()
4

View your trace

Open the Traces page to see your LiteLLM completions across providers as auto-traced spans.

Configuration

Respan

ParameterTypeDefaultDescription
api_keystr | NoneRESPAN_API_KEY env varRespan API key used for trace export.
base_urlstr | NoneRESPAN_BASE_URL env varRespan API base URL.
app_namestr"respan"Service name shown on exported spans.
instrumentationslist[]Include LiteLLMInstrumentor() to activate LiteLLM tracing.

LiteLLMInstrumentor

ParameterTypeDefaultDescription
include_contentboolTrueCapture request messages and assistant response content on chat spans.

See the LiteLLM SDK reference for the full API.

Attributes

Set defaults on Respan(...), or override attributes per request with propagate_attributes.

1with respan.propagate_attributes(
2 customer_identifier="user-123",
3 thread_identifier="conv_abc_123",
4 trace_group_identifier="litellm-support-flow",
5 metadata={"plan": "pro"},
6):
7 response = litellm.completion(
8 model="gpt-4.1-nano",
9 messages=[{"role": "user", "content": "Hello!"}],
10 )
AttributeDescription
customer_identifierIdentifies the end user in Respan analytics.
thread_identifierGroups related messages into a conversation.
trace_group_identifierGroups related traces for search and filtering.
metadataCustom key-value pairs attached to the span.

Async usage

The callback supports async completions automatically.

1import litellm
2from respan import Respan
3from respan_instrumentation_litellm import LiteLLMInstrumentor
4
5respan = Respan(instrumentations=[LiteLLMInstrumentor()])
6
7response = await litellm.acompletion(
8 model="gpt-4.1-nano",
9 messages=[{"role": "user", "content": "Tell me a joke"}],
10)
11respan.flush()

Multiple providers

LiteLLM’s unified interface means all providers are logged with the same callback.

1import litellm
2from respan import Respan
3from respan_instrumentation_litellm import LiteLLMInstrumentor
4
5respan = Respan(instrumentations=[LiteLLMInstrumentor()])
6
7litellm.completion(model="gpt-4.1-nano", messages=[...])
8litellm.completion(model="claude-sonnet-4-5-20250929", messages=[...])
9litellm.completion(model="together_ai/meta-llama/Llama-3-70b", messages=[...])
10respan.flush()