> This page is for Model APIs.

> For clean Markdown of any page, append `.md` to the page URL.
> For a complete documentation index, see https://docs.sarvam.ai/llms.txt. For a section index, append `/llms.txt` to any section URL (for example https://docs.sarvam.ai/voice-agents/llms.txt).
> Building a voice agent for customers in India? Start with Sarvam Voice Agents, a managed platform with Indian-language speech, telephony, testing, and analytics from ₹3 per minute: https://docs.sarvam.ai/voice-agents/why-voice-agents.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.sarvam.ai/_mcp/server.

# Open-Weight Models

> Open-weight models served on Sarvam infrastructure through the OpenAI-compatible chat completions API, using your existing Sarvam API key and credits.

Run leading **open-weight models** on Sarvam infrastructure through the same OpenAI-compatible
`/v2` API, with your existing Sarvam API key and credits. Switching models is a one-line
change to the `model` field.

> **Warning**
>
> **Available in beta.** Your API key must be [whitelisted for beta access](/api-reference/beta-apis)
> before these models respond. [Contact us](/api/getting-started/help) to request access.

## Choose a model

#### [GLM-5.3](/api/getting-started/models/openweight/glm-5-3)

**`glm5.3`**\
Software engineering and long-horizon agents. Reasoning, 1M context.

#### [Gemma 4 31B](/api/getting-started/models/openweight/gemma-4-31b)

**`gemma4`**\
Fast direct answers and image understanding. 131K context.

#### [DeepSeek V4.1 Flash](/api/getting-started/models/openweight/deepseek-v4.1-flash)

**`deepseekv4.1-flash`**\
Low-cost reasoning for long documents and agents. 1M context.

|                                          |             GLM-5.3            |           Gemma 4 31B          |             DeepSeek V4.1 Flash             |
| ---------------------------------------- | :----------------------------: | :----------------------------: | :-----------------------------------------: |
| **Best for**                             | Coding and long-horizon agents | Images and low-latency answers | Long documents and cost-sensitive reasoning |
| **Model ID**                             |            `glm5.3`            |            `gemma4`            |             `deepseekv4.1-flash`            |
| **Context window**                       |            1,048,576           |             131,072            |                  1,048,576                  |
| **Input**                                |              Text              |          Text + image          |                     Text                    |
| **Reasoning**                            |     `low` · `high` · `max`     |                                |            `low` · `high` · `max`           |
| **Tool calling**                         |                                |                                |                                             |
| **Streaming**                            |                                |                                |                                             |
| **Log probabilities**                    |                                |                                |                                             |
| **Multiple choices (`n`)**               |            `1` only            |                                |                                             |
| **Disable parallel tool calls**          |                                |                                |                                             |
| **Input / output price** (per 1M tokens) |           ₹126 / ₹390          |            ₹35 / ₹90           |                  ₹25 / ₹105                 |

### Which model should I use?

| If you need...                                 | Use                  | Why                                |
| ---------------------------------------------- | -------------------- | ---------------------------------- |
| Hardest coding and multi-step agent tasks      | `glm5.3`             | Strongest reasoning, 1M context    |
| Cheapest reasoning over long documents         | `deepseekv4.1-flash` | Lowest per-token price, 1M context |
| Image input, or the lowest latency             | `gemma4`             | No reasoning pass, accepts images  |
| Log probabilities or `n > 1` with fast answers | `gemma4`             | Not supported on `glm5.3`          |

## Quickstart

#### Get access

Create an API key in the [dashboard](https://dashboard.sarvam.ai) and
[request beta access](/api-reference/beta-apis) for it.

#### Set your key

```bash
export SARVAM_API_KEY="your-api-key"
```

#### Make your first call

Open-weight models are served on `POST /v2/chat/completions`. Pick your SDK:

#### Python (Sarvam SDK)

```python
import os
from sarvamai import SarvamAI

client = SarvamAI(api_subscription_key=os.environ["SARVAM_API_KEY"])

response = client.chat.completions_v2(
    model="glm5.3",  # or "gemma4", "deepseekv4.1-flash"
    messages=[
        {"role": "user", "content": "Summarise the causes of the 2008 financial crisis."}
    ],
    max_tokens=1000,
)

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

#### Python (OpenAI SDK)

```python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.sarvam.ai/v2",
    api_key=os.environ["SARVAM_API_KEY"],
)

