How to Build a Chatbot with Python and OpenAI (Step-by-Step)

Build a working AI chatbot from scratch with Python and the OpenAI API. Complete step-by-step tutorial covering setup, conversation management, streaming, and deployment.

By AI Mastermind Lab · Published 15 Mar 2026

Build a fully functional AI chatbot in Python using the OpenAI API. This tutorial takes you from zero to a working chatbot with conversation memory, streaming responses, and error handling.

What You'll Build

A terminal-based chatbot that:

  • Maintains conversation history
  • Streams responses in real-time
  • Handles errors gracefully
  • Can be extended with a web interface

Prerequisites

  • Python 3.9+
  • An OpenAI API key (get one at platform.openai.com)

Step 1: Project Setup

mkdir my-chatbot && cd my-chatbot
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
pip install openai python-dotenv

Create a .env file:

OPENAI_API_KEY=sk-your-key-here

Step 2: Basic Chatbot (10 Lines)

# chatbot.py
from openai import OpenAI
from dotenv import load_dotenv

load_dotenv()
client = OpenAI()

messages = [{"role": "system", "content": "You are a helpful assistant."}]

while True:
    user_input = input("You: ")
    if user_input.lower() in ("quit", "exit"):
        break

    messages.append({"role": "user", "content": user_input})

    response = client.chat.completions.create(
        model="gpt-4o",
        messages=messages
    )

    reply = response.choices[0].message.content
    messages.append({"role": "assistant", "content": reply})
    print(f"Bot: {reply}")

Run it: python chatbot.py

That's a working chatbot with conversation memory in 20 lines.

Step 3: Add Streaming

Streaming shows the response as it's generated, like ChatGPT:

def chat_stream(messages, user_input):
    messages.append({"role": "user", "content": user_input})

    stream = client.chat.completions.create(
        model="gpt-4o",
        messages=messages,
        stream=True
    )

    print("Bot: ", end="")
    full_reply = ""
    for chunk in stream:
        if chunk.choices[0].delta.content:
            text = chunk.choices[0].delta.content
            print(text, end="", flush=True)
            full_reply += text
    print()  # Newline after response

    messages.append({"role": "assistant", "content": full_reply})
    return full_reply

Step 4: Add Error Handling

from openai import APIError, RateLimitError
import time

def safe_chat(messages, user_input, max_retries=3):
    messages.append({"role": "user", "content": user_input})

    for attempt in range(max_retries):
        try:
            response = client.chat.completions.create(
                model="gpt-4o",
                messages=messages
            )
            reply = response.choices[0].message.content
            messages.append({"role": "assistant", "content": reply})
            return reply
        except RateLimitError:
            wait = 2 ** attempt
            print(f"Rate limited. Waiting {wait}s...")
            time.sleep(wait)
        except APIError as e:
            print(f"API error: {e}")
            messages.pop()  # Remove failed user message
            return "Sorry, something went wrong. Please try again."

    messages.pop()
    return "Service unavailable. Please try again later."

Step 5: Add a System Prompt

Customize your chatbot's personality:

SYSTEM_PROMPT = """You are CodeBuddy, a friendly programming assistant.
Rules:
- Give concise, practical answers
- Include code examples when helpful
- Suggest best practices
- If unsure, say so honestly"""

messages = [{"role": "system", "content": SYSTEM_PROMPT}]

Step 6: Manage Context Length

Prevent exceeding the model's context window:

def trim_messages(messages, max_messages=20):
    """Keep system prompt + last N messages."""
    if len(messages) <= max_messages + 1:
        return messages
    return [messages[0]] + messages[-(max_messages):]

Cost Estimation

Using GPT-4o for a chatbot:

  • Average message: ~500 tokens (input + output)
  • 100 messages/day: ~$0.25/day
  • 1,000 messages/day: ~$2.50/day

For lower costs, use gpt-4o-mini (10x cheaper) or switch to the Claude API.

Next Steps

Once your terminal chatbot works, extend it with:

  • Web UI: Add a Flask/FastAPI frontend
  • RAG: Connect a vector database for knowledge-grounded answers
  • Tool use: Let the chatbot call APIs and execute functions
  • Memory: Persist conversations to a database

TL;DR

A working Python chatbot with OpenAI takes about 20 lines of code. Install openai, set your API key, maintain a messages list, and call chat.completions.create(). Add streaming for real-time output and error handling for production use.

—

Learn about RAG chatbots in our RAG tutorial, build AI agents with LangChain, and explore the Claude API alternative.