LiteLLM Instrumentation
Learn how to instrument LiteLLM calls with the LangWatch Python SDK.
LiteLLM provides a unified interface to various Large Language Models. LangWatch integrates with LiteLLM by capturing OpenTelemetry traces, enabling detailed observability into your LLM calls made through LiteLLM.
This guide outlines three primary approaches for instrumenting LiteLLM with LangWatch:
- Using
autotrack_litellm_calls()
: This method, part of the LangWatch SDK, dynamically patches your LiteLLM module instance for the current trace to capture its calls. - Using LiteLLM’s Native OpenTelemetry Tracing with Global Setup: LiteLLM can automatically generate OpenTelemetry traces for its operations when a global OpenTelemetry environment (established by
langwatch.setup()
) is active. - Using Community OpenTelemetry Instrumentors (for Underlying SDKs): If LiteLLM internally uses other instrumented SDKs (like the
openai
SDK for OpenAI models), you can leverage community instrumentors for those specific underlying SDKs.
Using autotrack_litellm_calls()
The autotrack_litellm_calls()
function, called on a trace object, provides a straightforward way to capture all LiteLLM calls for the duration of the current trace. This is often the most direct way to ensure LiteLLM operations are captured by LangWatch within a specific traced function.
You typically call this method on the trace object obtained via langwatch.get_current_trace()
inside a function decorated with @langwatch.trace()
.
Key points for autotrack_litellm_calls()
:
- It must be called on an active trace object (e.g., obtained via
langwatch.get_current_trace()
). - It instruments the passed
litellm
module instance specifically for the current trace.
Using Community OpenTelemetry Instrumentors
If you are already using a dedicated community instrumentor for LiteLLM, such as the one provided by OpenInference, you can pass an instance of LiteLLMInstrumentor
from openinference.instrumentation.litellm
to the instrumentors
list in langwatch.setup()
.
1. Via langwatch.setup()
You can pass an instance of LiteLLMInstrumentor
from openinference.instrumentation.litellm
to the instrumentors
list in langwatch.setup()
.
Ensure you have the openinference-instrumentation-litellm
library installed.
2. Direct Instrumentation with LiteLLMInstrumentor
If you are managing your OpenTelemetry setup more directly, you can call instrument()
on an instance of LiteLLMInstrumentor
.
Key points for using OpenInference LiteLLMInstrumentor
:
- This instrumentor specifically targets LiteLLM calls.
- It provides an alternative to
autotrack_litellm_calls
if you prefer an explicit instrumentor pattern or are using OpenInference across your stack.
Which Approach to Choose?
autotrack_litellm_calls()
: Best for explicit, trace-specific instrumentation of LiteLLM. Offers clear control over when LiteLLM calls are tracked by LangWatch within a given trace.- OpenInference
LiteLLMInstrumentor
: Use if you are standardizing on OpenInference instrumentors or prefer this explicit way of instrumenting LiteLLM itself (rather than its underlying SDKs). It provides traces directly from LiteLLM’s perspective.
Choose the method that best fits your instrumentation strategy and the level of detail required.