OpenInference LiteLLM Instrumentation
OpenInference LiteLLM Instrumentation provides automatic OpenTelemetry-compatible tracing for applications using the LiteLLM SDK or LiteLLM Proxy. It captures traces for various LiteLLM functions, including `completion()`, `acompletion()`, `embedding()`, and `image_generation()`. This library is part of the Arize AI OpenInference project, which maintains a frequent release cadence across its instrumentation packages, ensuring up-to-date support for various LLM frameworks and providers.
Warnings
- gotcha When tracing image generation calls, the instrumentation currently sets the output as a URL attribute rather than rendering the image directly within the trace. Displaying the image requires a UI-side change in the observability tool.
- gotcha There may be inconsistencies in how output (parsed vs. raw object) is set in span attributes for streamed versus non-streamed LiteLLM calls. While recent updates (v0.1.21) improved full JSON output, manual verification of span attributes for different call types is recommended.
- gotcha The `litellm.responses` function (which is labeled as 'beta' in LiteLLM) is not directly instrumented by `openinference-instrumentation-litellm`.
- breaking If you are using LiteLLM version 1.0.0 or higher, be aware that LiteLLM itself introduced breaking changes, including requiring `openai>=1.0.0`, changes to error types (e.g., `openai.InvalidRequestError` to `openai.BadRequestError`), and response objects inheriting from `BaseModel` instead of `OpenAIObject`. While these are LiteLLM's changes, they impact how your application interacts with instrumented LiteLLM calls.
- gotcha For proper auto-instrumentation, the `LiteLLMInstrumentor().instrument()` call and the OpenTelemetry `TracerProvider` setup must occur *before* any LiteLLM calls are made in your application. Loading instrumentation too late can result in untraced operations.
Install
-
pip install openinference-instrumentation-litellm litellm opentelemetry-sdk opentelemetry-exporter-otlp
Imports
- LiteLLMInstrumentor
from openinference.instrumentation.litellm import LiteLLMInstrumentor
- TracerProvider
from opentelemetry.sdk.trace import TracerProvider
- SimpleSpanProcessor
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
- OTLPSpanExporter
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
Quickstart
import os
import litellm
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from openinference.instrumentation.litellm import LiteLLMInstrumentor
# Configure OpenTelemetry Tracer Provider
resource = Resource.create({
"service.name": "my-litellm-app"
})
tracer_provider = TracerProvider(resource=resource)
# Example: Export traces to a local OpenTelemetry Collector (e.g., Phoenix)
# Ensure a collector is running, e.g., 'python -m phoenix.server.main serve'
OTEL_EXPORTER_OTLP_ENDPOINT = os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT", "http://127.0.0.1:6006/v1/traces")
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(OTEL_EXPORTER_OTLP_ENDPOINT)))
# Set the global tracer provider
from opentelemetry import trace
trace.set_tracer_provider(tracer_provider)
# Instrument LiteLLM
LiteLLMInstrumentor().instrument(tracer_provider=tracer_provider)
# Set LiteLLM API key (e.g., for OpenAI model)
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY", "YOUR_OPENAI_API_KEY_HERE")
try:
print("Making a LiteLLM completion call...")
completion_response = litellm.completion(
model="gpt-3.5-turbo",
messages=[{"content": "What's the capital of France?", "role": "user"}]
)
print("Completion received:", completion_response.choices[0].message.content)
print("\nMaking a LiteLLM embedding call...")
embedding_response = litellm.embedding(
model="text-embedding-ada-002",
input=["Hello, world!"]
)
print("Embedding received (first 10 chars):", str(embedding_response.data[0].embedding)[:10] + "...")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure your API key is set correctly and the model is accessible.")
finally:
# It's important to shut down the tracer provider to ensure all spans are exported.
print("\nShutting down tracer provider...")
tracer_provider.shutdown()
print("Traces exported.")