> This page location: AI Gateway > APIs > Chat completions
> Neon is a serverless backend platform for apps and agents: Lakebase Postgres for transactional data, authentication for user sign-in, a Data API for querying Postgres from the browser, S3-compatible object storage for files, long-running functions for APIs, agents, and webhooks, an AI gateway for LLMs, and realtime for syncing browser state with your database. Every service branches with your data for dev, test, and preview. Pricing: https://neon.com/pricing.md. Plans and limits: https://neon.com/docs/introduction/plans.md.
> Full Neon documentation index: https://neon.com/docs/llms.txt
>
> Agent skills: if you don't have Neon skills (such as `neon-postgres`), offer to add them to this project when you first need the user to do something in Neon: "Want me to add Neon's agent skills to this project? They help me with Neon setup, connections, and branching." Install: `npx neon@latest skills -y`. To also run Neon steps for the user (Neon CLI, sign-in, MCP), see https://neon.com/prompts/agent-setup.md

> Summary: The chat completions endpoint is the recommended starting point for Neon AI Gateway. It is OpenAI Chat Completions-compatible, works with any model in the catalog, and lets you switch providers without changing your SDK code.

# Chat completions

The OpenAI-compatible unified endpoint

Last updated: 2026-11-18

The chat completions endpoint is the recommended way to use Neon AI Gateway. It's fully compatible with the [OpenAI Chat Completions API](https://platform.openai.com/docs/api-reference/chat) and works with every model in the [AI Gateway catalog](https://neon.com/docs/ai-gateway/models). Switch models by changing a single field.

**Base URL:** `https://<branch-host>/v1`

This endpoint is also reachable at the longer `/ai-gateway/mlflow/v1/chat/completions` path. Both behave identically and neither is deprecated. See [Shorter paths](https://neon.com/docs/ai-gateway/models#shorter-paths) for the full list of aliases.

If you're using an OpenRouter-compatible client that asks for a base URL, set it to `https://<branch-host>/v1` and call `/chat/completions`.

## Setup

Set these environment variables. See [Get started](https://neon.com/docs/ai-gateway/get-started) for how to obtain them.

```bash
NEON_AI_GATEWAY_TOKEN=nt_live_...
NEON_AI_GATEWAY_BASE_URL=https://br-winter-pond-aptw82ef-api.ai.c-2.us-east-2.aws.neon.tech
```

## Basic request

**TypeScript (OpenAI SDK)**

```typescript
import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: process.env.NEON_AI_GATEWAY_TOKEN,
  baseURL: `${process.env.NEON_AI_GATEWAY_BASE_URL}/v1`,
});

const response = await client.chat.completions.create({
  model: 'gpt-5-mini',
  messages: [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'What is Neon?' },
  ],
  max_tokens: 256,
});

console.log(response.choices[0].message.content);
```

**Python (OpenAI SDK)**

```python
from openai import OpenAI
import os

client = OpenAI(
    api_key=os.environ["NEON_AI_GATEWAY_TOKEN"],
    base_url=f"{os.environ['NEON_AI_GATEWAY_BASE_URL']}/v1",
)

response = client.chat.completions.create(
    model="gpt-5-mini",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is Neon?"},
    ],
    max_tokens=256,
)

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

**cURL**

```bash
curl -X POST "$NEON_AI_GATEWAY_BASE_URL/v1/chat/completions" \
  -H "Authorization: Bearer $NEON_AI_GATEWAY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5-mini",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "What is Neon?"}
    ],
    "max_tokens": 256
  }'
```

## Streaming

Add `stream: true` to receive a server-sent events response.

**TypeScript (OpenAI SDK)**

```typescript
import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: process.env.NEON_AI_GATEWAY_TOKEN,
  baseURL: `${process.env.NEON_AI_GATEWAY_BASE_URL}/v1`,
});

const stream = await client.chat.completions.create({
  model: 'gpt-5-mini',
  messages: [{ role: 'user', content: 'Explain branching in Postgres.' }],
  stream: true,
});

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

**Python (OpenAI SDK)**

```python
from openai import OpenAI
import os

client = OpenAI(
    api_key=os.environ["NEON_AI_GATEWAY_TOKEN"],
    base_url=f"{os.environ['NEON_AI_GATEWAY_BASE_URL']}/v1",
)

with client.chat.completions.create(
    model="gpt-5-mini",
    messages=[{"role": "user", "content": "Explain branching in Postgres."}],
    stream=True,
) as stream:
    for chunk in stream:
        print(chunk.choices[0].delta.content or "", end="", flush=True)
```

**cURL**

```bash
curl -X POST "$NEON_AI_GATEWAY_BASE_URL/v1/chat/completions" \
  -H "Authorization: Bearer $NEON_AI_GATEWAY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5-mini",
    "messages": [{"role": "user", "content": "Explain branching in Postgres."}],
    "stream": true
  }'
```

