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

# Custom LLMs

> Use your own model to generate agent responses.

Connect an OpenAI-compatible Chat Completions endpoint when you [create](https://docs.cartesia.ai/api-reference/agents/create.md) or [update](https://docs.cartesia.ai/api-reference/agents/update.md) an agent. Your endpoint generates the agent's responses. Cartesia still runs speech recognition, speech synthesis, turn-taking, and tools.

```bash theme={null}
curl -X POST "https://api.cartesia.ai/v1/agents" \
  -H "X-API-Key: $CARTESIA_API_KEY" \
  -H "Cartesia-Version: 2026-08-14" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Acme Support",
    "config": {
      "model": {
        "id": "custom_llm",
        "custom_llm": {
          "url": "https://llm.example.com/v1",
          "model_id": "acme-support-v1",
          "authentication": {
            "mode": "bearer",
            "token": {
              "type": "secret",
              "secret_value": "YOUR_LLM_API_KEY",
              "secret_description": "LLM API key"
            }
          }
        }
      }
    }
  }'
```

To switch an existing agent, send the same `model` object in an [agent update](https://docs.cartesia.ai/api-reference/agents/update.md).

Set `url` to your API's **base URL**: `https://llm.example.com/v1` receives requests at `https://llm.example.com/v1/chat/completions`. Your server must be reachable over public HTTPS.

## Request and response format

Cartesia sends streaming `POST` requests with the agent's instructions and conversation history:

```json theme={null}
{
  "model": "acme-support-v1",
  "messages": [
    { "role": "system", "content": "You are Acme's support agent. Help callers with orders and returns. Be warm and concise." },
    { "role": "user", "content": "Where is my order?" }
  ],
  "stream": true,
  "stream_options": { "include_usage": true }
}
```

| Field | When Cartesia sends it |
| - | - |
| `model` | When `custom_llm.model_id` is set. |
| `stream_options.include_usage` | Always `true`. Returning usage is optional. |
| `tools` | When the agent has tools available. Uses the Chat Completions function-tool format. |
| `tool_choice` | Set to `"none"` when the agent must answer without calling more tools. Otherwise omitted. |
| `temperature` | When `config.model.temperature` is not `null`. |
| `max_tokens` | When `config.model.max_output_tokens` is not `null`. |
| `cartesia_extra_body` | When [call context](#call-context) is enabled. |

Retries of a response reuse its `Idempotency-Key` request header.

[Speculative generation](https://docs.cartesia.ai/agents/configuration.md#speculative-generation) is on by default: Cartesia can start a request before the caller finishes and discard the reply if they keep talking, so requests for one call can overlap. Set `config.speculative_generation` to `false` to disable it.

### Stream the response

Return `Content-Type: text/event-stream` with Chat Completions chunks separated by a blank line, followed by `data: [DONE]`. For example:

```text theme={null}
data: {"id":"chatcmpl-1","object":"chat.completion.chunk","created":1792152000,"model":"acme-support-v1","choices":[{"index":0,"delta":{"role":"assistant","content":"What is your order number?"},"finish_reason":null}]}

data: {"id":"chatcmpl-1","object":"chat.completion.chunk","created":1792152000,"model":"acme-support-v1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: {"id":"chatcmpl-1","object":"chat.completion.chunk","created":1792152000,"model":"acme-support-v1","choices":[],"usage":{"prompt_tokens":30,"completion_tokens":7,"total_tokens":37}}

data: [DONE]
```

Cartesia does not charge for tokens generated by your custom endpoint.

Return an uncompressed response of at most 256 KiB, including SSE framing. Cartesia does not follow redirects. This integration uses Chat Completions; a Responses-only endpoint is not compatible.

### Use tools

Your model must support function calling to use the agent's [tools](https://docs.cartesia.ai/agents/tools.md), including [system tools](https://docs.cartesia.ai/agents/system-tools.md). Return calls in `choices[0].delta.tool_calls`, using the names and parameter schemas from the request's `tools` array. Cartesia dispatches the calls and includes their results in subsequent LLM requests. A single conversation turn can therefore make several requests to your endpoint.

## Authentication

Set `config.model.custom_llm` to one of these configurations:

```json Bearer token theme={null}
{
  "url": "https://llm.example.com/v1",
  "model_id": "acme-support-v1",
  "authentication": {
    "mode": "bearer",
    "token": {
      "type": "secret",
      "secret_value": "YOUR_LLM_API_KEY",
      "secret_description": "LLM API key"
    }
  }
}
```

```json Basic auth theme={null}
{
  "url": "https://llm.example.com/v1",
  "model_id": "acme-support-v1",
  "authentication": {
    "mode": "basic_auth",
    "username": "acme",
    "password": {
      "type": "secret",
      "secret_value": "YOUR_LLM_PASSWORD",
      "secret_description": "LLM password"
    }
  }
}
```

```json API key header theme={null}
{
  "url": "https://llm.example.com/v1",
  "model_id": "acme-support-v1",
  "request_headers": {
    "api-key": {
      "type": "secret",
      "secret_value": "YOUR_LLM_API_KEY",
      "secret_description": "LLM API key"
    },
    "X-Tenant": "acme"
  }
}
```

Bearer and Basic authentication set the `Authorization` header. You can combine either with additional `request_headers`, but not with another `Authorization` header. For an endpoint without authentication, omit `authentication`.

## Call context

Set `config.model.custom_llm.send_call_context` to `true` to include your custom and available system [dynamic variables](https://docs.cartesia.ai/agents/dynamic-variables.md) in every LLM request:

```jsonc theme={null}
{
  "cartesia_extra_body": {
    "dynamic_variables": {
      "customer_name": "Jordan",
      "order_id": "ORD-1042",
      "system__conversation_id": "ac_abc123",
      "system__caller_id": "+14155550123",
      "system__agent_id": "agent_Fo7pKNBUwLZxrTd6jvhpaE",
      "system__time_utc": "2026-09-07T15:30:00Z"
      // ... additional system variables
    }
  }
}
```

This defaults to `false`; enable it only if your endpoint accepts `cartesia_extra_body`. Variables in the agent's instructions are substituted regardless of this setting.

## Update the endpoint or credentials

Sending `custom_llm` in an [agent update](https://docs.cartesia.ai/api-reference/agents/update.md) **replaces the entire object**: include every setting you want to keep. Omitting it preserves the existing configuration.

This update changes the model while keeping its stored bearer token:

```json theme={null}
{
  "config": {
    "model": {
      "custom_llm": {
        "url": "https://llm.example.com/v1",
        "model_id": "acme-support-v2",
        "authentication": {
          "mode": "bearer",
          "token": { "type": "secret", "secret_description": "LLM API key" }
        }
      }
    }
  }
}
```

The `{ "type": "secret" }` placeholder keeps the credential stored at that field. To rotate it, supply a new `secret_value`.

To switch back, set `model.id` to a [managed model ID](https://docs.cartesia.ai/api-reference/agents/models/list.md).

## Failures and fallback

If your endpoint and every fallback fail, or a failure comes after output starts, that reply stops and the call continues.

| Limit | Duration |
| - | - |
| First chunk, per attempt | 3 seconds |
| Between chunks | 6 seconds |
| Each response attempt | 60 seconds |

Any chunk meets these deadlines, including reasoning-only and empty ones; SSE comments don't. Once text or a tool call arrives, Cartesia stops retrying and falling back.

Before text or a tool call arrives, Cartesia retries timeouts, dropped connections, DNS failures, `429` and `5xx` responses, and streams that end without output. Each endpoint gets at most two attempts and each reply at most four, with fallback models tried before the retry. `400`, `401`, `403`, `404`, `413`, and `422` responses are not retried.

Custom LLMs have **no fallback by default**. To enable [fallback](https://docs.cartesia.ai/agents/models.md#fallback-models), set `config.model.fallback_strategy` with up to three [managed model IDs](https://docs.cartesia.ai/api-reference/agents/models/list.md), tried in order:

```json theme={null}
{
  "config": {
    "model": {
      "fallback_strategy": { "type": "explicit", "ids": ["gpt-5.4-mini", "claude-haiku-4.5"] }
    }
  }
}
```
