> ## Documentation Index
> Fetch the complete documentation index at: https://langwatch.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## 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.

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

> [!NOTE]
>
> 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.md).

## Installation

```bash pip theme={null}
pip install langwatch litellm
```

```bash uv theme={null}
uv add langwatch litellm
```

## Usage

> [!NOTE]
>
> 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 theme={null}
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.md) - Learn how to capture RAG data from retrievers and tools
* [Capturing Metadata and Attributes](https://langwatch.ai/docs/integration/python/tutorials/capturing-metadata.md) - Add custom metadata and attributes to your traces and spans
* [Capturing Evaluations & Guardrails](https://langwatch.ai/docs/integration/python/tutorials/capturing-evaluations-guardrails.md) - Log evaluations and implement guardrails in your LiteLLM applications


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.