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

# OpenAI

> Wrap the OpenAI client to meter and gate Chat Completions and Responses API calls, streaming included.

## Set up

Wrap an instance of `OpenAI` from `openai`. The SDK recognizes it automatically, and both the Chat Completions and Responses APIs are covered.

```typescript theme={null}
import OpenAI from 'openai';

const openai = stigg.wrap(new OpenAI());

const stream = await openai.chat.completions.create({
  model: 'gpt-5',
  messages,
  stream: true,
  stream_options: { include_usage: true }, // required for streamed usage
  stiggContext: { customerId: 'customer-acme', entitlement: { currencyId: 'credits' } },
});
```

<Note>
  The SDK never modifies your request. For streamed Chat Completions, set `stream_options: { include_usage: true }` yourself, or the provider sends no usage to meter.
</Note>

## What is reported

Every OpenAI response includes a `usage` object: the number of tokens the call consumed, split into input and output, with the number of input tokens served from OpenAI's prompt cache. Prompt caching lets OpenAI reuse the start of a prompt from an earlier call at a lower price. The field names differ between the two APIs. The wrapper reads these counts and reports them to Stigg as the metrics below. For details on each field, see OpenAI's [Chat Completions reference](https://developers.openai.com/api/reference/resources/chat/subresources/completions/methods/create), [Responses reference](https://developers.openai.com/api/reference/resources/responses/methods/create) and [prompt caching guide](https://developers.openai.com/api/docs/guides/prompt-caching).

| Method | Event name |
| - | - |
| `chat.completions.create` | `openai.chat.completions.create` |
| `responses.create` | `openai.responses.create` |

**Chat Completions**

| Metric | What it counts |
| - | - |
| `prompt_tokens` | Input tokens, **including** cached tokens |
| `completion_tokens` | Output tokens |
| `cached_tokens` | Input tokens served from the cache |
| `total_tokens` | All input and output tokens, as reported by the vendor |

**Responses**

| Metric | What it counts |
| - | - |
| `input_tokens` | Input tokens, **including** cached tokens |
| `output_tokens` | Output tokens |
| `cached_tokens` | Input tokens served from the cache |
| `total_tokens` | All input and output tokens, as reported by the vendor |

<Warning>
  OpenAI includes cached tokens **inside** `prompt_tokens` and `input_tokens`. If you price both the input metric and `cached_tokens`, cached tokens are billed twice. Price one or the other, or use the [normalized metrics](/documentation/ai-agents/llm-wrappers/metering#normalized-metrics-across-vendors). Likewise, `total_tokens` includes everything above: price the total, or the parts, but never both.
</Warning>

OpenAI has no field for cache creation, so a feature fed by `cache_creation_input_tokens` stays at zero for OpenAI traffic.

Every usage field the vendor returns is also included, with a `raw.` prefix. See [How usage is reported](/documentation/ai-agents/llm-wrappers/supported-providers#how-usage-is-reported).


## Related topics

- [OpenAI-compatible endpoints](/documentation/ai-agents/llm-wrappers/providers/openai-compatible.md)
- [Example - OpenAI pricing](/documentation/modeling-your-pricing-in-stigg/credits/how-to-examples.md)
- [OpenRouter](/documentation/ai-agents/llm-wrappers/providers/openrouter.md)
- [Ollama](/documentation/ai-agents/llm-wrappers/providers/ollama.md)
- [vLLM](/documentation/ai-agents/llm-wrappers/providers/vllm.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.