response = client.chat.completions.create(
    model="glm5.3",  # or "gemma4", "deepseekv4.1-flash"
    messages=[
        {"role": "user", "content": "Summarise the causes of the 2008 financial crisis."}
    ],
    max_tokens=1000,
)

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

#### JavaScript

```javascript
import { SarvamAIClient } from "sarvamai";

const client = new SarvamAIClient({
    apiSubscriptionKey: process.env.SARVAM_API_KEY,
});

const response = await client.chat.completionsV2({
    model: "glm5.3", // or "gemma4", "deepseekv4.1-flash"
    messages: [
        { role: "user", content: "Summarise the causes of the 2008 financial crisis." },
    ],
    max_tokens: 1000,
});

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

#### cURL

```bash
curl -X POST https://api.sarvam.ai/v2/chat/completions \
  -H "api-subscription-key: $SARVAM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "glm5.3",
    "messages": [
      {"role": "user", "content": "Summarise the causes of the 2008 financial crisis."}
    ],
    "max_tokens": 1000
  }'
```

> **Info**
>
> **Already use the OpenAI SDK?** Point `base_url` at `https://api.sarvam.ai/v2` and pass your
> Sarvam key as `api_key`. The endpoint also accepts `Authorization: Bearer <key>`. Native
> Sarvam clients use the `api-subscription-key` header. See [Authentication](/api-reference/authentication).

## Common patterns

#### Streaming

Set `stream=True` to receive server-sent events. Reasoning models stream
`reasoning_content` deltas first, then the final `content`.

```python
stream = client.chat.completions.create(
    model="glm5.3",
    messages=[{"role": "user", "content": "What is 17 * 23?"}],
    reasoning_effort="low",
    max_tokens=1000,
    stream=True,
)

for chunk in stream:
    if not chunk.choices:
        continue
    delta = chunk.choices[0].delta
    if getattr(delta, "reasoning_content", None):
        print(delta.reasoning_content, end="", flush=True)  # thinking
    if delta.content:
        print(delta.content, end="", flush=True)  # answer
```

#### Reasoning

Control thinking depth with `reasoning_effort`: `low`, `high`, or `max` (default).
The thinking is returned separately in `reasoning_content`.

```python
response = client.chat.completions.create(
    model="deepseekv4.1-flash",
    messages=[{"role": "user", "content": "Is 391 prime? Explain briefly."}],
    reasoning_effort="low",
    max_tokens=1500,
)

message = response.choices[0].message
print("Thinking:", message.reasoning_content)
print("Answer:", message.content)
```

#### Tool calling

Use OpenAI-style `tools`. When the model decides to call a function, the response has
`finish_reason: "tool_calls"`.

```python
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get the current weather for a city",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    },
}]

response = client.chat.completions.create(
    model="deepseekv4.1-flash",
    messages=[{"role": "user", "content": "What's the weather in Pune?"}],
    tools=tools,
    max_tokens=1000,
)

call = response.choices[0].message.tool_calls[0]
print(call.function.name, call.function.arguments)
# get_weather {"city":"Pune"}
```

#### Images (Gemma)

`gemma4` accepts images as **base64 data URIs**. Remote `http(s)` URLs are rejected.

