> 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 Completion

POST https://api.sarvam.ai/v1/chat/completions
Content-Type: application/json

Creates a model response for the given chat conversation. This endpoint serves **only** `sarvam-105b` and `sarvam-105b-conversations`.

Reference: https://docs.sarvam.ai/api-reference/chat/chat-completions

## Authentication

- `Authorization` header (bearer token, required)
- `api-subscription-key` header (required)

## Request

### Headers

- `api-subscription-key` (string, optional, nullable) — API subscription key in sk_xxx format. [Steps to get your key](https://docs.sarvam.ai/api-reference-docs/authentication)

### Body (application/json)

- `messages` (list of object or object or object or object or object, required) — A list of messages comprising the conversation so far.
  - ChatCompletionRequestSystemMessage
    - `content` (string, required) — The contents of the system message.
    - `role` ("system", required) — The role of the messages author, in this case `system`.
  - ChatCompletionRequestUserMessage
    - `role` ("user", required) — The role of the messages author, in this case `user`.
    - `content` (string or list of object, required) — The contents of the user message.
  - ChatCompletionRequestAssistantMessage
    - `role` ("assistant", required) — The role of the messages author, in this case `assistant`.
    - `content` (string, optional, nullable) — The contents of the assistant message
    - `tool_calls` (list of object, optional, nullable) — The tool calls generated by the model.
      - `id` (string, required) — The ID of the tool call.
      - `type` ("function", required) — The type of the tool call, currently only `function`.
      - `function` (object, required)
        - `name` (string, required) — The name of the function to call.
        - `arguments` (string, required) — The arguments to call the function with, as a JSON string.
  - ChatCompletionRequestToolMessage
    - `role` ("tool", required) — The role of the messages author, in this case `tool`.
    - `content` (string, required) — The contents of the tool message.
    - `tool_call_id` (string, required) — Tool call that this message is responding to.
  - ChatCompletionRequestGenericMessage
    - `role` (string, required) — The role of the messages author.
    - `content` (string or list of object, required) — The contents of the message.
    - `name` (string, optional, nullable) — An optional name for the participant.
- `model` (enum, required) — Chat model ID. Use `sarvam-105b` (128K context) for complex reasoning and agentic tasks, or `sarvam-105b-conversations` for real-time conversational and voice-agent workloads.
  - Allowed values: `sarvam-105b`, `sarvam-105b-conversations`
- `temperature` (double, optional, nullable, default: 0.2) — What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or `top_p` but not both.
- `top_p` (double, optional, nullable, default: 1) — An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or `temperature` but not both.
- `reasoning_effort` (enum, optional, nullable, default: medium) — The effort to use for reasoning. Can be disabled by explicitly setting to None. Default is 'medium'.
  - Allowed values: `low`, `medium`, `high`
- `max_tokens` (integer, optional, nullable, default: 2048) — The maximum number of tokens that can be generated in the chat completion.
- `stream` (boolean, optional, nullable, default: false) — If set to true, the model response data will be streamed to the client as it is generated using [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format).
- `stop` (string or list of string, optional, nullable) — Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.
- `n` (integer, optional, nullable, default: 1) — How many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices. Keep `n` as `1` to minimize costs.
- `seed` (integer, optional, nullable) — This feature is in Beta. If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same `seed` and parameters should return the same result. Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend.
- `frequency_penalty` (double, optional, nullable, default: 0) — Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
- `presence_penalty` (double, optional, nullable, default: 0) — Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
- `wiki_grounding` (boolean, optional, nullable, default: false) — If set to true, the model response will be wiki grounded.
- `tools` (list of object, optional, nullable) — A list of tools the model may call. Currently, only functions are supported as a tool.
  - `type` ("function", required) — The type of the tool. Currently, only `function` is supported.
  - `function` (object, required)
    - `name` (string, required) — The name of the function to be called.
    - `description` (string, optional, nullable) — A description of what the function does.
    - `parameters` (map from string to any, optional, nullable) — The parameters the function accepts, described as a JSON Schema object.
- `tool_choice` (enum or object, optional, nullable) — Controls which (if any) tool is called by the model.
  - ChatCompletionNamedToolChoice
    - `type` ("function", required) — The type of the tool. Currently, only `function` is supported.
    - `function` (object, required)
      - `name` (string, required) — The name of the function to call.
- `response_format` (object, optional, nullable) — An object specifying the format that the model must output. Setting `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which guarantees the model generates output matching the supplied JSON Schema. Setting `{ "type": "json_object" }` enables the older JSON mode, which guarantees valid JSON but not a specific schema.
  - `type`: `json_object` (ResponseFormatJsonObject)
  - `type`: `json_schema` (ResponseFormatJsonSchema)
    - `json_schema` (object, required) — Structured Outputs configuration options, including a JSON Schema.
      - `name` (string, required) — The name of the response format. Must contain only alphanumeric characters, underscores and dashes.
      - `schema` (map from string to any, required) — The schema for the response format, described as a JSON Schema object.
      - `description` (string, optional, nullable) — A description of what the response format is for, used by the model to determine how to respond in the format.
      - `strict` (boolean, optional, nullable, default: false) — Whether to enable strict schema adherence when generating the output.
  - `type`: `text` (ResponseFormatText)

## Response

### 200

Successful Response

- `id` (string, required) — A unique identifier for the chat completion.
- `choices` (list of object, required) — A list of chat completion choices. Can be more than one if `n` is greater than 1.
  - `finish_reason` (enum, required) — The reason the model stopped generating tokens. This will be `stop` if the model hit a natural stop point or a provided stop sequence, `length` if the maximum number of tokens specified in the request was reached, `content_filter` if content was omitted due to a flag from our content filters, `tool_calls` if the model called a tool, or `function_call` (deprecated) if the model called a function.
    - Allowed values: `stop`, `length`, `tool_calls`, `content_filter`, `function_call`
  - `index` (integer, required) — The index of the choice in the list of choices.
  - `message` (object, required)
    - `content` (string, required, nullable) — The contents of the message.
    - `role` (enum, required) — The role of the author of this message.
      - Allowed values: `assistant`
    - `refusal` (string, optional, nullable)
    - `reasoning_content` (string, optional, nullable) — The contents of the reasoning message.
    - `tool_calls` (list of object, optional, nullable) — The tool calls generated by the model.
      - `id` (string, required) — The ID of the tool call.
      - `type` ("function", required) — The type of the tool call, currently only `function`.
      - `function` (object, required)
        - `name` (string, required) — The name of the function to call.
        - `arguments` (string, required) — The arguments to call the function with, as a JSON string.
  - `logprobs` (map from string to any, optional, nullable)
- `created` (integer, required) — The Unix timestamp (in seconds) of when the chat completion was created.
- `model` (string, required) — The model used for the chat completion.
- `object` ("chat.completion", required) — The object type, which is always `chat.completion`.
- `service_tier` (string, optional, nullable)
- `system_fingerprint` (string, optional, nullable)
- `usage` (object, optional, nullable)
  - `completion_tokens` (integer, required) — Number of tokens in the generated completion.
  - `prompt_tokens` (integer, required) — Number of tokens in the prompt.
  - `total_tokens` (integer, required) — Total number of tokens used in the request (prompt + completion).
  - `completion_tokens_details` (map from string to any, optional, nullable)
  - `prompt_tokens_details` (map from string to any, optional, nullable)

## Examples

**Request**

```json
{
  "messages": [
    {
      "content": "Say hi in one word",
      "role": "user"
    }
  ],
  "model": "sarvam-105b"
}
```

**Response**

```json
{
  "id": "20250101_0a1b2c3d-1234-5678-9abc-def012345678",
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "नमस्ते! मैं आपकी कैसे मदद कर सकता हूँ?",
        "role": "assistant"
      },
      "logprobs": {}
    }
  ],
  "created": 1735689600,
  "model": "sarvam-105b",
  "object": "chat.completion",
  "service_tier": null,
  "system_fingerprint": null,
  "usage": {
    "completion_tokens": 18,
    "prompt_tokens": 12,
    "total_tokens": 30
  }
}
```

**SDK Code**

```typescript chat_completions_example
import { SarvamAIClient } from "sarvamai";

