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

# Chat Completions

> Use OpenAI-compatible Chat Completions with any LLM provider through Prisma AIRS AI Gateway.

<Info>
  Available on all AI Gateway plans.
</Info>

The [Chat Completions](https://platform.openai.com/docs/api-reference/chat) API is the most widely adopted format for LLM interaction. The AI Gateway makes it work with **every provider** — send the same `POST /v1/chat/completions` request to OpenAI, Anthropic, Gemini, Bedrock, or any of the 3,000+ supported models.

## Quick Start

<CodeGroup>
  ```sh cURL theme={"system"}
  curl https://aigw.portkey.ai/v1/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $PORTKEY_API_KEY" \
    -d '{
      "model": "@openai-provider/gpt-4o",
      "messages": [{"role": "user", "content": "Explain quantum computing in simple terms"}]
    }'
  ```
</CodeGroup>

<Info>
  Switch `model` to use any provider — `@anthropic-provider/claude-sonnet-4-5-20250514`, `@google-provider/gemini-2.0-flash`, or any of the 3,000+ supported models.
</Info>

### Using the OpenAI SDK

Point the OpenAI SDK at the AI Gateway's base URL and all Chat Completions methods work unchanged:

<CodeGroup>
  ```python OpenAI Python SDK theme={"system"}
  from openai import OpenAI

  client = OpenAI(
      api_key="PORTKEY_API_KEY",
      base_url="https://aigw.portkey.ai/v1"
  )

  response = client.chat.completions.create(
      model="@openai-provider/gpt-4o",
      messages=[{"role": "user", "content": "Explain quantum computing in simple terms"}]
  )

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

  ```javascript OpenAI Node SDK theme={"system"}
  import OpenAI from 'openai';

  const client = new OpenAI({
      apiKey: "PORTKEY_API_KEY",
      baseURL: "https://aigw.portkey.ai/v1"
  });

  const response = await client.chat.completions.create({
      model: "@openai-provider/gpt-4o",
      messages: [{ role: "user", content: "Explain quantum computing in simple terms" }]
  });

  console.log(response.choices[0].message.content);
  ```
</CodeGroup>

## System Messages

Set a system prompt using the `system` role in the messages array:

<CodeGroup>
  ```sh cURL theme={"system"}
  curl https://aigw.portkey.ai/v1/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $PORTKEY_API_KEY" \
    -d '{
      "model": "@openai-provider/gpt-4o",
      "messages": [
        {"role": "system", "content": "You are a pirate. Always respond in pirate speak."},
        {"role": "user", "content": "Say hello."}
      ]
    }'
  ```
</CodeGroup>

## Streaming

Stream responses token-by-token with `stream: true`.

<CodeGroup>
  ```sh cURL theme={"system"}
  curl https://aigw.portkey.ai/v1/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $PORTKEY_API_KEY" \
    -d '{
      "model": "@openai-provider/gpt-4o",
      "messages": [{"role": "user", "content": "Write a haiku about AI"}],
      "stream": true
    }'
  ```
</CodeGroup>

## Function Calling

Define tools with the `tools` parameter. Works across all providers that support function calling.

<CodeGroup>
  ```sh cURL theme={"system"}
  curl https://aigw.portkey.ai/v1/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $PORTKEY_API_KEY" \
    -d '{
      "model": "@openai-provider/gpt-4o",
      "messages": [{"role": "user", "content": "What'\''s the weather in San Francisco?"}],
      "tools": [{
        "type": "function",
        "function": {
          "name": "get_weather",
          "description": "Get current weather for a location",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {"type": "string", "description": "City name"}
            },
            "required": ["location"]
          }
        }
      }]
    }'
  ```
</CodeGroup>

### Function Call Results

Pass tool results back to continue the conversation:

## Vision

Send images in the `content` array using the `image_url` type. Works with all vision-capable models.

<CodeGroup>
  ```sh cURL theme={"system"}
  curl https://aigw.portkey.ai/v1/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $PORTKEY_API_KEY" \
    -d '{
      "model": "@openai-provider/gpt-4o",
      "messages": [{
        "role": "user",
        "content": [
          {"type": "text", "text": "Describe this image"},
          {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
        ]
      }]
    }'
  ```
</CodeGroup>

Base64-encoded images are also supported — pass a data URL as the `url` value:

```python Python theme={"system"}
{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,/9j/4AAQ..."}}
```

## Structured Output

### JSON Schema

Force the model to return structured JSON matching a specific schema:

### JSON Mode

For free-form JSON output without a strict schema:

## Multi-turn Conversations

Pass the full conversation history in the `messages` array:

<CodeGroup>
  ```sh cURL theme={"system"}
  curl https://aigw.portkey.ai/v1/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $PORTKEY_API_KEY" \
    -d '{
      "model": "@openai-provider/gpt-4o",
      "messages": [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "My name is Alice."},
        {"role": "assistant", "content": "Hello Alice! How can I help you?"},
        {"role": "user", "content": "What is my name?"}
      ]
    }'
  ```
</CodeGroup>

## Using with AI Gateway Features

Chat Completions works with all AI Gateway features:

* **[Configs](/docs/aigw/product/ai-gateway/configs)** -- Route, load balance, and set fallbacks
* **[Caching](/docs/aigw/product/ai-gateway/cache-simple-and-semantic)** -- Cache responses for faster, cheaper calls
* **[Guardrails](/docs/aigw/product/guardrails)** -- Add input/output guardrails
* **[Observability](/docs/aigw/product/observability)** -- Full logging and tracing

<CodeGroup>
  ```sh cURL theme={"system"}
  curl https://aigw.portkey.ai/v1/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $PORTKEY_API_KEY" \
    -H "x-portkey-config: pp-config-xxx" \
    -d '{
      "model": "gpt-4o",
      "messages": [{"role": "user", "content": "Hello!"}]
    }'
  ```
</CodeGroup>

## API Reference

* [Chat Completions](/docs/api-reference/inference-api/chat) -- `POST /v1/chat/completions`

<CardGroup cols={2}>
  <Card title="OpenAI Chat API Docs" icon="book" href="https://platform.openai.com/docs/api-reference/chat">
    OpenAI specification
  </Card>

  <Card title="API Reference" icon="code" href="/docs/api-reference/inference-api/chat">
    AI Gateway Chat Completions reference
  </Card>

  <Card title="Universal API" icon="arrows-rotate" href="/docs/aigw/product/ai-gateway/universal-api">
    All three API formats
  </Card>

  <Card title="Function Calling Guide" icon="wrench" href="/docs/aigw/product/ai-gateway/multimodal-capabilities/function-calling">
    Detailed function calling guide
  </Card>
</CardGroup>


## Related topics

- [Nscale (EU Sovereign)](/docs/aigw/integrations/llms/nscale.md)
- [Lepton AI](/docs/aigw/integrations/llms/lepton.md)
- [Text-to-Speech](/docs/aigw/product/ai-gateway/multimodal-capabilities/text-to-speech.md)
- [Create chat completion](/docs/aigw/api-reference/chat/create-chat-completion.md)
- [LibreChat](/docs/aigw/integrations/libraries/librechat.md)
