For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
CommunityAPI StatusAPI PricingSign Up
DocumentationAPI ReferencesCookbookIntegrationDeveloper Tools
DocumentationAPI ReferencesCookbookIntegrationDeveloper Tools
  • Getting Started
    • Welcome
    • Quickstart
    • SDKs & Libraries
    • Building for Indian Languages
    • Models
    • Credits & Rate Limits
    • Errors & Troubleshooting
    • Talk to us
    • Pricing
    • Changelog
  • API Guides & Tutorials
      • Overview
        • List your chat messages
        • Control response randomness
        • Control response diversity
        • Adjust the model's thinking level
        • Improve response factual accuracy
        • Encourage new topics in response
        • Reduce repetition words or phrases in response
        • Get repeatable results
        • Control the response length
        • Control where the model stops
LogoLogo
CommunityAPI StatusAPI PricingSign Up
On this page
  • Why use max_tokens?
  • How to choose the value:
  • Parameter details:
API Guides & TutorialsChat CompletionHow-to

How to control the response length with max_tokens

||View as Markdown|
Was this page helpful?
Previous

How to control where the model stops using stop

Next
Built with

The max_tokens parameter lets you control how long the model’s response can be — in terms of tokens.

  • A token can be a word, part of a word, or even punctuation
    (Example: “Hello!” = 2 tokens: "Hello" + "!")

Why use max_tokens?

  • To limit the size of the output
  • To control latency / cost (fewer tokens = faster and cheaper)
  • To avoid overly long answers if you want concise responses

How to choose the value:

Parameter details:

ParameterTypeDefault
max_tokensInteger2048

First, install the SDK:

$pip install -Uqq sarvamai

Then use the following Python code:

1from sarvamai import SarvamAI
2
3# Initialize the SarvamAI client with your API key
4client = SarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY")
5
6# Example 1: Using default max_tokens (not specified) — defaults to 2048
7response = client.chat.completions(
8 model="sarvam-105b",
9 messages=[
10 {"role": "system", "content": "You are a helpful assistant."},
11 {"role": "user", "content": "Tell me about the planet Mars."}
12 ],
13 # max_tokens not specified → defaults to 2048
14)
15
16print(response.choices[0].message.content)
1from sarvamai import SarvamAI
2
3client = SarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY")
4
5# Example 2: Using max_tokens = 100 — limit response length
6response = client.chat.completions(
7 model="sarvam-105b",
8 messages=[
9 {"role": "system", "content": "You are a helpful assistant."},
10 {"role": "user", "content": "Summarize the plot of Mahabharata."}
11 ],
12 max_tokens=100
13)
14
15# Receive assistant's reply as output
16print(response.choices[0].message.content)