---
title: "Python"
description: "Use OpenLIT SDK in Python with Middleware LLM Observability to trace AI model calls, capture metrics, and debug efficiently."
section: "LLM Observability"
breadcrumb: "LLM Observability > Openlit > Python"
canonical: "https://docs.middleware.io/features/llm/openlit-python"
---
# Python

This guide will walk you through the process of connecting the OpenLIT SDK to the middleware, enabling you to observe Python LLM workloads from end to end. The key idea: initialise OpenLIT, pass your Middleware endpoint and headers, then run at least one real LLM call to see data in LLM Observability.

## Before you begin

- Have credentials handy: Your Middleware UID (for the OTLP endpoint) and Middleware API key (for the `Authorization` header).
- Two setup modes: Either pass config as function arguments or via environment variables (both shown below).
- Metrics toggle: You can disable metrics with `disable_metrics=True` or `OPENLIT_DISABLE_METRICS=true`.

## 1. Install the SDK

Run this in your terminal to add OpenLIT:

```bash
pip install openlit
```

## 2. Initialize the SDK

You can configure OpenLIT via function arguments or environment variables. Pick one consistent approach from the following.

### A. Setup using function arguments

In your LLM application, initialise OpenLIT with your tenant endpoint and headers:

```js
import openlit

openlit.init(
    otlp_endpoint="https://<MW_UID>.middleware.io:443",
    application_name="YOUR_APPLICATION_NAME",
    otlp_headers={
        "Authorization": "<MW_API_KEY>",
        "X-Trace-Source": "openlit",
    },
)
```

### B. Setup using environment variables

First, export the required variables so OpenLIT can auto-read them:

```js
export OPENLIT_OTLP_ENDPOINT="your-initial-uid"
export OPENLIT_APPLICATION_NAME="YOUR_APPLICATION_NAME"
export OPENLIT_OTLP_HEADERS='{"Authorization": "your-initial-token", "X-Trace-Source": "openlit"}'
```

Then, initialise OpenLIT in code without parameters (it will use the env vars):

```js
import openlit

openlit.init()
```

> **Note:** **Note**: Metrics collection can be disabled either by passing `disable_metrics=True` to init or setting `OPENLIT_DISABLE_METRICS=true`.

## 3. Use the SDK

Here’s the example from the doc showing OpenLIT monitoring with OpenAI. Use it as a template for your first request:

```py
from openai import OpenAI
import openlit

openlit.init(
    otlp_endpoint="https://<MW_UID>.middleware.io:443",
    application_name="YOUR_APPLICATION_NAME",
    otlp_headers={
        "Authorization": "<MW_API_KEY>",
        "X-Trace-Source": "openlit",
    },
)

client = OpenAI(
    api_key="YOUR_OPENAI_KEY"
)

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "What is LLM Observability",
        }
    ],
    model="gpt-3.5-turbo",
)
```

## Viewing your traces and metrics

After initialisation and at least one LLM request, open the [LLM Observability section in Middleware](https://app.middleware.io/llm/traces?) to see traces and metrics for your Python service.

For advanced options, refer to the [OpenLIT Python SDK docs](https://docs.openlit.io/latest/sdk/configuration).

## Troubleshooting

- No data showing up: Make sure an actual LLM request was executed after `openlit.init(...)`, and that your service can reach `https://<MW_UID>.middleware.io:443`.
- Auth/tenant issues: Re-check the exact `Authorization` value (Middleware API key) and the `otlp_endpoint` UID.
- Env-var setup not picked up: Confirm the variable names (`OPENLIT_OTLP_ENDPOINT`, `OPENLIT_APPLICATION_NAME`, `OPENLIT_OTLP_HEADERS`) and then call `openlit.init()` with no arguments.
- Metrics missing only: If traces appear but metrics don’t, verify you haven’t set `disable_metrics=True` or `OPENLIT_DISABLE_METRICS=true` unintentionally.

> **Note:** Need assistance or want to learn more about using OpenLIT with Middleware? Refer to the [OpenLIT documentation](https://docs.openlit.io/latest/overview) or get in touch with us via our [Contact Us](https://middleware.io/contact-us/).
