You now know how to send a message list and get a reply. A chatbot is what you get when you do that over and over.
A chatbot is a loop
Behind the chat bubbles, a simple chatbot runs the same few steps again and again:
- Wait for the user to type something.
- Add their message to the history, with the role "user."
- Send the whole history, plus the system prompt, to the model.
- Show the reply, and add it to the history with the role "assistant."
- Go back to step 1.
That's the whole trick. The model itself remembers nothing between requests. Your code is the memory. It holds the list of messages and hands it over fresh each time, so the model can "see" the conversation so far.
The history keeps growing
Each turn adds two messages: one from the user and one from the model. After a long chat, you are sending a lot of text with every request. That causes two problems.
First, cost. You pay for every token you send, so a long history makes each new reply more expensive. Second, every model has a context window, the most text it can read at once. Sooner or later, a long history won't fit.
So real chatbots trim the history. Common ways to do it:
- Keep the system prompt and only the last several messages.
- Replace older messages with a short summary of what was said.
- Start a fresh chat when the topic changes.
Whatever you choose, always keep the system prompt. It holds the ground rules, and dropping it means the bot forgets its job.
Give it a clear job
A chatbot that tries to do everything tends to do nothing well. Decide what it's for, and write that into the system prompt. Say who it helps, what topics it covers, and what it should do when someone asks about something else. For example, a library chatbot might answer questions about opening hours and borrowing, and kindly point people elsewhere for anything medical or legal.
Boundaries like these make the bot more useful and less likely to give advice it has no business giving. They also make it easier to test, which is coming up later in this branch.
Try it
Draw the chatbot loop as five boxes with arrows on a scrap of paper. Then, next to it, write one sentence for your own chatbot idea: what it does, and one thing it should politely refuse.