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

# OpenAI

> Using Moda with OpenAI

## Overview

Moda automatically tracks your OpenAI API calls. Chat completions, streaming, function calling, and tool use are all captured with no additional code required.

## Setup

<CodeGroup>
  ```bash Python theme={"dark"}
  pip install moda-ai openai
  ```

  ```bash Node.js theme={"dark"}
  npm install moda-ai openai
  ```
</CodeGroup>

<CodeGroup>
  ```python Python theme={"dark"}
  import moda
  from openai import OpenAI

  moda.init("YOUR_MODA_API_KEY")

  client = OpenAI()
  response = client.chat.completions.create(
      model="gpt-4o",
      messages=[{"role": "user", "content": "Hello!"}]
  )

  moda.flush()
  ```

  ```typescript Node.js theme={"dark"}
  import { Moda } from 'moda-ai';
  import OpenAI from 'openai';

  await Moda.init('YOUR_MODA_API_KEY');

  const client = new OpenAI();
  const response = await client.chat.completions.create({
    model: 'gpt-4o',
    messages: [{ role: 'user', content: 'Hello!' }]
  });

  await Moda.flush();
  ```
</CodeGroup>

## Supported Features

| Feature                     | Captured                         |
| --------------------------- | -------------------------------- |
| Chat completions            | Yes                              |
| Streaming                   | Yes                              |
| Function calling / tool use | Yes (captured as content blocks) |
| Embeddings                  | Yes                              |
| Token usage                 | Yes (input, output, total)       |
| Model name                  | Yes (request and response)       |

## Streaming

Streaming responses are automatically tracked. The SDK captures the complete response after the stream finishes:

<CodeGroup>
  ```python Python theme={"dark"}
  stream = client.chat.completions.create(
      model="gpt-4o",
      messages=[{"role": "user", "content": "Count to 5"}],
      stream=True,
  )

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

  ```typescript Node.js theme={"dark"}
  const stream = await client.chat.completions.create({
    model: 'gpt-4o',
    messages: [{ role: 'user', content: 'Count to 5' }],
    stream: true,
  });

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

## Tool Use

Tool calls and function calling are automatically captured with full input/output details:

<CodeGroup>
  ```python Python theme={"dark"}
  response = client.chat.completions.create(
      model="gpt-4o",
      messages=[{"role": "user", "content": "What's the weather in London?"}],
      tools=[{
          "type": "function",
          "function": {
              "name": "get_weather",
              "description": "Get the weather for a location",
              "parameters": {
                  "type": "object",
                  "properties": {
                      "location": {"type": "string"}
                  }
              }
          }
      }]
  )
  ```

  ```typescript Node.js theme={"dark"}
  const response = await client.chat.completions.create({
    model: 'gpt-4o',
    messages: [{ role: 'user', content: "What's the weather in London?" }],
    tools: [{
      type: 'function',
      function: {
        name: 'get_weather',
        description: 'Get the weather for a location',
        parameters: {
          type: 'object',
          properties: {
            location: { type: 'string' }
          }
        }
      }
    }]
  });
  ```
</CodeGroup>

## Troubleshooting

**Data not appearing?**

* Ensure `moda.init()` is called before creating the OpenAI client
* Call `moda.flush()` (Python) or `await Moda.flush()` (Node.js) before exit
* Verify your API key is correct

**Streaming responses incomplete?**

* The SDK captures the full response after the stream ends. Ensure you consume the entire stream.

<Info>
  For full SDK documentation, see the [Python SDK](/ingestion/moda-sdk) or [Node.js SDK](/ingestion/moda-sdk-node) guides.
</Info>
