Chatbot from Scratch · Chapter 2

ui.py, a window that talks to bot.py

Your bot answers one question written inside its code. By the end of this chapter, you type as many questions as you like into a window of your own, and the window shows each reply with its tokens.

One sitting~10 min read, ~45 min to build along (the agent works for about half an hour)
You needthe project from Chapter 1, Antigravity, your own key in .env
You builda terminal chat, then ui.py: a window from your own sketch
Chapter 2after Chapter 1 · bot.py, the first reply

Prefer to watch? This chapter goes with Episode 2 (9:16). Open it on YouTube.

Subscribe on YouTube

Goes with Episode 2"How do I give my chatbot its own window?". Watch it just above, or build along here at your own pace.
Before you startThe project from Chapter 1, with 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.

One question, then it stops

At the end of Chapter 1, bot.py asked Gemini one question, with your key safely in .env.

Our project after Chapter 1: bot.py on our PC reads the API key from .env and sends the question to Gemini 3.1 Flash Lite in Google's cloud; the answer comes back.
Where Chapter 1 left us: one file on our PC, Gemini in Google's cloud.

But the question is written into the code itself. Ours asked for a one-line poem about the rain. To ask anything else, you open the code, change the line, save, and run again. Nobody chats like that.

The question for this chapter

How do I give my chatbot its own window?

We get there in two steps:

  1. A chat in the terminal: type a question, read the answer, type the next one.
  2. A window around it, built from a sketch you draw yourself.

Step 1: a chat in the terminal

Make a new file in your prompts folder: 02-terminal-chat.txt. It has the same three parts as in Chapter 1: the task, the context, and the folder layout.

prompts/02-terminal-chat.txt
Task : Let me chat with the bot in the terminal, as many questions as I like

Context :
- Use the virtual environment .venv in this folder. Don't create a new one.
- Today the question is written inside bot.py. I want to type my question in the terminal while it runs.
- After each answer, ask me for the next question.
- When I type exit or bye, the chat ends.
- Show my question as "You: ..." and the answer as "Bot: ...", so the chat reads like a conversation.
- Put the call to Gemini in one function, ask(question), that returns the reply text and the token usage (tokens in, tokens out), so a window can use it later.
- Add a function usage() that returns the totals so far in this run: tokens in, tokens out, and the cost in dollars (Gemini 3.1 Flash Lite: $0.25 per 1 million input tokens, $1.50 per 1 million output tokens).
- Keep the model, the thinking setting and load_dotenv exactly as they are.
- Each question is on its own: no chat history yet.

Folder arch :
- bot.py : the chatbot (backend)
- .env : GEMINI_API_KEY
- requirements.txt : the libraries used

This is the file as it ends the chapter. The "You:" and "Bot:" line came one run later; see You and Bot.

Line in the promptWhy it is there
Use .venv in this folderWithout it, agents often make a second environment, and your libraries end up in two places.
exit or bye ends the chatA loop needs a way out that you choose.
ask(question)One door to Gemini. The window in Step 2 calls the same function.
usage(), with the pricesThe running total of tokens and cost, for the window's counter.
Keep the model and load_dotenvThe agent changes only what you asked for.

Hand the file to the agent.

Type into the agent panel
Review and implement prompts/02-terminal-chat.txt

It asks before it reads your .env file: we chose Yes, and always allow in this conversation. It also asks before each test run: choose Yes, allow this time.

When it is done, its summary says: bot.py changed, and the README too.

Click Accept all, and read bot.py yourself. At the top, the model and the thinking setting are as they were. Further down are the two new functions, ask and usage. At the bottom, a loop keeps asking.

Now run it, and ask three questions:

Type in the terminal
python bot.py

The prompt should start with (.venv). If not, run .venv\Scripts\activate first.

We asked: what is the capital of France? Paris. What does a laptop's RAM do, in two sentences? And a one-line poem about the rain.

Then bye, and the chat ended. Three questions, and not one edit to the code.

You and Bot

Our first run printed "Ask a question:" before each question, and the bare answer under it. It read like a form, not a chat.

So we added one line to the prompt, the fifth one above: show my question as "You:" and the answer as "Bot:". Then we sent a short message to the agent.

Type into the agent panel
Review the new line in prompts/02-terminal-chat.txt and implement it

A prompt file can grow. Add the line, and point the agent at it.

