Welcome! Today, we’ll explore a core feature of AI assistants: the interactive chat loop. If you’ve used a chatbot or virtual assistant, you’ve experienced this loop — an ongoing conversation between you and an AI.
Our goal is to build a simple chat loop using Python and the OpenAI API. By the end, you’ll know how to collect user input, send it to the AI, receive a response, and keep the conversation going. This is a key step toward building advanced AI agents.
What is a chat loop? Think of texting a friend: you send a message, get a reply, and respond again. In programming, a chat loop lets a user and an AI assistant interact repeatedly.
In Python, a while loop keeps the conversation running until the user wants to stop:
Sample output if you type "Hello" and then "exit":
This basic loop asks for input and prints a response, stopping only when "exit" is typed. In a real assistant, we’ll send the input to an AI model and show its reply.
Why track message history? For context. If you ask, “What is AI?” and then, “Can you give an example?” the assistant needs to remember the earlier question.
We keep a list of messages, each a dictionary with a role ("user", "assistant", or "system") and content (the text):
To guide the assistant’s behavior, add a system message at the start:
Usually, you include only the most recent exchanges to save memory and reduce API costs. Here’s a function to build the message list:
This prepares the messages for the API.
With our messages ready, we use the OpenAI API’s chat completions endpoint to get a response.
Here’s a function to send messages and get a reply:
Sample output:
Key parameters:
model: AI model to use (e.g., "gpt-4o").messages: List of system, user, and assistant messages.max_tokens: Maximum length of the reply.temperature: Controls randomness (higher = more creative).
For shorter answers, set max_tokens=30. For more creative replies, try temperature=1.0.
Here’s how to use this in a chat loop:
Sample output if you type "What is AI?" and then "exit":
This keeps the conversation going, sending each new input and recent history to the AI.
The build_messages function prepares the list of messages to send to the API, combining the system instruction, recent user and assistant exchanges from the history, and the latest user input. This gives the AI enough context to generate a relevant response.
For more context, you could include the entire chat history in each request. However, this can quickly exceed the model’s token limit, especially in long conversations. To avoid this, it often makes sense to include only the most recent few turns (for example, the last 1–3 exchanges) when building the messages list. This balances providing enough context for coherent replies while staying within token constraints.
A good assistant handles user input well:
- Recognizes "exit" or "quit" to end the chat.
- Ignores empty input.
- Handles errors, such as missing API keys.
Here’s how to add these features:
Sample output if you press Enter, then type "exit":
For errors (such as missing API keys), catch and display a helpful message:
Sample output if an error occurs:
This makes your assistant more robust.
You’ve learned how to build a simple chat loop for an AI assistant using Python and the OpenAI API. We covered:
- The structure of a chat loop.
- Managing message history for context.
- Sending messages to the API and getting responses.
- Handling user input and errors.
Next, you’ll practice building and testing your own chat loop, reinforcing these concepts and preparing for more advanced AI agent features. Let’s get started!
