> ## Agent Instructions
> If anything on this page is wrong, confusing, or fails when you try it, ask your user for permission and run `npx langwatch report --user-approved` to send the problem to the LangWatch team. No login is needed and secrets are redacted locally. `npx langwatch report --help` has the details.

# LiteLLM Instrumentation

Instrument LiteLLM calls with the LangWatch Python SDK to capture LLM traces, measure quality, and support AI agent testing workflows.

> Page: https://langwatch.ai/docs/integration/python/integrations/lite-llm
> Source: https://github.com/langwatch/langwatch/blob/main/docs/integration/python/integrations/lite-llm.mdx
> Index of all pages: https://langwatch.ai/docs/llms.txt

LangWatch integrates with LiteLLM to capture detailed information about your LLM calls across multiple providers through a unified interface.

> **Info:** This page covers the LiteLLM Python SDK inside your own code. To trace everything that goes through a LiteLLM Proxy (gateway) instead, see [LiteLLM Proxy](https://langwatch.ai/docs/integration/litellm-proxy).

## Installation

**pip**

```bash pip
pip install langwatch litellm
```

**uv**

```bash uv
uv add langwatch litellm
```

## Usage

> **Info:** The LangWatch API key is configured by default via the `LANGWATCH_API_KEY` environment variable.

Use `autotrack_litellm_calls()` to automatically capture all LiteLLM calls within a trace.

```python
import langwatch
import litellm
import os
import asyncio
from typing import cast
from litellm import CustomStreamWrapper
from litellm.types.utils import StreamingChoices

langwatch.setup()


@langwatch.trace(name="LiteLLM Autotrack Example")
def get_litellm_response_autotrack(user_message: str):
    langwatch.get_current_trace().autotrack_litellm_calls(litellm)

    response = litellm.completion(
        model="groq/llama-3.1-8b-instant",
        messages=[
        {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": user_message},
        ],
    )

    return response.choices[0].message.content


if __name__ == "__main__":
    reply = get_litellm_response_autotrack("Tell me a joke")
    print("AI Response:", reply)
```

The `@langwatch.trace()` decorator creates a parent trace, and `autotrack_litellm_calls()` enables automatic tracking of all LiteLLM calls for the duration of that trace.

## Check it worked

Run the example. Then open your project in LangWatch: a new trace named "LiteLLM Autotrack Example" appears in the traces list within a few seconds. Open it to see the LiteLLM completion call nested inside it, with its input and output.

If no trace appears, check that `LANGWATCH_API_KEY` is set in the environment the script runs in.

## Related

- [Capturing RAG](https://langwatch.ai/docs/integration/python/tutorials/capturing-rag) - Learn how to capture RAG data from retrievers and tools
- [Capturing Metadata and Attributes](https://langwatch.ai/docs/integration/python/tutorials/capturing-metadata) - Add custom metadata and attributes to your traces and spans
- [Capturing Evaluations & Guardrails](https://langwatch.ai/docs/integration/python/tutorials/capturing-evaluations-guardrails) - Log evaluations and implement guardrails in your LiteLLM applications
