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

# Chat Completions

> Create chat completions with any supported model

## Request

Create a chat completion using any supported model.

### Headers

| Header          | Value                 | Required |
| --------------- | --------------------- | -------- |
| `Authorization` | `Bearer YOUR_API_KEY` | Yes      |
| `Content-Type`  | `application/json`    | Yes      |

### Body Parameters

<ParamField body="model" type="string" required>
  The model to use (e.g., `gpt-5.1`, `claude-sonnet-4.5`, `gpt-4.2`, `o3`)
</ParamField>

<ParamField body="messages" type="array" required>
  Array of message objects with `role` and `content`
</ParamField>

<ParamField body="temperature" type="number" default="1">
  Sampling temperature between 0 and 2
</ParamField>

<ParamField body="max_tokens" type="integer">
  Maximum number of tokens to generate
</ParamField>

<ParamField body="stream" type="boolean" default="false">
  Whether to stream responses
</ParamField>

<ParamField body="top_p" type="number" default="1">
  Nucleus sampling parameter
</ParamField>

<ParamField body="frequency_penalty" type="number" default="0">
  Penalize frequent tokens (-2.0 to 2.0)
</ParamField>

<ParamField body="presence_penalty" type="number" default="0">
  Penalize new tokens (-2.0 to 2.0)
</ParamField>

<ParamField body="stop" type="string or array">
  Stop sequences
</ParamField>

<ParamField body="functions" type="array">
  Function definitions for function calling
</ParamField>

<ParamField body="function_call" type="string or object">
  Controls function calling behavior
</ParamField>

## Response

### Response Fields

<ResponseField name="id" type="string">
  Unique completion ID
</ResponseField>

<ResponseField name="object" type="string">
  Object type (always `chat.completion`)
</ResponseField>

<ResponseField name="created" type="integer">
  Unix timestamp
</ResponseField>

<ResponseField name="model" type="string">
  Model used for completion
</ResponseField>

<ResponseField name="choices" type="array">
  Array of completion choices

  <Expandable title="choice object">
    <ResponseField name="index" type="integer">
      Choice index
    </ResponseField>

    <ResponseField name="message" type="object">
      The generated message

      <Expandable title="message object">
        <ResponseField name="role" type="string">
          Message role (always `assistant`)
        </ResponseField>

        <ResponseField name="content" type="string">
          Message content
        </ResponseField>

        <ResponseField name="function_call" type="object">
          Function call if applicable
        </ResponseField>
      </Expandable>
    </ResponseField>

    <ResponseField name="finish_reason" type="string">
      Reason completion finished (`stop`, `length`, `function_call`, etc.)
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="usage" type="object">
  Token usage information

  <Expandable title="usage object">
    <ResponseField name="prompt_tokens" type="integer">
      Tokens in prompt
    </ResponseField>

    <ResponseField name="completion_tokens" type="integer">
      Tokens in completion
    </ResponseField>

    <ResponseField name="total_tokens" type="integer">
      Total tokens used
    </ResponseField>
  </Expandable>
</ResponseField>

## Examples

<CodeGroup>
  ```bash cURL theme={null}
  curl https://api.savegate.ai/v1/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer sk-savegate-xxxxxxxxxxxxx" \
    -d '{
      "model": "gpt-5.1",
      "messages": [
        {
          "role": "system",
          "content": "You are a helpful assistant."
        },
        {
          "role": "user",
          "content": "What is the capital of France?"
        }
      ],
      "temperature": 0.7
    }'
  ```

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

  client = OpenAI(
      api_key="sk-savegate-xxxxxxxxxxxxx",
      base_url="https://api.savegate.ai/v1"
  )

  response = client.chat.completions.create(
      model="gpt-5.1",
      messages=[
          {"role": "system", "content": "You are a helpful assistant."},
          {"role": "user", "content": "What is the capital of France?"}
      ],
      temperature=0.7
  )

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

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

  const client = new OpenAI({
    apiKey: 'sk-savegate-xxxxxxxxxxxxx',
    baseURL: 'https://api.savegate.ai/v1'
  });

  const response = await client.chat.completions.create({
    model: 'gpt-5.1',
    messages: [
      { role: 'system', content: 'You are a helpful assistant.' },
      { role: 'user', content: 'What is the capital of France?' }
    ],
    temperature: 0.7
  });

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

### Response Example

```json theme={null}
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "gpt-5.1",
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "The capital of France is Paris."
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 20,
    "completion_tokens": 8,
    "total_tokens": 28
  }
}
```

## Streaming Example

<CodeGroup>
  ```python Python theme={null}
  response = client.chat.completions.create(
      model="gpt-5.1",
      messages=[{"role": "user", "content": "Tell me a story"}],
      stream=True
  )

  for chunk in response:
      if chunk.choices[0].delta.content:
          print(chunk.choices[0].delta.content, end="")
  ```

  ```javascript Node.js theme={null}
  const stream = await client.chat.completions.create({
    model: 'gpt-5.1',
    messages: [{ role: 'user', content: 'Tell me a story' }],
    stream: true
  });

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

## Function Calling Example

<CodeGroup>
  ```python Python theme={null}
  functions = [
      {
          "name": "get_weather",
          "description": "Get current weather",
          "parameters": {
              "type": "object",
              "properties": {
                  "location": {
                      "type": "string",
                      "description": "City and state, e.g. San Francisco, CA"
                  },
                  "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
              },
              "required": ["location"]
          }
      }
  ]

  response = client.chat.completions.create(
      model="gpt-5.1",
      messages=[{"role": "user", "content": "What's the weather in Boston?"}],
      functions=functions,
      function_call="auto"
  )

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

  ```javascript Node.js theme={null}
  const functions = [
    {
      name: 'get_weather',
      description: 'Get current weather',
      parameters: {
        type: 'object',
        properties: {
          location: {
            type: 'string',
            description: 'City and state, e.g. San Francisco, CA'
          },
          unit: { type: 'string', enum: ['celsius', 'fahrenheit'] }
        },
        required: ['location']
      }
    }
  ];

  const response = await client.chat.completions.create({
    model: 'gpt-5.1',
    messages: [{ role: 'user', content: "What's the weather in Boston?" }],
    functions: functions,
    function_call: 'auto'
  });

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