> 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 V1 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-v1 ## Authentication - `Authorization` header (bearer token, required) — Bearer authentication of the form `Bearer `, where token is your auth token. - `api-subscription-key` header (required) — API Key authentication via header ## 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) This endpoint expects a CreateChatCompletionRequest. - `messages` (list of ChatCompletionRequestMessage, required) — A list of messages comprising the conversation so far. - `model` (enum, required) — Chat model ID. Use `sarvam-105b` (128K context) for complex reasoning and agentic tasks, or `sarvam-105b-conversations` (32K context) for real-time conversational and voice-agent workloads. - Allowed values: `sarvam-105b`, `sarvam-105b-conversations` - `temperature` (double, optional, nullable, default: 0.2) — Sampling temperature between 0 and 2. Higher values such as 0.8 increase variation; lower values such as 0.2 make output more focused. Output is not bit-reproducible, even at `0`. Prefer changing either `temperature` or `top_p`, 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`, `high`, `max` - `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` (StopConfiguration, optional, nullable) — Up to 4 stop sequences (string or array). Generated text excludes the matched sequence. On `glm5.3`, more than four entries may return `503 model_overloaded` rather than `400`. - `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. - `tools` (list of ChatCompletionTool, optional, nullable) — A list of tools the model may call. Currently, only functions are supported as a tool. - `tool_choice` (ToolChoiceOption, optional, nullable) — Controls which (if any) tool is called by the model. - `response_format` (ResponseFormat, 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. ## Response ### 200 Successful Response - `id` (string, required) — A unique identifier for the chat completion. - `choices` (list of Choice, required) — A list of chat completion choices. Can be more than one if `n` is greater than 1. - `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` (CompletionUsage, optional, nullable) ## Errors ### 400 Bad Request Error Bad Request - `error` (ErrorDetails, required) — Error details ### 403 Forbidden Error Forbidden - `error` (ErrorDetails, required) — Error details ### 422 Unprocessable Entity Error Unprocessable Entity - `error` (ErrorDetails, required) — Error details ### 429 Too Many Requests Error Quota Exceeded - `error` (ErrorDetails, required) — Error details ### 500 Internal Server Error Internal Server Error - `error` (ErrorDetails, required) — Error details ## Types ### ChatCompletionRequestMessage ### StopConfiguration Up to 4 stop sequences (string or array). Generated text excludes the matched sequence. On `glm5.3`, more than four entries may return `503 model_overloaded` rather than `400`. ### ChatCompletionTool - `type` ("function", required) — The type of the tool. Currently, only `function` is supported. - `function` (FunctionDefinition, required) ### ToolChoiceOption ### ResponseFormat - `type`: `json_object` (ResponseFormatJsonObject) - `type`: `json_schema` (ResponseFormatJsonSchema) - `json_schema` (JsonSchemaDefinition, required) — Structured Outputs configuration options, including a JSON Schema. - `type`: `text` (ResponseFormatText) ### Choice - `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` (ChatCompletionResponseMessage, required) - `logprobs` (map from string to any, optional, nullable) — Token log probabilities when supported (`sarvam-105b`, `gemma4`). Always `null` on `glm5.3` and `deepseekv4-flash`, which don't support this field. ### CompletionUsage - `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` (CompletionTokensDetails, optional, nullable) - `prompt_tokens_details` (CompletionUsagePromptTokensDetails, optional, nullable) — `null` on a cache miss. On a hit, includes `cached_tokens`. Treat a missing object and `cached_tokens: 0` as different when computing cache-hit rates. ### ErrorDetails - `message` (string, required) — Message describing the error - `code` (enum, required) — Error code for the specific error that has occurred. Refer to the error code documentation for more details. - Allowed values: `invalid_request_error`, `internal_server_error`, `unprocessable_entity_error`, `insufficient_quota_error`, `invalid_api_key_error`, `authentication_error`, `rate_limit_exceeded_error`, `not_found_error` - `request_id` (string, optional, default: ) — Unique identifier for the request. Format: date_UUID4 ### ChatCompletionRequestSystemMessage - `content` (string, required) — The contents of the system message. - `role` ("system", required) — The role of the messages author, in this case `system`. ### ChatCompletionRequestDeveloperMessage - `content` (string, required) — The contents of the developer message. - `role` ("developer", required) — The role of the messages author, in this case `developer`. ### ChatCompletionRequestUserMessage - `role` ("user", required) — The role of the messages author, in this case `user`. - `content` (ChatCompletionRequestUserMessageContent, 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 ChatCompletionMessageToolCall, optional, nullable) — The tool calls generated by the model. ### 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` (ChatCompletionRequestGenericMessageContent, required) — The contents of the message. - `name` (string, optional, nullable) — An optional name for the participant. ### FunctionDefinition - `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. ### ChatCompletionNamedToolChoice - `type` ("function", required) — The type of the tool. Currently, only `function` is supported. - `function` (ChatCompletionNamedToolChoiceFunction, required) ### JsonSchemaDefinition The `json_schema` body of a `response_format` of type `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. ### ChatCompletionResponseMessage - `content` (string, required, nullable) — Assistant message text. May be `null` when `tool_calls` is present. - `role` (enum, required) — The role of the author of this message. - Allowed values: `assistant` - `refusal` (string, optional, nullable) - `reasoning_content` (string, optional, nullable) — Visible reasoning trace from the model, when supported. Billed as completion tokens. - `tool_calls` (list of ChatCompletionMessageToolCall, optional, nullable) — The tool calls generated by the model. ### CompletionTokensDetails - `reasoning_tokens` (integer, optional, nullable) — Tokens spent on the chain-of-thought. Billed as completion / output tokens. ### CompletionUsagePromptTokensDetails `null` on a cache miss. On a hit, includes `cached_tokens`. Treat a missing object and `cached_tokens: 0` as different when computing cache-hit rates. - `cached_tokens` (integer, optional) — Prompt tokens served from cache and billed at the cached-input rate. ### ChatCompletionRequestUserMessageContent The contents of the user message. ### ChatCompletionMessageToolCall - `id` (string, required) — Tool call id (for example `call-`). - `type` ("function", required) — The type of the tool call, currently only `function`. - `function` (FunctionCall, required) ### ChatCompletionRequestGenericMessageContent The contents of the message. ### ChatCompletionNamedToolChoiceFunction - `name` (string, required) — The name of the function to call. ### FunctionCall - `name` (string, required) — The name of the function to call. - `arguments` (string, required) — The arguments to call the function with, as a JSON string. ## 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** ```python chat_completions_example import requests url = "https://api.sarvam.ai/v1/chat/completions" payload = { "messages": [ { "content": "Say hi in one word", "role": "user" } ], "model": "sarvam-105b" } headers = { "Authorization": "Bearer ", "Content-Type": "application/json" } response = requests.post(url, json=payload, headers=headers) print(response.json()) ``` ```javascript chat_completions_example const url = 'https://api.sarvam.ai/v1/chat/completions'; const options = { method: 'POST', headers: {Authorization: 'Bearer ', 'Content-Type': 'application/json'}, body: '{"messages":[{"content":"Say hi in one word","role":"user"}],"model":"sarvam-105b"}' }; try { const response = await fetch(url, options); const data = await response.json(); console.log(data); } catch (error) { console.error(error); } ``` ```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 ") 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 ' 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 response = Unirest.post("https://api.sarvam.ai/v1/chat/completions") .header("Authorization", "Bearer ") .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 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 ', '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 "); 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 ", "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() ```