The terminal: (.venv) python bot.py. You: What is the capital of France? Bot: The capital of France is Paris. You: Explain what a laptop's RAM does, in two sentences. Bot: RAM acts as your laptop's short-term memory... You: Write a one-line poem about the rain. Bot: The clouds dissolve in silver threads to touch the thirsty earth.
The agent changed two lines in bot.py. Now it reads like a conversation.

Step 2: why a window, if the terminal works?

A terminal is for programmers. Everyone else expects a window: a place to type, a Send button, and the chat above it.

So start a second prompt, prompts/02-window.txt, and try to describe that window in words. We tried:

Our first try, in wordsA big area at the top where the chat shows up, my question on the left and the answer on the right, a bit lower, and under it a box to type in, not too tall, maybe one or two lines, and a button on the right of the box, and somewhere in a corner the number of tokens

It gets long, and it is still vague. How tall is "not too tall"? Which corner? The agent would have to guess.

A sketch, not words

A picture says it faster. Open Paint and draw the window you want. Ours, in the order we drew it:

  1. A frame for the window.
  2. At the bottom, a typing box and a Send button.
  3. Above them, a big chat area.
  4. Inside it, your question in a bubble on the left, and the reply in a bubble on the right.
  5. Labels in the bubbles: User and Bot.
A sketch drawn in Paint: a window frame; a large rounded chat area with a bubble on the left labelled User : <Query> and a bubble on the right labelled Bot : <response>; at the bottom a long typing box and a small Send button.
Our sketch. Rough is fine: it only has to show the layout.

Save it as ui-sketch.png in your prompts folder, next to the prompt files, and open it in Antigravity. Now the agent can see it too.

The window prompt

Replace the words in 02-window.txt with a prompt that points at the picture. The picture carries the layout; the prompt carries what a picture cannot show.

prompts/02-window.txt
Task : Create a desktop window for the chatbot, from my sketch prompts/ui-sketch.png

Context :
- Open the picture prompts/ui-sketch.png first and build the window to look like it: a large chat area, my question and the bot's reply in bubbles, a typing box at the bottom and a Send button.
- Use the virtual environment .venv in this folder. Don't create a new one.
- Use tkinter. Create ui.py; it calls ask() from bot.py.
- The chat area is only for reading: I can read and copy from it, but not type into it.
- In the typing box, pressing Enter sends my question, just like clicking Send.
- Under each reply, one small grey line: tokens in and tokens out.
- In the top right corner, a token counter like Google AI Studio's: it shows the total tokens so far; when I hover over it, it shows tokens in, tokens out and the cost so far. It gets these numbers from usage() in bot.py.
- While the bot is thinking, show "Thinking..." and keep the window responsive.
- If anything goes wrong, show a short plain message in the chat, in the error colour below, and keep the window open. For example:
-- 429 RESOURCE_EXHAUSTED: "Too many requests for the free tier. Wait a minute and try again."
-- 503 UNAVAILABLE: "Gemini is busy right now. Try again in a moment."
-- no internet: "No internet connection."
- The window opens at half the size of the screen.
- The typing box at the bottom is 10% of the window's height; the chat area above it takes the rest.
- Use the Learn AI by Building colours: window background cream #faf6ee; chat area white #ffffff; my questions in plum #5a3d5c; the bot's replies in ink #2b2730; the token line in grey #6a626e; error messages in clay #a8441f.
- No menus, no system prompt and no chat history yet.

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

Sizes and colours are written out in numbers. "Not too tall" became "10% of the window's height".

Type into the agent panel
Review and implement prompts/02-window.txt

It reads the sketch first. It asks before a live test of the window; allow it once.

Ours took about 12 minutes. Its summary lists what it built. Speech bubbles with tails, matching the sketch. A read-only chat, a token line under each reply, and the counter. ui.py is new; bot.py did not change. Click Accept all.

Our window

Type in the terminal
python ui.py

The window opens, laid out like the sketch. We asked the same three questions.

France went with the Enter key: "Thinking..." showed, then Paris, with "8 in, 7 out" under it. The RAM question went with the Send button. Then the poem.

The Chatbot window: a cream background, a white chat area with speech bubbles. User : Write a one-line poem about the rain. Bot : The sky descends in silver threads to stitch the earth to clouds. 11 in, 13 out. The counter at the top right says 106 tokens, and its hover box says Tokens in: 33, Tokens out: 73, Cost so far: $0.000118. A typing box and a plum Send button at the bottom.
Hover over the counter: 33 tokens in, 73 out, $0.000118 so far.

