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

# OpenAI

> Track costs for GPT-4, GPT-3.5, o1, DALL-E, Whisper, and all OpenAI models

This guide covers how to send OpenAI usage data to Fenra.

## Chat Completions (GPT-4, GPT-3.5, o1, o3)

<CodeGroup>
  ```javascript Node.js theme={null}
  import OpenAI from 'openai';

  const openai = new OpenAI();

  async function chat(messages) {
    const response = await openai.chat.completions.create({
      model: 'gpt-4o',
      messages
    });

    // Send to Fenra
    await fetch('https://ingest.fenra.io/usage/transactions', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-Api-Key': process.env.FENRA_API_KEY
      },
      body: JSON.stringify({
        provider: 'openai',
        model: response.model,
        usage: [{
          type: 'tokens',
          metrics: {
            input_tokens: response.usage.prompt_tokens,
            output_tokens: response.usage.completion_tokens,
            total_tokens: response.usage.total_tokens
          }
        }],
        context: {
          billable_customer_id: process.env.BILLABLE_CUSTOMER_ID
        }
      })
    });

    return response;
  }
  ```

  ```python Python theme={null}
  from openai import OpenAI
  import requests
  import os

  client = OpenAI()

  def chat(messages):
      response = client.chat.completions.create(
          model="gpt-4o",
          messages=messages
      )

      # Send to Fenra
      requests.post(
          'https://ingest.fenra.io/usage/transactions',
          headers={
              'Content-Type': 'application/json',
              'X-Api-Key': os.getenv('FENRA_API_KEY')
          },
          json={
              'provider': 'openai',
              'model': response.model,
              'usage': [{
                  'type': 'tokens',
                  'metrics': {
                      'input_tokens': response.usage.prompt_tokens,
                      'output_tokens': response.usage.completion_tokens,
                      'total_tokens': response.usage.total_tokens
                  }
              }],
              'context': {
                  'billable_customer_id': os.getenv('BILLABLE_CUSTOMER_ID')
              }
          }
      )

      return response
  ```
</CodeGroup>

## Prompt Caching

When the response includes `prompt_tokens_details.cached_tokens`, include it:

```javascript theme={null}
usage: [{
  type: 'tokens',
  metrics: {
    input_tokens: response.usage.prompt_tokens,
    output_tokens: response.usage.completion_tokens,
    total_tokens: response.usage.total_tokens,
    cached_tokens: response.usage.prompt_tokens_details?.cached_tokens || 0
  }
}]
```

## Reasoning Models (o1, o3)

OpenAI's reasoning models include `reasoning_tokens`. Include them for accurate cost tracking:

```javascript theme={null}
usage: [{
  type: 'tokens',
  metrics: {
    input_tokens: response.usage.prompt_tokens,
    output_tokens: response.usage.completion_tokens,
    total_tokens: response.usage.total_tokens,
    reasoning_tokens: response.usage.completion_tokens_details?.reasoning_tokens || 0
  }
}]
```

## Batch and Priority Tiers

OpenAI offers different pricing tiers. Specify the tier using `model_tier`:

```javascript theme={null}
{
  provider: 'openai',
  model: 'gpt-4o',
  model_tier: 'batch',  // Options: 'batch', 'priority', 'flex', 'standard'
  usage: [{
    type: 'tokens',
    metrics: {
      input_tokens: response.usage.prompt_tokens,
      output_tokens: response.usage.completion_tokens,
      total_tokens: response.usage.total_tokens
    }
  }],
  context: {
    billable_customer_id: process.env.BILLABLE_CUSTOMER_ID
  }
}
```

## Multimodal Tokens (Realtime API)

For realtime and multimodal models, track separate token types:

```javascript theme={null}
usage: [{
  type: 'tokens',
  metrics: {
    input_tokens: response.usage.prompt_tokens,
    output_tokens: response.usage.completion_tokens,
    total_tokens: response.usage.total_tokens,
    text_tokens: response.usage.prompt_tokens_details?.text_tokens || 0,
    audio_tokens: response.usage.prompt_tokens_details?.audio_tokens || 0,
    image_tokens: response.usage.prompt_tokens_details?.image_tokens || 0
  }
}]
```

## Image Generation (DALL-E)

```javascript theme={null}
const response = await openai.images.generate({
  model: 'dall-e-3',
  prompt: 'A sunset over mountains',
  n: 1,
  size: '1024x1024',
  quality: 'hd'
});

await fetch('https://ingest.fenra.io/usage/transactions', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-Api-Key': process.env.FENRA_API_KEY
  },
  body: JSON.stringify({
    provider: 'openai',
    model: 'dall-e-3',
    usage: [{
      type: 'images',
      metrics: {
        generated: response.data.length,
        size_px: 1024,
        quality: 'hd'  // Options: 'standard', 'hd' for DALL-E
      }
    }],
    context: {
      billable_customer_id: process.env.BILLABLE_CUSTOMER_ID
    }
  })
});
```

## GPT Image Models

For GPT Image 1.5 and GPT Image 1, include quality and dimensions:

```javascript theme={null}
const response = await openai.images.generate({
  model: 'gpt-image-1.5',
  prompt: 'A futuristic city',
  quality: 'high',
  size: '1024x1536'
});

await fetch('https://ingest.fenra.io/usage/transactions', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-Api-Key': process.env.FENRA_API_KEY
  },
  body: JSON.stringify({
    provider: 'openai',
    model: 'gpt-image-1.5',
    usage: [{
      type: 'images',
      metrics: {
        generated: response.data.length,
        quality: 'high',  // Options: 'low', 'medium', 'high'
        width_px: 1024,
        height_px: 1536
      }
    }],
    context: {
      billable_customer_id: process.env.BILLABLE_CUSTOMER_ID
    }
  })
});
```

## Audio (Whisper, TTS)

For Whisper transcription:

```javascript theme={null}
usage: [{
  type: 'audio_seconds',
  metrics: {
    input_seconds: audioDurationInSeconds,
    total_seconds: audioDurationInSeconds
  }
}]
```

For TTS:

```javascript theme={null}
usage: [{
  type: 'audio_seconds',
  metrics: {
    output_seconds: estimatedDuration,
    total_seconds: estimatedDuration
  }
}]
```

## Supported Models

Fenra supports all OpenAI models. Common models include:

| Model                   | Type      | Usage Type                |
| ----------------------- | --------- | ------------------------- |
| `gpt-4o`, `gpt-4o-mini` | Chat      | `tokens`                  |
| `gpt-4-turbo`, `gpt-4`  | Chat      | `tokens`                  |
| `gpt-3.5-turbo`         | Chat      | `tokens`                  |
| `o1-preview`, `o1-mini` | Reasoning | `tokens` (with reasoning) |
| `o3`, `o3-mini`         | Reasoning | `tokens` (with reasoning) |
| `dall-e-3`, `dall-e-2`  | Image     | `images`                  |
| `whisper-1`             | Audio     | `audio_seconds`           |
| `tts-1`, `tts-1-hd`     | Audio     | `audio_seconds`           |

## Next Steps

* [Set up alerts](/guides/alerts) to get notified when OpenAI spending exceeds thresholds
* [View the dashboard](/product-overview/dashboard) to see your OpenAI costs
