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

# OpenAI

> Connect your OpenAI-powered application to Latitude Telemetry for feature-level observability and evaluations.

## Overview

This guide shows you how to integrate **Latitude Telemetry** into an existing application that uses the official **OpenAI SDK**.

After completing these steps:

* Every OpenAI call (e.g. `chat.completions.create`) can be captured as a log in Latitude.
* Logs are grouped under a **prompt**, identified by a `path`, inside a Latitude **project**.
* You can inspect inputs/outputs, measure latency, and debug OpenAI-powered features from the Latitude dashboard.

<Check>
  You'll keep calling OpenAI exactly as you do today — Telemetry simply observes
  and enriches those calls.
</Check>

***

## Requirements

Before you start, make sure you have:

* A **Latitude account** and **API key**
* A **Latitude project ID**
* A Node.js or Python-based project that uses the **OpenAI SDK**

That's it — prompts do **not** need to be created ahead of time.

***

## Steps

<Steps>
  <Step title="Install requirements">
    Add the Latitude Telemetry package to your project:

    <Tabs>
      <Tab title="TypeScript">
        <CodeGroup>
          ```bash npm theme={null}
          npm add @latitude-data/telemetry
          ```

          ```bash pnpm theme={null}
          pnpm add @latitude-data/telemetry
          ```

          ```bash yarn theme={null}
          yarn add @latitude-data/telemetry
          ```

          ```bash bun theme={null}
          bun add @latitude-data/telemetry
          ```
        </CodeGroup>
      </Tab>

      <Tab title="Python">
        <CodeGroup>
          ```bash pip theme={null}
          pip install latitude-telemetry
          ```

          ```bash uv theme={null}
          uv add latitude-telemetry
          ```

          ```bash poetry theme={null}
          poetry add latitude-telemetry
          ```
        </CodeGroup>
      </Tab>
    </Tabs>
  </Step>

  <Step title="Wrap your OpenAI-powered feature">
    Initialize Latitude Telemetry and wrap the code that calls OpenAI using <code>telemetry.capture</code>.

    <Tabs>
      <Tab title="TypeScript">
        ```ts theme={null}
        import { LatitudeTelemetry } from '@latitude-data/telemetry'
        import OpenAI from 'openai'

        const telemetry = new LatitudeTelemetry(
          process.env.LATITUDE_API_KEY,
          { instrumentations: { openai: OpenAI } }
        )

        async function generateSupportReply(input: string) {
          return telemetry.capture(
            {
              projectId: 123, // The ID of your project in Latitude
              path: 'generate-support-reply', // Add a path to identify this prompt in Latitude
            },
            async () => {
              const client = new OpenAI()
              const completion = await client.chat.completions.create({
                model: 'gpt-4o',
                messages: [{ role: 'user', content: input }],
              })
              return completion.choices[0].message.content
            }
          )
        }
        ```
      </Tab>

      <Tab title="Python">
        You can use the `capture` method as a decorator (recommended) or as a context manager:

        ```python Using decorator (recommended) theme={null}
        import os
        from openai import OpenAI
        from latitude_telemetry import Telemetry, Instrumentors, TelemetryOptions

        telemetry = Telemetry(
            os.environ["LATITUDE_API_KEY"],
            TelemetryOptions(instrumentors=[Instrumentors.OpenAI]),
        )

        @telemetry.capture(
            project_id=123,  # The ID of your project in Latitude
            path="generate-support-reply",  # Add a path to identify this prompt in Latitude
        )
        def generate_support_reply(input: str) -> str:
            client = OpenAI()
            completion = client.chat.completions.create(
                model="gpt-4o",
                messages=[{"role": "user", "content": input}],
            )
            return completion.choices[0].message.content
        ```

        ```python Using context manager theme={null}
        import os
        from openai import OpenAI
        from latitude_telemetry import Telemetry, Instrumentors, TelemetryOptions

        telemetry = Telemetry(
            os.environ["LATITUDE_API_KEY"],
            TelemetryOptions(instrumentors=[Instrumentors.OpenAI]),
        )

        def generate_support_reply(input: str) -> str:
            with telemetry.capture(
                project_id=123,  # The ID of your project in Latitude
                path="generate-support-reply",  # Add a path to identify this prompt in Latitude
            ):
                client = OpenAI()
                completion = client.chat.completions.create(
                    model="gpt-4o",
                    messages=[{"role": "user", "content": input}],
                )
                return completion.choices[0].message.content
        ```
      </Tab>
    </Tabs>

    <Info>
      The `path`:

      * Identifies the prompt in Latitude
      * Can be new or existing
      * Should not contain spaces or special characters (use letters, numbers, `- _ / .`)
    </Info>
  </Step>
</Steps>

***

## Streaming responses

When using streaming (`stream: true`), consume the stream inside your capture block so the span covers the entire operation.

<Tabs>
  <Tab title="TypeScript">
    **Consume the stream inside** your `capture()` callback. The span stays open until your callback completes:

    ```typescript theme={null}
    async function streamSupportReply(input: string, res: Response) {
      await telemetry.capture(
        { projectId: 123, path: 'generate-support-reply' },
        async () => {
          const client = new OpenAI()
          const stream = await client.chat.completions.create({
            model: 'gpt-4o',
            messages: [{ role: 'user', content: input }],
            stream: true,
          })

          // Consume stream inside capture — span covers entire operation
          for await (const chunk of stream) {
            const content = chunk.choices[0]?.delta?.content
            if (content) {
              res.write(content)
            }
          }
          res.end()
        }
      )
    }
    ```

    <Info>
      By consuming the stream inside capture, the span duration accurately reflects the total time of the operation, and all child spans from OpenAI instrumentation are properly nested.
    </Info>
  </Tab>

  <Tab title="Python">
    **Use a generator function** with the decorator. The SDK keeps the span open until all chunks are yielded:

    ```python theme={null}
    @telemetry.capture(project_id=123, path="generate-support-reply")
    async def stream_support_reply(input: str):
        client = OpenAI()
        stream = client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": input}],
            stream=True,
        )
        for chunk in stream:
            if chunk.choices[0].delta.content:
                yield chunk.choices[0].delta.content
    ```

    <Info>
      The generator pattern is ideal for streaming — each `yield` sends a chunk to the caller while the span remains open. The span ends automatically when the generator is exhausted.
    </Info>
  </Tab>
</Tabs>

***

## Seeing your logs in Latitude

Once your feature is wrapped, logs will appear automatically.

1. Open the **prompt** in your Latitude dashboard (identified by `path`)
2. Go to the **Traces** section
3. Each execution will show:
   * Input and output messages
   * Model and token usage
   * Latency and errors
   * One trace per feature invocation

Each OpenAI call appears as a child span under the captured prompt execution, giving you a full, end-to-end view of what happened.

***

## That's it

No changes to your OpenAI calls, no special return values, and no extra plumbing — just wrap the feature you want to observe.
