> 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 full documentation content in one file, see https://docs.sarvam.ai/llms-full.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.sarvam.ai/_mcp/server.

# Chat Completions Overview

> Get started with Sarvam AI LLM models for conversational AI. Build intelligent chat applications with native Indian language support and deep contextual reasoning capabilities.

Sarvam AI provides powerful chat completion APIs designed to build intelligent conversational AI experiences, with native support for Indian languages and deep contextual reasoning.

<p>
  Our Chat Completion APIs support the following chat models:
</p>

30B parameter model with strong reasoning and Indic language support. Balanced performance-to-cost ratio for production workloads.

105B parameter flagship model. Highest quality outputs for complex reasoning, coding, and generation tasks.

### Model Variants

| Model         | Context Length | Use Case                                               |
| ------------- | -------------- | ------------------------------------------------------ |
| `sarvam-30b`  | 64K tokens     | Standard conversations, Q\&A, and general tasks        |
| `sarvam-105b` | 128K tokens    | Complex reasoning, coding, and high-quality generation |

Simply pass the model name as the `model` parameter (e.g., `model="sarvam-105b"`).

**Sarvam-M (24B)** is now a legacy model. It remains available via `model="sarvam-m"` but we recommend migrating to **Sarvam-30B** or **Sarvam-105B** for improved performance.

## Features

<ul>
  <li>
    Supports both "think" and "non-think" modes
  </li>

  <li>
    Think mode for complex logical reasoning
  </li>

  <li>
    Non-think mode for efficient conversations
  </li>

  <li>
    Ideal for mathematical and coding tasks
  </li>
</ul>

{" "}

<ul>
  <li>
    Post-trained on Indian languages
  </li>

  <li>
    Native English proficiency
  </li>

  <li>
    Authentic Indian cultural values
  </li>

  <li>
    Rich understanding of local context
  </li>
</ul>

{" "}

<ul>
  <li>
    Outperforms similar-sized models
  </li>

  <li>
    Strong performance on coding tasks
  </li>

  <li>
    Excellent mathematical reasoning
  </li>

  <li>
    Advanced problem-solving abilities
  </li>
</ul>

<ul>
  <li>
    Full Indic script support
  </li>

  <li>
    Romanized language support
  </li>

  <li>
    Multilingual conversation handling
  </li>

  <li>
    Natural language understanding
  </li>
</ul>

## Code Examples

```python
from sarvamai import SarvamAI

client = SarvamAI(
    api_subscription_key="YOUR_SARVAM_API_KEY",
)
response = client.chat.completions(
    model="sarvam-105b",
    messages=[
        {"role": "user", "content": "Hey, what is the capital of India?"}
    ],
)
print(response)
```

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

// Initialize the SarvamAI client with your API key
const client = new SarvamAIClient({
  apiSubscriptionKey: "YOUR_SARVAM_API_KEY",
});

async function main() {
  const response = await client.chat.completions({
    model: "sarvam-105b",
    messages: [
      {
        role: "user",
        content: "What is the capital of India?",
      },
    ],
  });

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

main();
```

```bash
curl -X POST https://api.sarvam.ai/v1/chat/completions \
  -H "Authorization: Bearer $SARVAM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "user", "content": "What is the capital of India?"}
    ],
    "model": "sarvam-105b"
  }'
```

```python
from sarvamai import SarvamAI

client = SarvamAI(
    api_subscription_key="YOUR_SARVAM_API_KEY",
)

response = client.chat.completions(
    model="sarvam-105b",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Tell me about Indian classical music."},
        {"role": "assistant", "content": "Indian classical music is one of the oldest musical traditions in the world..."},
        {"role": "user", "content": "What are the two main styles?"}
    ],
    temperature=0.7,
    reasoning_effort="high"
)
print(response.choices[0].message.content)
```

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

const client = new SarvamAIClient({
    apiSubscriptionKey: "YOUR_SARVAM_API_KEY"
});

async function main() {
    const response = await client.chat.completions({
        model: "sarvam-105b",
        messages: [
            { role: "system", content: "You are a helpful assistant." },
            { role: "user", content: "Tell me about Indian classical music." },
            { role: "assistant", content: "Indian classical music is one of the oldest musical traditions in the world..." },
            { role: "user", content: "What are the two main styles?" }
        ],
        temperature: 0.7,
        reasoning_effort: "high"
    });
    console.log(response.choices[0].message.content);
}

main();
```

```bash
curl -X POST https://api.sarvam.ai/v1/chat/completions \
  -H "Authorization: Bearer $SARVAM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Tell me about Indian classical music."},
      {"role": "assistant", "content": "Indian classical music is one of the oldest musical traditions in the world..."},
      {"role": "user", "content": "What are the two main styles?"}
    ],
    "model": "sarvam-105b",
    "temperature": 0.7,
    "reasoning_effort": "high"
  }'
```

