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

# OpenAI

> Trace and monitor OpenAI GPT model calls with Netra auto-instrumentation. Capture prompts, completions, token usage, and latency with zero code changes.

<img src="https://mintcdn.com/netra/IXT7TOAHn4HQhvyF/images/integration-logos/llm-providers/openai.png?fit=max&auto=format&n=IXT7TOAHn4HQhvyF&q=85&s=a841531738176ac694dd5c40a1d486df" alt="OpenAI" width="274" height="80" data-path="images/integration-logos/llm-providers/openai.png" />

## Installation

Install both the Netra SDK and OpenAI SDK:

<CodeGroup>
  ```bash Python theme={null}
  pip install netra-sdk openai
  ```

  ```bash Typescript theme={null}
  npm install netra-sdk openai
  ```
</CodeGroup>

## Usage

Initialize the Netra SDK with OpenAI instrumentation enabled. The SDK automatically traces all OpenAI API calls once initialized.

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra
  from openai import OpenAI

  # Initialize Netra with OpenAI instrumentation
  Netra.init(
      app_name="my-ai-app",
      headers=f"x-api-key={os.environ.get("NETRA_API_KEY")}",
      trace_content=True
  )

  # Use OpenAI client as usual - all calls are automatically traced
  client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

  completion = client.chat.completions.create(
      model="gpt-4",
      messages=[
          {"role": "user", "content": "What is observability?"}
      ]
  )

  print(completion.choices[0].message.content)
  ```

  ```typescript Typescript theme={null}
  import { Netra } from "netra-sdk";
  import OpenAI from "openai";

  async function main() {
    // Initialize Netra with OpenAI instrumentation (must await)
    await Netra.init({
      appName: "my-ai-app",
      headers: `x-api-key=${process.env.NETRA_API_KEY}`,
      traceContent: true
    });

    // Use OpenAI client as usual - all calls are automatically traced
    const openai = new OpenAI({
      apiKey: process.env.OPENAI_API_KEY
    });

    const completion = await openai.chat.completions.create({
      model: "gpt-4",
      messages: [
        { role: "user", content: "What is observability?" }
      ]
    });

    console.log(completion.choices[0].message.content);
  }

  main();
  ```
</CodeGroup>

### Streaming Responses

The SDK automatically handles streaming responses and captures the complete output:

<CodeGroup>
  ```python Python theme={null}
  stream = client.chat.completions.create(
      model="gpt-4",
      messages=[{"role": "user", "content": "Tell me a story"}],
      stream=True
  )

  for chunk in stream:
      if chunk.choices[0].delta.content:
          print(chunk.choices[0].delta.content, end="")
  ```

  ```typescript Typescript theme={null}
  const stream = await openai.chat.completions.create({
    model: "gpt-4",
    messages: [{ role: "user", content: "Tell me a story" }],
    stream: true
  });

  for await (const chunk of stream) {
    process.stdout.write(chunk.choices[0]?.delta?.content || "");
  }
  ```
</CodeGroup>

### Embeddings

Embedding operations are also automatically instrumented:

<CodeGroup>
  ```python Python theme={null}
  embedding = client.embeddings.create(
      model="text-embedding-ada-002",
      input="The quick brown fox jumps over the lazy dog"
  )

  print(embedding.data[0].embedding)
  ```

  ```typescript Typescript theme={null}
  const embedding = await openai.embeddings.create({
    model: "text-embedding-ada-002",
    input: "The quick brown fox jumps over the lazy dog"
  });

  console.log(embedding.data[0].embedding);
  ```
</CodeGroup>

### Selective Instrumentation

Control which integrations are enabled using the `instruments` or `blockInstruments` configuration:

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra
  from netra.instrumentation.instruments import InstrumentSet

  # Only enable OpenAI instrumentation
  Netra.init(
      headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
      instruments={InstrumentSet.OPENAI}
  )

  # Or block specific instrumentations
  Netra.init(
      headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
      block_instruments={InstrumentSet.HTTPX}
  )
  ```

  ```typescript Typescript theme={null}
  import { Netra, NetraInstruments } from "netra-sdk";

  // Only enable OpenAI instrumentation
  await Netra.init({
    headers: `x-api-key=${process.env.NETRA_API_KEY}`,
    instruments: new Set([NetraInstruments.OPENAI])
  });

  // Or block specific instrumentations
  await Netra.init({
    headers: `x-api-key=${process.env.NETRA_API_KEY}`,
    blockInstruments: new Set([NetraInstruments.HTTP])
  });
  ```
</CodeGroup>

## Next Steps

* [Quick Start Guide](https://docs.getnetra.ai/quick-start/python) - Complete setup and configuration
* [Auto Instrumentation](https://docs.getnetra.ai/tracing/auto-instrumentation) - Automatic tracing for supported libraries
* [Decorators](https://docs.getnetra.ai/tracing/decorators) - Add custom tracing with `@workflow`, `@agent`, and `@task` decorators
* [Session Tracking](https://docs.getnetra.ai/tracing/session) - Track user sessions and conversations
* [OpenAI Documentation](https://platform.openai.com/docs/quickstart) - Official OpenAI API quickstart guide
