Your First Chatbot
A conversation with memory.
Now we build. A chatbot is just a loop, that asks for input, sends the whole conversation to the model, prints the reply, and remembers it. In this lesson, you will see the complete idea, in about fifteen lines of code.

- A chatbot is a loop
- Remember by sending history
- Fifteen lines of code
The chatbot loop
Here is the loop. Step one, read what the user types. Step two, add it to the message list. Step three, send the whole list to the model. Step four, print the reply. Step five, add the reply to the list too. Then go back to step one. The list grows, and that growing list is the bot's memory.
- Read inputthe user types
- Add to historyappend the message
- Ask the modelsend the whole list
- Print and rememberappend the reply
The chatbot in code
Here it is in code. We start the list with a system message. Inside a while loop, we read the input, and stop if the user types quit. We append the user message, call the model, and read the reply text. We print it, and append it, as an assistant message. That is a complete chatbot. Everything else, is polish.
from openai import OpenAI
client = OpenAI()
messages = [{"role": "system", "content": "You are a helpful tutor."}]
while True:
text = input("You: ")
if text == "quit":
break
messages.append({"role": "user", "content": text})
reply = client.chat.completions.create(model="gpt-4o-mini", messages=messages)
answer = reply.choices[0].message.content
print("Bot:", answer)
messages.append({"role": "assistant", "content": answer})Making it better
Now improve it. Write a better system message, with a clear role, and rules. Stream the reply, so words appear as they are generated. Trim old messages, when the history grows too long. Save conversations to a file, or database. Add a friendly interface, with a web page, instead of a terminal. Each of these is a small step, built on the same loop.
Summary
Excellent. A chatbot is a loop, with a growing message list that gives it memory. You have seen the full idea in a handful of lines, and a path to improve it. Next, we make model outputs reliable enough for real software, with structured outputs.
Key takeaways- Chatbot = loop + message history
- History is the bot's memory
- Improve: stream, trim, save, interface