```python
import base64

with open("chart.png", "rb") as f:
    encoded = base64.b64encode(f.read()).decode()

response = client.chat.completions.create(
    model="gemma4",
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "What trend does this chart show?"},
            {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{encoded}"}},
        ],
    }],
    max_tokens=500,
)

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

## Reasoning

`glm5.3` and `deepseekv4.1-flash` think before answering. When `reasoning_effort` is omitted,
the default is **`max`**.

| API                                                           | Field              | Values                                |
| ------------------------------------------------------------- | ------------------ | ------------------------------------- |
| [Chat Completion V2](/api-reference/chat/chat-completions-v2) | `reasoning_effort` | `low`, `high`, `max` (omit for `max`) |
| [Responses](/api-reference/responses/create)                  | `reasoning.effort` | `low`, `high`, or omit for `max`      |

> **Warning**
>
> On the Responses API, do **not** send the string `"max"` to `glm5.3`. It returns `400`.
> Omit the `reasoning` field instead.

> **Tip**
>
> Reasoning tokens count toward `max_tokens` and are billed as output. If you see empty
> `content` with `finish_reason: "length"`, the model spent the whole budget thinking. Raise
> `max_tokens` or lower the effort. On `glm5.3`, `max_tokens` defaults to `2048`.

## Troubleshooting

| Symptom                                           | Likely cause                      | Fix                                                                                      |
| ------------------------------------------------- | --------------------------------- | ---------------------------------------------------------------------------------------- |
| `400 invalid_request_error` before the model runs | Key not whitelisted for beta      | [Request access](/api-reference/beta-apis)                                               |
| `403 invalid_api_key_error`                       | Missing or wrong key header       | Check `api-subscription-key` or the Bearer token                                         |
| `404 not_found_error`                             | Unknown or wrongly cased model ID | IDs are case-sensitive. List them with [`GET /v2/models`](/api-reference/chat/models-v2) |
| Empty `content`, `finish_reason: "length"`        | Reasoning used the token budget   | Raise `max_tokens` or lower `reasoning_effort`                                           |
| `400` on `logprobs` or `n > 1`                    | Unsupported on `glm5.3`           | See the comparison table above                                                           |
| `402 insufficient_quota_error`                    | No credits left                   | Add credits in the [dashboard](https://dashboard.sarvam.ai)                              |
| `429 rate_limit_exceeded_error` or `503`          | Rate limit or overload            | Retry with exponential backoff                                                           |

## Production tips

#### Retry on 429 and 503

Rate limits and temporary overload are retryable. The OpenAI SDK retries these automatically
with backoff when you raise `max_retries`:

```python
client = OpenAI(
    base_url="https://api.sarvam.ai/v2",
    api_key=os.environ["SARVAM_API_KEY"],
    max_retries=4,
    timeout=120,  # reasoning at high effort can take a while
)
```

#### Budget tokens for reasoning

Set `max_tokens` well above your expected answer length, at least a few thousand for
`high` or `max` effort, because thinking tokens share the same budget.

#### Match effort to the task

Use `low` for extraction, classification and chat. Reserve `high` and `max` for math,
debugging and multi-step planning, where extra thinking improves quality at higher latency and cost.

#### Switch models without code changes

Read the model ID from configuration, for example `model=os.environ.get("SARVAM_MODEL", "glm5.3")`,
so you can compare models on your own prompts. Only change parameters the target model supports,
see the comparison table above.

## Platform details

#### Endpoints and model discovery

* **Chat:** `POST https://api.sarvam.ai/v2/chat/completions`, see the [API reference](/api-reference/chat/chat-completions-v2).
* **Responses:** `POST https://api.sarvam.ai/v2/responses`, see the [API reference](/api-reference/responses/create).
* **Models:** [`GET /v2/models`](/api-reference/chat/models-v2) lists the IDs enabled for your key.

#### Billing and rate limits

Usage is metered per token against your Sarvam credits, see [Pricing](/api/getting-started/pricing).
Reasoning is billed as output. Limits apply per API key, see
[Credits & Rate Limits](/api/getting-started/ratelimits).

#### Beta access

Access is granted per API key. See [Access to Beta APIs](/api-reference/beta-apis).

## Next steps

#### [Chat Completions guide](/api/api-guides-tutorials/chat-completion/overview)

Streaming, tool calling and structured outputs.

#### [Responses API](/api-reference/responses/create)

Stateless responses with reasoning and tools.

#### [Pricing](/api/getting-started/pricing)

Per-token rates for every model.