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
  • How it works:
API Guides & TutorialsChat CompletionHow-to

How to control response randomness with temperature

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

How to control response diversity with top_p

Next
Built with

The temperature parameter controls how random or deterministic the model’s responses will be.

Range: 0 to 2
Default: 0.2

  • Lower temperature → more focused, predictable answers (e.g. 0.2)
  • Higher temperature → more creative, varied responses (e.g. 0.8 or 1.0)

👉 Tip: For most use cases, values between 0.2 and 0.8 give good results.

How it works:

ModeRecommended temperatureBehavior
Non-thinking mode0.2 (default)Straightforward, factual responses
Thinking mode0.5 or higherDeeper reasoning, more exploration
Highly creative0.8 - 1.0Storytelling, brainstorming, poetry
Very random / playful> 1.0Unexpected, experimental output

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 temperature (0.2) — straightforward, factual response
7response = client.chat.completions(
8 model="sarvam-105b",
9 messages=[
10 {"role": "system", "content": "You are a helpful assistant."},
11 {"role": "user", "content": "Explain the concept of gravity."}
12 ],
13 # temperature is not specified → uses default 0.2
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 temperature = 0.9 — more creative, varied response
6response = client.chat.completions(
7 model="sarvam-105b",
8 messages=[
9 {"role": "system", "content": "You are a creative storyteller."},
10 {"role": "user", "content": "Tell me a story about a magical tiger."}
11 ],
12 temperature=0.9 # More creative storytelling
13)
14
15# Receive assistant's reply as output
16print(response.choices[0].message.content)