OpenInference LiteLLM Instrumentation

0.1.30 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

This quickstart demonstrates how to set up OpenInference LiteLLM Instrumentation with a basic OpenTelemetry configuration, making an LLM call via LiteLLM. It initializes a `TracerProvider`, configures an `OTLPSpanExporter` (e.g., for a local Phoenix collector), instruments LiteLLM, and then executes a sample `completion` and `embedding` call. Remember to set your `OPENAI_API_KEY` environment variable.

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.")

view raw JSON →