Chatbot from Scratch · Chapter 3

The model's settings

Ask your bot the same thing three times and you can get three answers. This chapter shows where that comes from, and gives your window a Settings menu to control it.

One sitting~10 min read, ~40 min to build along (the agent works for about twenty of them)
You needthe project from Chapter 2, Antigravity, your own key in .env
You builda Settings menu: temperature, top-P, max tokens, reset
Chapter 3after Chapter 2 · ui.py, a window that talks to bot.py

Prefer to watch? This chapter goes with Episode 3 (7:22). Open it on YouTube.

Subscribe on YouTube

Goes with Episode 3"Why does the same question give different answers?". Watch it just above, or build along here at your own pace.
Before you startThe project from Chapter 2, with its window ui.py and its .venv. Antigravity, signed in: install Antigravity on Windows. And your own Gemini key in .env.
The code, ready to runDownload this chapter's code (zip). Every chapter's code is on GitHub. Add your own .env with GEMINI_API_KEY; the README shows the setup.

Three answers to one question

Chapter 2 ended with a window, ui.py, that sends your question to Gemini through bot.py.

Our project after Chapter 2: you type in ui.py, the window (frontend); it calls ask() and usage() in bot.py (backend), which reads the API key from .env and asks Gemini 3.1 Flash Lite in Google's cloud.
Where Chapter 2 left us.

Type this into the window three times, word for word: Write a one-line slogan for my new electronics shop. Reply with the slogan only. Ours answered:

Same question, three runsInnovating your everyday.
Powering your future, today.
Your world, powered by innovation.
The question for this chapter

Why does the same question give different answers?

How the next word is picked

A model writes its answer one word at a time. For each next word, it has a list of candidates, each with a probability. Then it picks one, a little like rolling a weighted die. A few settings decide how much that roll counts.

The probabilities in the top-K and top-P picture below are an example. Our model does not show its real ones. We asked, and the API said: "not enabled for this model".

Four settings

SettingWhat it changesLowHigh
TemperatureHow creative the pick isNearly always the likeliest wordLess likely words get more room
Top-KHow many words stay in the listOnly the few likeliestMany words
Top-PHow much probability stays in the listOnly the top words, until a small shareAlmost every word
Max tokensHow long the answer may beCut shortRoom for a full answer

Top-K and top-P sound alike, and both cut the list before the pick. The difference is what they count.

Two bar charts of the same example words: Powering 40%, Innovating 25%, Your 15%, Smart 10%, Bright 6%, Future 4%. Left, top-K = 3 keeps the first three words, always. Right, top-P = 0.5 keeps Powering and Innovating, because 40% + 25% already reaches 50%.
Top-K counts words; top-P adds up probabilities. Example numbers.
Careful here

On Gemini 3.1 Flash Lite, Google fixes top-K at 64. In our test, asking for top-K 1 still gave different slogans, so the setting is ignored. Top-P is the one you can turn.

Try it in Google AI Studio first

Open Google AI Studio and look at the run settings on the right.

  • Gemini 3.1 Flash Lite shows no temperature and no top-P, only Output length. For this model, those two are set from our code. Set Output length to 20 and ask what a laptop's RAM does: the answer stops at "holding the data and".
  • Gemma 4, Google's open model, does show the temperature slider. At 2, the slogan question gave three different slogans in one chat. At 0, two new chats both gave "Connecting you to the future."

Google advises keeping temperature at 1.0 on the Gemini 3 models; for 3.1 Flash Lite, AI Studio does not show it at all.

AI Studio vs our window

Why a new chat for each test at temperature 0? Because the two behave differently, and the test depends on it.

Google AI StudioOur window
What it isA full chat appOur own small frontend
Sends with each questionThe whole conversation so farOnly your latest question
Asking again in the same chatNot the same input any moreThe same input
A fair repeat testOpen a new chatJust ask again

Our window keeps no history at all. Remember that: it is why the bot forgets your name at the end of this chapter.

The settings prompt

Now bring the same settings into our own window. Make a new file, prompts/03-settings.txt. This time there is no sketch: a menu is easy to describe in words.

prompts/03-settings.txt
Task : Add a menu bar to the window with a Settings menu, with the same settings as Google AI Studio, so I can change how the bot picks its words

