1. How Temperature Works
DefinitionTemperature 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
Always Coffee0%
Normal (1)
The model's own chances
Coffee
50%
Brew
25%
The
15%
Moon
Mostly Coffee, sometimes others10%
High (above 1)
The chances flatten out
Coffee
37%
Brew
26%
The
20%
Moon
Other words win more often17%
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 timeNormal (1)
The model's own choice
blue.
clear today.
blue.
Some varietyHigh (above 1)
Unusual words get a chance
a quiet painting.
full of small surprises.
blue.
Creative, but it can go off trackExample 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 task | Temperature |
|---|---|
| Facts, code, data extraction | 0 to 0.3 |
| Stories, ideas, marketing copy | 0.7 to 1 |
| Company | Range of temperature |
|---|---|
| OpenAI | 0 to 2 |
| Claude | 0 to 1 |
| Gemini | 0 to 2 |
| Mistral | 0 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
DefinitionMax 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=20The 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 hereExample. 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-openaifrom 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.
temperaturehas the same name in every class.- In this class, the most tokens is
max_output_tokens.max_tokensalso worksmax_tokensmax_tokensmax_tokens.
Way 2one function for every company
install:
pip install langchain langchain-google-genailangchain-groqlangchain-mistralailangchain-openaifrom 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)
temperatureandmax_tokenshave the same names for every company.- LangChain changes
max_tokensinto the name that the company's class uses. For Gemini that ismax_output_tokens. - To switch company, change only the string. The settings stay the same.