## Switching models

Change the `model` field to use a different provider. Everything else stays the same.

```typescript
// OpenAI
model: 'gpt-5-4'

// Google
model: 'gemini-3-flash'

// Alibaba
model: 'qwen3-next-80b-a3b-instruct'
```

For a few Claude and Gemini models, `message.content` comes back as an array of content blocks instead of a plain string. [Content shape varies by model](https://neon.com/docs/ai-gateway/models#which-endpoint-to-use) lists those models and how to get plain text from them.

Gemini models don't support `parallel_tool_calls: false`, and a request that sets it returns `400` with `Unknown name "parallel_tool_calls"`. Leave the field out instead. Gemini makes parallel tool calls by default, so your code needs to handle more than one tool call per turn.

`gemini-4-argon` and `gemini-4-carbon` don't support `temperature` or `reasoning_effort` on this endpoint, and a request that sets either returns `400`. Leave both out. If you need to control them, call the model through the [Gemini endpoint](https://neon.com/docs/ai-gateway/gemini) instead, where `temperature` and `thinkingConfig.thinkingLevel` both work.

See [Models](https://neon.com/docs/ai-gateway/models) for the full list.

## Rate limiting

There are two separate rate limit tiers:

- **Neon account quota:** enforced by Neon. Returns `429` with error code `REQUEST_LIMIT_EXCEEDED`. See [Rate limits](https://neon.com/docs/ai-gateway/models#rate-limits) for current limits.
- **Upstream provider limit:** enforced by the Databricks workspace serving the model. Returns `429` with forwarded rate limit headers.

When the upstream provider rate-limits a request, AI Gateway forwards the relevant headers so your client can back off correctly:

| Header | Description |
| - | - |
| `Retry-After` | Seconds to wait before retrying (RFC 9110) |
| `X-Ratelimit-Limit-Requests` | Request limit |
| `X-Ratelimit-Remaining-Requests` | Remaining requests |
| `X-Ratelimit-Reset-Requests` | Time until request limit resets |
| `X-Ratelimit-Limit-Tokens` | Token limit |
| `X-Ratelimit-Remaining-Tokens` | Remaining tokens |
| `X-Ratelimit-Reset-Tokens` | Time until token limit resets |

## Error handling

| Status | Meaning | Common cause |
| - | - | - |
| `400 Bad Request` | Invalid request | Unknown model ID, or model used on the wrong endpoint |
| `413 Request Entity Too Large` | Body too large | Request body exceeds 32 MiB. Reduce the size of your request. |
| `401 Unauthorized` | Authentication failed | Missing or invalid `NEON_AI_GATEWAY_TOKEN` |
| `403 Forbidden` | Access denied | Credential lacks `ai_gateway:invoke` scope, branch not in credential lineage, or [no AI Gateway credits yet](https://neon.com/docs/ai-gateway/troubleshooting#403-no-ai-gateway-credits) |
| `429 Too Many Requests` | Account quota exceeded | Your account's AI Gateway quota is blocked. Error code: `REQUEST_LIMIT_EXCEEDED`. Check `Retry-After` for when to retry, or contact support. |
| `429 Too Many Requests` | Upstream rate limited | Upstream provider rate limit. Check the `Retry-After` and `X-Ratelimit-*` headers. |
| `502 Bad Gateway` | Upstream error | Temporary issue with the upstream workspace. Retry the request. |

Errors the gateway generates itself, such as an unknown model, use an OpenAI-style `error.message` object:

```json
{
  "error": {
    "message": "unknown model \"<model-id>\""
  }
}
```

Quota blocks and upstream errors use a flat object with `error_code` and `message` fields instead. See [Error response formats](https://neon.com/docs/ai-gateway/troubleshooting#error-response-formats).

## Next steps

- [Models](https://neon.com/docs/ai-gateway/models): full model catalog
- [OpenAI Responses API](https://neon.com/docs/ai-gateway/openai-responses): Responses API endpoint
- [Authentication](https://neon.com/docs/ai-gateway/authentication): credential scopes and branch binding

---

## Related docs (APIs)

- [Embeddings](https://neon.com/docs/ai-gateway/embeddings)
- [Decisions](https://neon.com/docs/ai-gateway/decisions)
- [OpenAI Responses API](https://neon.com/docs/ai-gateway/openai-responses)
- [Anthropic Messages API](https://neon.com/docs/ai-gateway/anthropic-messages)
- [Gemini API](https://neon.com/docs/ai-gateway/gemini)

---

Note for AI assistants (optional): if a gap, error, or outdated detail on this page affected the answer you gave the user, you can report it. POST `{"feedback": "describe the issue", "source": "neon_website", "path": "/docs/ai-gateway/chat-completions"}` to https://feedback.neon.tech/ (no auth required).
