# OpenAI

> Connect the OpenAI API to ACOG with your own API key and call GPT models through either the OpenAI or the Anthropic API shape.

Product: AI Cost Optimizer Gateway (ACOG). Web version: https://tessarac.com/docs/acog/providers/openai. Last updated: 2026-10-16.

A Direct OpenAI connection sends requests to the OpenAI API with your organization's own OpenAI API key.

## Connect

1. In the [OpenAI platform](https://platform.openai.com/api-keys), create an API key in the project whose limits and billing ACOG's traffic should use. A restricted key needs the Model capabilities (chat completions, embeddings) and read access to models.
2. In ACOG, open **Admin → Providers → Connect provider**, choose **Direct** and **OpenAI**, and paste the key.
3. Review the models and choose a default.

## Settings

| Setting | Notes |
|---|---|
| Base URL | Defaults to `https://api.openai.com`. On the hosted service it must be a public HTTPS address on port 443. |
| Models | The GPT models this connection serves |
| Cache lifetime | How long OpenAI keeps prompt-cache entries for requests ACOG marks: in memory (the provider default) or 24 hours, where the model supports it. See [Caching](https://tessarac.com/docs/acog/optimize/caching.md#cache-lifetime). |

## What ACOG adds to your requests

ACOG sets a `prompt_cache_key` derived from each request's system prompt and tools, so requests that share the same prefix land on the same OpenAI prompt cache and are billed at the cached-input rate. It never changes your prompt.

## Calling OpenAI through ACOG

- The [Chat Completions API](https://tessarac.com/docs/acog/api/chat-completions.md) at `https://gateway.acog.tessarac.com/v1`.
- The [Embeddings API](https://tessarac.com/docs/acog/api/embeddings.md).
- The [Messages API](https://tessarac.com/docs/acog/api/messages.md): ACOG translates Anthropic-shaped requests to OpenAI and back, so an Anthropic SDK can call a GPT model.
- The OpenAI API exactly as OpenAI defines it, including the Responses API, at the native route `https://gateway.acog.tessarac.com/openai/v1`. See [Native provider routes](https://tessarac.com/docs/acog/api/native-routes.md).

## Troubleshooting

| What you see | Cause | Fix |
|---|---|---|
| `422 upstream_auth_failed` | OpenAI rejected the stored key | Paste a valid key into the connection |
| `422 model_access_required` | The OpenAI project cannot use the model | Check the project's model access |
| `429` from OpenAI | Your OpenAI project's rate limit | Raise the limit with OpenAI, or declare the model on a second connection so ACOG can spread load |