Check the cost yourself: 33 input tokens at $0.25 per million, plus 73 output tokens at $1.50 per million, is $0.000118. One more test: try to type into the chat area. The text goes to the typing box, as the prompt asked.

The prompt asked forCheck it in your window
A layout like the sketchChat area, bubbles left and right, typing box, Send
Enter sends, like the buttonAsk one question each way
A grey token line under each reply"8 in, 7 out" under Paris
A counter with a hover boxTokens in, tokens out, cost so far
A read-only chat areaTyping there lands in the typing box
"Thinking..." while it waitsShows, and Send is greyed out until the reply
Half the screen, typing box 10%Ours opened 960 pixels wide on a 1920 screen

When something goes wrong

What happens when Gemini cannot be reached? We switched off the internet on our test machine and asked for the capital of Japan.

The Chatbot window: User : What is the capital of Japan? and under it, in clay, No internet connection. The window is still open.
"No internet connection." in clay, and the window stays open. With the internet back on, the same question got Tokyo.

The free tier has limits too. Google AI Studio shows yours on its Rate Limit page. On the day we recorded, 3.1 Flash Lite allowed 15 requests a minute, and our peak was 16, mostly from the agent's own tests.

Google AI Studio's Rate Limit table: Gemini 3.1 Flash Lite, Text-out models, RPM 16 / 15 with a red bar, TPM 36.2K / 250K, RPD 78 / 500.
RPM is requests per minute, TPM tokens per minute, RPD requests per day. The limits change; check your own page.

Here are the two errors you are most likely to meet on the free tier. Our window has a plain message for each:

ErrorWhat it meansWhat our window says
429 RESOURCE_EXHAUSTEDToo many requests for your limitToo many requests for the free tier. Wait a minute and try again.
503 UNAVAILABLEGemini's servers are busyGemini is busy right now. Try again in a moment.
No connectionYour PC cannot reach GeminiNo internet connection.
Careful here

The free tier does not always stop you at the exact number on the page. We sent 120 quick requests on the same day and got no 429. Treat the page as a guide, and let your code handle the error when it does come.

Frontend and backend

How do the two files talk? Line 4 of ui.py:

ui.py, line 4from bot import ask, usage

In bot.py, ask talks to Gemini, and usage adds up the tokens and the cost. The window never talks to Gemini itself.

That split has a name. ui.py is the frontend: the part you see and click. bot.py is the backend: the part that talks to Gemini. You can change one without touching the other.

Our project after Chapter 2: You type and read in ui.py, the window (frontend), on our PC. ui.py calls ask() and usage() in bot.py (backend), which reads the API key from .env and sends the question over the internet to Gemini 3.1 Flash Lite in Google's cloud. You and ui.py are new, in orange.
Our project so far. The new parts are in orange: you, and the window.

Quick reference

Every command from this chapter. The two prompt files are above, in A chat in the terminal and The window prompt.

CommandWhat it doesUse it when
.venv\Scripts\activateSwitches the environment onIn every new terminal
Review and implement prompts/02-terminal-chat.txtBuilds the terminal chatStep 1
python bot.pyRuns the terminal chat; type bye to endAfter Step 1
Review and implement prompts/02-window.txtBuilds the window from your sketchStep 2
python ui.pyOpens the windowAfter Step 2

The takeaway

  1. For a layout, a rough sketch beats a paragraph. Hand the agent the picture, and put sizes and colours in numbers.
  2. Name your virtual environment in every prompt, so the agent does not make a new one.
  3. The window is the frontend, bot.py the backend. One function, ask, joins them.

References

Where each step and number on this page comes from. Everything was also checked in our own recording on 17 Nov 2026.

  1. Google · Google AI Studio, Rate Limit The free tier's limits per model: requests a minute, tokens a minute, requests a day. https://aistudio.google.com/rate-limit
  2. Google · Gemini API rate limits The free tier and its current limits. https://ai.google.dev/gemini-api/docs/rate-limits
  3. Python · tkinter: Python interface to Tcl/Tk The window library ui.py is built on, part of Python itself. https://docs.python.org/3/library/tkinter.html
  4. Learn AI by Building · chatbot-from-scratch on GitHub This chapter's finished code, prompts and sketch. https://github.com/Aditya-402/chatbot-from-scratch
  5. My own recording · 17 Nov 2026, Windows 11 Every picture and answer on this page is from those takes: Gemini 3.1 Flash Lite, Antigravity's agent on Gemini 3.8 Flash.