```python
from sarvamai import SarvamAI

client = SarvamAI(
    api_subscription_key="YOUR_SARVAM_API_KEY",
)

response = client.chat.completions(
    model="sarvam-105b",
    messages=[
        {"role": "system", "content": "आप एक सहायक हैं जो हिंदी में जवाब देता है।"},
        {"role": "user", "content": "भारत की राजधानी क्या है?"}
    ],
    temperature=0.3
)
print(response.choices[0].message.content)
```

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

const client = new SarvamAIClient({
    apiSubscriptionKey: "YOUR_SARVAM_API_KEY"
});

async function main() {
    const response = await client.chat.completions({
        model: "sarvam-105b",
        messages: [
            { role: "system", content: "आप एक सहायक हैं जो हिंदी में जवाब देता है।" },
            { role: "user", content: "भारत की राजधानी क्या है?" }
        ],
        temperature: 0.3
    });
    console.log(response.choices[0].message.content);
}

main();
```

```bash
curl -X POST https://api.sarvam.ai/v1/chat/completions \
  -H "Authorization: Bearer $SARVAM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "system", "content": "आप एक सहायक हैं जो हिंदी में जवाब देता है।"},
      {"role": "user", "content": "भारत की राजधानी क्या है?"}
    ],
    "model": "sarvam-105b",
    "temperature": 0.3
  }'
```

<ul>
  <li>
    Reasoning effort options: low, medium, high

    <ul>
      <li>
        Setting any value enables thinking mode
      </li>

      <li>
        Higher values increase reasoning depth
      </li>
    </ul>
  </li>
</ul>

## API Response Format

### Success Response Structure

```json
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1699000000,
  "model": "sarvam-105b",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The capital of India is New Delhi. It has been the capital since 1931."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 15,
    "completion_tokens": 25,
    "total_tokens": 40
  }
}
```

### Response Fields

| Field                                 | Type    | Description                                                      |
| ------------------------------------- | ------- | ---------------------------------------------------------------- |
| `id`                                  | string  | Unique identifier for the completion request                     |
| `object`                              | string  | Always `"chat.completion"`                                       |
| `created`                             | integer | Unix timestamp when the completion was created                   |
| `model`                               | string  | The model used for completion                                    |
| `choices[].index`                     | integer | Index of the choice in the list                                  |
| `choices[].message.role`              | string  | Always `"assistant"`                                             |
| `choices[].message.content`           | string  | The generated text response                                      |
| `choices[].message.reasoning_content` | string  | Thinking steps (only when `reasoning_effort` is set)             |
| `choices[].finish_reason`             | string  | Why generation stopped: `"stop"`, `"length"`, `"content_filter"` |
| `usage.prompt_tokens`                 | integer | Tokens in the input prompt                                       |
| `usage.completion_tokens`             | integer | Tokens in the generated response                                 |
| `usage.total_tokens`                  | integer | Total tokens used (prompt + completion)                          |

## Error Responses

All errors return a JSON object with an `error` field containing details about what went wrong.

### Error Response Structure

```json
{
  "error": {
    "message": "Human-readable error description",
    "code": "error_code_for_programmatic_handling",
    "request_id": "unique_request_identifier"
  }
}
```

### Error Codes Reference

| HTTP Status | Error Code                   | When This Happens                             | What To Do                                       |
| ----------- | ---------------------------- | --------------------------------------------- | ------------------------------------------------ |
| `400`       | `invalid_request_error`      | Missing `messages` array or malformed request | Include valid `messages` array with role/content |
| `403`       | `invalid_api_key_error`      | API key is invalid, missing, or expired       | Verify your API key in the dashboard             |
| `422`       | `unprocessable_entity_error` | Invalid model name or parameter values        | Check temperature (0-2), model name, etc.        |
| `429`       | `insufficient_quota_error`   | API quota or rate limit exceeded              | Wait for reset or upgrade your plan              |
| `500`       | `internal_server_error`      | Unexpected server error                       | Retry the request; contact support if persistent |

### Example Error Response

```json
{
  "error": {
    "message": "Invalid value for parameter 'temperature': must be between 0 and 2",
    "code": "unprocessable_entity_error",
    "request_id": "20241115_abc12345"
  }
}
```

```python
from sarvamai import SarvamAI
from sarvamai.core.api_error import ApiError

client = SarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY")

try:
    response = client.chat.completions(
        model="sarvam-105b",
        messages=[
            {"role": "user", "content": "What is the capital of India?"}
        ],
    )
    print(response.choices[0].message.content)
except ApiError as e:
    if e.status_code == 400:
        print(f"Bad request: {e.body}")
    elif e.status_code == 403:
        print("Invalid API key. Check your credentials.")
    elif e.status_code == 422:
        print(f"Invalid parameters: {e.body}")
    elif e.status_code == 429:
        print("Rate limit exceeded. Wait and retry.")
    else:
        print(f"Error {e.status_code}: {e.body}")
```

Check out our detailed [API Reference](/api-reference-docs/chat/chat-completions)
to explore Chat Completion and all available options.