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

# Middleware

> LLM Observability with Middleware and OpenLIT

To send OpenTelemetry metrics and traces generated by OpenLIT from your AI Application to Middleware, follow the below steps.

### 1. Get your Credentials

1. **Sign in to your Middleware account**: Go to [Middleware Dashboard](https://app.middleware.io/)
2. **Navigate to API Keys**: Go to **Settings** → **API Keys** ([Direct Link](https://app.middleware.io/settings/api-keys))
3. **Copy your credentials**:

   * **MW\_API\_KEY**: Your Middleware API key for authentication
   * **MW\_TARGET**: Your Middleware target URL endpoint

   Save these values - you'll need them for configuration.

### 2. Instrument your application

<Tabs>
  <Tab title="SDK">
    **For direct integration into your Python applications:**

    <Tabs>
      <Tab title="Function Arguments">
        ```python theme={null}
        import openlit

        openlit.init(
          otlp_endpoint="MW_TARGET",
          application_name="YOUR_APPLICATION_NAME",
          otlp_headers={
              "Authorization": "MW_API_KEY",
              "X-Trace-Source": "openlit",
          }
        )
        ```

        Replace:

        1. `MW_TARGET` with your Middleware target URL from Step 1.
           * Example: `https://abcd.middleware.io:443`
        2. `MW_API_KEY` with your Middleware API key from Step 1.
           * Example: `dxyxsdojzrgpsvizzzcsvhrwnmzqdsdsd`
        3. `YOUR_APPLICATION_NAME` with your application name.
      </Tab>

      <Tab title="Environment Variables">
        ```python theme={null}
        import openlit

        openlit.init()
        ```

        Set these environment variables:

        ```shell theme={null}
        export OTEL_EXPORTER_OTLP_ENDPOINT="MW_TARGET"
        export OTEL_SERVICE_NAME="YOUR_APPLICATION_NAME"
        export OTEL_EXPORTER_OTLP_HEADERS="Authorization=MW_API_KEY,X-Trace-Source=openlit"
        ```

        Replace:

        1. `MW_TARGET` with your Middleware target URL from Step 1.
           * Example: `https://abcd.middleware.io:443`
        2. `MW_API_KEY` with your Middleware API key from Step 1.
           * Example: `dxyxsdojzrgpsvizzzcsvhrwnmzqdsdsd`
        3. `YOUR_APPLICATION_NAME` with your application name.
      </Tab>
    </Tabs>

    Refer to the OpenLIT [Python SDK repository](https://github.com/openlit/openlit/tree/main/sdk/python) for more advanced configurations and use cases.
  </Tab>

  <Tab title="CLI">
    **For zero-code auto-instrumentation via command line:**

    <Tabs>
      <Tab title="CLI Arguments">
        ```shell theme={null}
        # Using CLI arguments
        openlit-instrument \
          --otlp-endpoint "MW_TARGET" \
          --otlp-headers "Authorization=MW_API_KEY,X-Trace-Source=openlit" \
          --service-name "YOUR_APPLICATION_NAME" \
          --deployment-environment "production" \
          python app.py
        ```

        Replace:

        1. `MW_TARGET` with your Middleware target URL from Step 1.
           * Example: `https://abcd.middleware.io:443`
        2. `MW_API_KEY` with your Middleware API key from Step 1.
           * Example: `dxyxsdojzrgpsvizzzcsvhrwnmzqdsdsd`
        3. `YOUR_APPLICATION_NAME` with your application name.
      </Tab>

      <Tab title="Environment Variables">
        ```shell theme={null}
        # Set environment variables (takes precedence over CLI args)
        export OTEL_EXPORTER_OTLP_ENDPOINT="MW_TARGET"
        export OTEL_EXPORTER_OTLP_HEADERS="Authorization=MW_API_KEY,X-Trace-Source=openlit"
        export OTEL_SERVICE_NAME="YOUR_APPLICATION_NAME"
        export OTEL_DEPLOYMENT_ENVIRONMENT="production"

        # Run your application
        openlit-instrument python app.py
        ```

        Replace:

        1. `MW_TARGET` with your Middleware target URL from Step 1.
           * Example: `https://abcd.middleware.io:443`
        2. `MW_API_KEY` with your Middleware API key from Step 1.
           * Example: `dxyxsdojzrgpsvizzzcsvhrwnmzqdsdsd`
        3. `YOUR_APPLICATION_NAME` with your application name.
      </Tab>
    </Tabs>

    Refer to the OpenLIT [Python SDK repository](https://github.com/openlit/openlit/tree/main/sdk/python) for more advanced configurations and use cases.
  </Tab>
</Tabs>

### 3. Visualize in Middleware

Once your LLM application is instrumented, you can explore the telemetry data in Middleware:

1. **Navigate to LLM Observability**: Go to your [Middleware Dashboard](https://app.middleware.io/) and click on **LLM Observability** in the sidebar
2. **Explore AI Operations**: View your AI application traces including:
   * LLM request traces with detailed timing
   * Token usage and cost information
   * Vector database operations
   * Model performance analytics
   * Request/response payloads (if enabled)
3. **Custom Dashboards**: Create custom dashboards for your specific LLM metrics
4. **Alerting**: Set up alerts for LLM performance anomalies and cost thresholds
5. **Performance Analysis**: Analyze latency, throughput, and resource usage patterns

For detailed information on LLM Observability features, consult the [Middleware LLM Observability Documentation](https://docs.middleware.io/llm-observability/overview).
