AI Hints
Home›LangChain Tutorial›Settings, Streaming, Batches
LangChain · Lesson 7 of 120

Model Settings

Temperature and limits.

⏱ 2 min read▶ Video at 6:20🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

When you create a model, you can tune how it behaves. Two settings matter most. Temperature, and the maximum number of output tokens.

Model Settings
From the video: Model Settings
  • Tune the model
  • Temperature
  • Max output tokens

Common settings

Temperature controls randomness. Zero gives steady, predictable answers, good for facts and data. Higher values, like one, give more varied, creative writing. Max tokens caps how long the answer can be, which protects your costs. Timeout and max retries control what happens when the provider is slow.

keyvalue
temperature=0steady, predictable
temperature=1varied, creative
max_tokenscaps answer length
timeout, max_retrieswhen calls are slow

Temperature in action

Let's ask for a name for a bookshop cat three times, at two temperatures. Run it. Compare the two lists. Low temperatures favour the single most likely answer, while high temperatures let less likely answers through, so they tend to vary more. Use low temperatures for facts, and higher ones for creative tasks.

temperature.pypython
from langchain.chat_models import init_chat_model

prompt = "Invent an unusual name for a bookshop cat. Reply with the name only."
for temp in [0, 1.5]:
    model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=temp)
    names = [model.invoke(prompt).text.strip() for _ in range(3)]
    print(f"temperature {temp}: {names}")
Output
temperature 0: ['Paragraph', 'Bibliopatch', 'Paragraph']
temperature 1.5: ['Paragraph', 'Folio Fluffernutter', 'Footnote']

Summary

Temperature controls randomness, and max tokens caps the answer length. Use temperature zero for facts. Next, showing answers as they're written.

Key takeaways
  • temperature
  • max_tokens
  • Low for facts
Next up: Streaming — word by word