Generative AIby Dhruv Narang

1. How Temperature Works

Definition

Temperature is a setting that controls how random the model's choice of the next token is. Low temperature gives safe, predictable answers. High temperature gives more creative, surprising ones.

The model picks the first word of a coffee shop name from a list of chances:
Low (0)

Always the top token

Coffee
100%
Brew
0%
The
0%
Moon
0%
Always Coffee
Normal (1)

The model's own chances

Coffee
50%
Brew
25%
The
15%
Moon
10%
Mostly Coffee, sometimes others
High (above 1)

The chances flatten out

Coffee
37%
Brew
26%
The
20%
Moon
17%
Other words win more often
Example chances for one word. The real numbers depend on the model and the prompt.

2. One Prompt, Three Temperatures

The same prompt, asked three times at each temperature.

Prompt: "Finish the sentence: The sky is" Asked three times:
Low (0)

Safe and predictable

blue.
blue.
blue.
Almost the same every time
Normal (1)

The model's own choice

blue.
clear today.
blue.
Some variety
High (above 1)

Unusual words get a chance

a quiet painting.
full of small surprises.
blue.
Creative, but it can go off track
Example answers to show the idea. Real answers vary.

3. Which Temperature to Use

Pick it by the job. Each company allows its own range.

Your taskTemperature
Facts, code, data extraction0 to 0.3
Stories, ideas, marketing copy0.7 to 1
CompanyRange of temperature
OpenAI0 to 2
Claude0 to 1
Gemini0 to 2
Mistral0 to 1.5
Temperature 0 is the most consistent setting, but it does not promise the same answer every time. Small differences on the server can still change a word.

4. Max Tokens

Definition

Max tokens (max_tokens) is the most tokens the model may write in one reply. It controls the cost and keeps answers short.

Prompt: "Explain what an API key is." with max_tokens=20
The reply stops after 20 tokens

It can end in the middle of a sentence

An API key is a secret code that a program sends to a service to show who it is, so the
The reply is cut here
Example. Without the limit, the model keeps writing until it is done.

5. Setting Both in LangChain

Both settings go in the same line that creates the model. Pick your company in each way.

Way 1a class for each company
install: pip install langchain-google-genailangchain-groqlangchain-mistralailangchain-openai
from dotenv import load_dotenv
from langchain_google_genai import ChatGoogleGenerativeAIfrom langchain_groq import ChatGroqfrom langchain_mistralai import ChatMistralAIfrom langchain_openai import ChatOpenAI

load_dotenv()

model = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0.2, max_output_tokens=150)model = ChatGroq(model="openai/gpt-oss-20b", temperature=0.2, max_tokens=150)model = ChatMistralAI(model="open-mistral-nemo", temperature=0.2, max_tokens=150)model = ChatOpenAI(model="gpt-4o-mini", temperature=0.2, max_tokens=150)
  • The settings go inside the brackets of the class, after the model name.
  • temperature has the same name in every class.
  • In this class, the most tokens is max_output_tokens. max_tokens also worksmax_tokensmax_tokensmax_tokens.
Way 2one function for every company
install: pip install langchain langchain-google-genailangchain-groqlangchain-mistralailangchain-openai
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model

load_dotenv()

model = init_chat_model("google_genai:gemini-2.5-flash", temperature=0.2, max_tokens=150)model = init_chat_model("groq:openai/gpt-oss-20b", temperature=0.2, max_tokens=150)model = init_chat_model("mistralai:open-mistral-nemo", temperature=0.2, max_tokens=150)model = init_chat_model("openai:gpt-4o-mini", temperature=0.2, max_tokens=150)
  • temperature and max_tokens have the same names for every company.
  • LangChain changes max_tokens into the name that the company's class uses. For Gemini that is max_output_tokens.
  • To switch company, change only the string. The settings stay the same.