async function main() {
    const client = new SarvamAIClient();
    await client.chat.completions({
        messages: [
            {
                role: "user",
                content: "Say hi in one word",
            },
        ],
        model: "sarvam-105b",
    });
}
main();

```

```python chat_completions_example
from sarvamai import SarvamAI

client = SarvamAI()

client.chat.completions(
    messages=[
        {
            "content": "Say hi in one word",
            "role": "user"
        }
    ],
    model="sarvam-105b",
)

```

```go chat_completions_example
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://api.sarvam.ai/v1/chat/completions"

	payload := strings.NewReader("{\n  \"messages\": [\n    {\n      \"content\": \"Say hi in one word\",\n      \"role\": \"user\"\n    }\n  ],\n  \"model\": \"sarvam-105b\"\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Authorization", "Bearer <token>")
	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby chat_completions_example
require 'uri'
require 'net/http'

url = URI("https://api.sarvam.ai/v1/chat/completions")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"messages\": [\n    {\n      \"content\": \"Say hi in one word\",\n      \"role\": \"user\"\n    }\n  ],\n  \"model\": \"sarvam-105b\"\n}"

response = http.request(request)
puts response.read_body
```

```java chat_completions_example
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.sarvam.ai/v1/chat/completions")
  .header("Authorization", "Bearer <token>")
  .header("Content-Type", "application/json")
  .body("{\n  \"messages\": [\n    {\n      \"content\": \"Say hi in one word\",\n      \"role\": \"user\"\n    }\n  ],\n  \"model\": \"sarvam-105b\"\n}")
  .asString();
```

```php chat_completions_example
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.sarvam.ai/v1/chat/completions', [
  'body' => '{
  "messages": [
    {
      "content": "Say hi in one word",
      "role": "user"
    }
  ],
  "model": "sarvam-105b"
}',
  'headers' => [
    'Authorization' => 'Bearer <token>',
    'Content-Type' => 'application/json',
  ],
]);

echo $response->getBody();
```

```csharp chat_completions_example
using RestSharp;

var client = new RestClient("https://api.sarvam.ai/v1/chat/completions");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer <token>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"messages\": [\n    {\n      \"content\": \"Say hi in one word\",\n      \"role\": \"user\"\n    }\n  ],\n  \"model\": \"sarvam-105b\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift chat_completions_example
import Foundation

let headers = [
  "Authorization": "Bearer <token>",
  "Content-Type": "application/json"
]
let parameters = [
  "messages": [
    [
      "content": "Say hi in one word",
      "role": "user"
    ]
  ],
  "model": "sarvam-105b"
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://api.sarvam.ai/v1/chat/completions")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```