Context :
- Use the virtual environment .venv in this folder. Don't create a new one.
- In bot.py, let ask() also take the settings: temperature, top_p and max_output_tokens. Keep the model and the thinking setting as they are.
- Add a menu bar at the top of the window with one menu, Settings.
- Settings > Temperature... opens a small dialog with a slider from 0 to 2, in steps of 0.1, starting at 1.0.
- Settings > Top-P... opens a small dialog with a slider from 0 to 1, in steps of 0.05, starting at 0.95.
- Settings > Max tokens... opens a small dialog with a slider from 10 to 2000, in steps of 10, starting at 2000.
- Each dialog shows the slider's current value, an OK button and a Cancel button.
- Settings > Top-K: 64 (fixed for this model) is shown greyed out, and top_k is left out of the call.
- Settings > Reset to defaults puts every setting back where it started.
- Under the menu bar, one small grey line always shows the current settings, for example: Temperature 1.0 · Top-P 0.95 · Max tokens 2000.
- The grey line under each reply also shows the settings used, and "stopped: max tokens" when the answer was cut short.
- Keep everything else in the window as it is: the chat, the token counter, the error messages and the colours.
- The terminal chat in bot.py keeps working with the starting settings.

Folder arch :
- ui.py : the window (frontend)
- bot.py : the chatbot (backend)
- prompts/ : our prompts

The starting values match AI Studio's defaults, so Reset puts you back where Google recommends.

Type into the agent panel
Review and implement prompts/03-settings.txt

It works for a while. Its summary lists the Settings menu, the status line and the grey line under each reply: 3 files changed. Click Accept all.

Turn the knobs

Type in the terminal
python ui.py

The window now has a Settings menu, with Top-K greyed out at 64. Ask the slogan question after each change.

Temperature 0

Settings › Temperature…, drag the slider to 0, OK. Three asks, one answer: "Powering your future, today."

Temperature 0, sped up. Watch this step in the video.

Temperature 2

Drag it to the top. "Elevating your everyday through innovative technology." "Empowering your world, one gadget at a time." "Upgrade Your World." Different every time.

Temperature 2, sped up. Watch this step in the video.

Top-P 0.1, still at temperature 2

Keep temperature at 2 and set Top-P… to 0.1. Only the top candidates are allowed, and the answer stays put: "Powering your future, today.", twice.

Top-P 0.1 at temperature 2, sped up. Watch this step in the video.

Max tokens 30

Set Max tokens… to 30 and ask what a laptop's RAM does, in two sentences. The answer stops mid-sentence, and the grey line says why: stopped: max tokens. Then Settings › Reset to defaults puts everything back: 1.0, 0.95, 2000.

Max tokens 30, sped up. Watch this step in the video.

Which setting when

You wantTurnOn Gemini 3.1 Flash Lite
A good defaultNothing: Reset to defaultsTemperature 1.0, top-P 0.95 (Google's advice for Gemini 3)
The same answer every timeLower top-P (or temperature)Top-P 0.1 held even at temperature 2
More varied ideasHigher temperatureTemperature 2 changed the slogan on every ask
Short answers, lower costMax tokens30 cut the answer: stopped: max tokens
Fewer word choices by countTop-KFixed at 64; not available
Our project after Chapter 3: you, ui.py the window with a new Settings menu (temperature, top-P, max tokens, in orange), bot.py with ask() and usage(), the key in .env, and Gemini 3.1 Flash Lite in Google's cloud.
Our project so far. The new part is in orange: the Settings menu.

Quick reference

Every command from this chapter. The prompt is above, in The settings prompt.

CommandWhat it doesUse it when
.venv\Scripts\activateSwitches the environment onIn every new terminal
Review and implement prompts/03-settings.txtAdds the Settings menuAfter writing the prompt
python ui.pyOpens the windowTo try each setting

The takeaway

  1. A model picks each next word by chance, weighted by probability. That is why one question can get several answers.
  2. Temperature sets how creative the pick is; top-K and top-P trim the list first, by count or by probability; max tokens caps the length.
  3. On Gemini 3, keep the defaults unless you have a reason. Use max tokens to keep answers short and cheap.

Next: it forgets your name

Tell the window "My name is Adi." It answers "Hi Adi! It's nice to meet you." Then ask "What is my name?" It does not know. Chapter 4 finds out why.

Our window forgets, sped up. Watch this step in the video.

References

Where each setting and number on this page comes from. Everything was also checked in our own recording and test run on 18 Nov 2026.

  1. Google · Gemini 3 developer guide Google's advice to keep temperature at 1.0 on the Gemini 3 models. https://ai.google.dev/gemini-api/docs/gemini-3
  2. Google · Prompt design strategies Temperature, top-K, top-P and max output tokens explained. https://ai.google.dev/gemini-api/docs/prompting-strategies
  3. Google · Google AI Studio The run settings for each model. https://aistudio.google.com
  4. Learn AI by Building · chatbot-from-scratch on GitHub This chapter's finished code and prompts. https://github.com/Aditya-402/chatbot-from-scratch
  5. My own recording · 18 Nov 2026, Windows 11 Every answer on this page is from those takes and a test run: Gemini 3.1 Flash Lite (top-K ignored, word probabilities not enabled, MAX_TOKENS at 20 and 5).