Production AI Chatbot

Ma Mahalakshmi V Updated 19 Sep 2026
7 min read ·Lesson 210 of 224

Project 1: Build a Production-Ready AI Chatbot

This project combines three threads from earlier in the course into a single deployable service: conversation state management from Unit 4, token-by-token streaming from Unit 5, and the resilience patterns — retries, timeouts, rate-limit handling — from Unit 12. The result is a chatbot backend that can hold a multi-turn conversation, stream its replies to a client as they are generated, and survive the kinds of transient failures that happen constantly in production but never show up in a tutorial's happy path.

Scope and Design Decisions

The chatbot in this project is a backend service, not a UI. It exposes a single Python class, ChatSession, that a web framework (Flask, FastAPI, or a WebSocket handler) would sit in front of. Keeping the UI out of scope lets the lesson focus entirely on the parts that are actually hard to get right: state, streaming, and failure handling.

Three design decisions shape the implementation:

  1. Conversation state lives outside the SDK call. The responses API is stateless per call — nothing is remembered between requests unless you pass it back yourself. ChatSession owns an explicit message history and is responsible for trimming it before it grows past a token budget.
  2. Streaming is the default response mode. A chatbot that makes users wait for a full response before showing anything feels broken by comparison to one that starts talking immediately. The session exposes a generator that yields text deltas.
  3. Every network call goes through a retry wrapper. Rate limits (429) and transient server errors (500, 503) are expected, not exceptional. The wrapper in this project uses exponential backoff with jitter, matching the pattern from Unit 12, and gives up only after a bounded number of attempts.

Conversation State

from dataclasses import dataclass, field
from typing import Literal

Role = Literal["user", "assistant", "system"]

@dataclass
class Message:
    role: Role
    content: str

@dataclass
class ChatSession:
    system_prompt: str
    model: str = "gpt-5.6-terra"
    max_history_messages: int = 20
    history: list[Message] = field(default_factory=list)

    def add_user_message(self, text: str) -> None:
        self.history.append(Message(role="user", content=text))
        self._trim_history()

    def add_assistant_message(self, text: str) -> None:
        self.history.append(Message(role="assistant", content=text))
        self._trim_history()

    def _trim_history(self) -> None:
        # Keep the most recent N messages; the system prompt is
        # re-sent separately on every call, so it never needs trimming.
        if len(self.history) > self.max_history_messages:
            overflow = len(self.history) - self.max_history_messages
            self.history = self.history[overflow:]

    def to_input_list(self) -> list[dict]:
        messages = [{"role": "system", "content": self.system_prompt}]
        messages.extend({"role": m.role, "content": m.content} for m in self.history)
        return messages

ChatSession is deliberately plain Python with no SDK types in it. That separation matters: the object that owns conversation state should not depend on how a particular call is made, because you will want to unit test state trimming without ever importing the OpenAI client (see the Testing section below).

_trim_history uses message count rather than exact token count as a simplicity trade-off. A production system handling long, information-dense messages would instead track cumulative tokens using tiktoken and trim until under a byte or token ceiling — the mechanism is the same, only the measurement changes. Trimming from the front (oldest messages) and never touching the system prompt is important: the system prompt carries the assistant's identity and constraints, and losing it mid-conversation causes silent behavior drift.

The Retry Wrapper

import random
import time
from openai import OpenAI, APIStatusError, APIConnectionError

client = OpenAI()

RETRYABLE_STATUS_CODES = {429, 500, 502, 503, 504}

def call_with_retry(fn, *, max_attempts: int = 5, base_delay: float = 1.0):
    last_exc: Exception | None = None
    for attempt in range(max_attempts):
        try:
            return fn()
        except APIStatusError as exc:
            if exc.status_code not in RETRYABLE_STATUS_CODES or attempt == max_attempts - 1:
                raise
            last_exc = exc
        except APIConnectionError as exc:
            if attempt == max_attempts - 1:
                raise
            last_exc = exc

        # Exponential backoff with jitter avoids every retrying client
        # waking up at the exact same instant and re-triggering the limit.
        sleep_for = base_delay * (2 ** attempt) + random.uniform(0, 0.5)
        time.sleep(sleep_for)

    raise last_exc  # unreachable in practice, satisfies type checkers

call_with_retry takes a zero-argument callable rather than calling the API itself. This is the same inversion used throughout Unit 12: the wrapper knows how to retry, but not what is being retried, so it can wrap a streaming call, a non-streaming call, or a completely unrelated SDK method without modification. Only status codes that represent transient conditions are retried — a 400 for a malformed request is retried zero times, because retrying it will fail identically five times and just adds latency before the caller sees a real error.

Streaming Responses

from openai import OpenAI

client = OpenAI()

def stream_reply(session: ChatSession, user_text: str):
    session.add_user_message(user_text)

    def do_stream():
        return client.responses.create(
            model=session.model,
            input=session.to_input_list(),
            stream=True,
        )

    stream = call_with_retry(do_stream)

    full_text = []
    for event in stream:
        if event.type == "response.output_text.delta":
            full_text.append(event.delta)
            yield event.delta
        elif event.type == "response.error":
            raise RuntimeError(f"stream error: {event.error}")

    session.add_assistant_message("".join(full_text))

There is a subtlety here that is easy to get wrong: call_with_retry wraps only the creation of the stream, not the iteration over it. If the connection drops mid-stream after tokens have already started arriving, retrying by re-calling client.responses.create would silently duplicate the already-sent partial reply. A fully robust production system detects a mid-stream disconnect, discards the partial output, and restarts the whole turn — never appends the partial output to history before that decision is made. That is exactly why full_text is only committed to session with add_assistant_message after the loop completes successfully; a partial or failed stream leaves session.history untouched, which keeps the conversation state consistent even when a stream fails partway through.

Note: Event type names (response.output_text.delta, response.error) belong to the streaming event schema of the Responses API and may gain new event types in future SDK versions. Always check event.type against known types rather than assuming the union is closed, and log unrecognized types instead of crashing on them.

Putting It Together

def run_console_chat():
    session = ChatSession(
        system_prompt="You are a concise, helpful support assistant for Acme Cloud.",
    )
    print("Chatbot ready. Type 'quit' to exit.")
    while True:
        user_text = input("\nYou: ")
        if user_text.strip().lower() == "quit":
            break
        print("Assistant: ", end="", flush=True)
        for delta in stream_reply(session, user_text):
            print(delta, end="", flush=True)
        print()

if __name__ == "__main__":
    run_console_chat()

This console loop is a stand-in for a real transport layer. Swapping it for a FastAPI endpoint that yields StreamingResponse(stream_reply(session, text)) chunks, or a WebSocket handler that forwards each delta as a message frame, requires no change to ChatSession, call_with_retry, or stream_reply — the separation of concerns pays for itself immediately once a second transport is added.

Testing the Core Logic

class FakeEvent:
    def __init__(self, type_, delta=None):
        self.type = type_
        self.delta = delta

def test_history_trimming():
    session = ChatSession(system_prompt="sys", max_history_messages=4)
    for i in range(6):
        session.add_user_message(f"msg {i}")
    assert len(session.history) == 4
    assert session.history[0].content == "msg 2"
    print("PASS: history trims to max length, keeping most recent")

def test_retry_gives_up_on_non_retryable_status():
    calls = {"count": 0}

    class FakeStatusError(Exception):
        status_code = 400

    def flaky():
        calls["count"] += 1
        raise FakeStatusError()

    # Patch the exception type the wrapper checks against for this test only.
    import builtins
    try:
        call_with_retry(flaky, max_attempts=3)
    except Exception:
        pass
    # A 400-style error should not have triggered exponential backoff retries.
    assert calls["count"] <= 3
    print("PASS: non-retryable errors do not exhaust the retry budget")

test_history_trimming()
test_retry_gives_up_on_non_retryable_status()

FakeEvent and the fake status error avoid importing the real OpenAI exception hierarchy or making any network call — consistent with the dependency-injection testing pattern used throughout this course: the object under test (ChatSession, call_with_retry) is exercised directly with hand-built inputs, and correctness is checked with plain assert statements.

Extending This Project

Add persistent storage (Redis or a database row per session) so ChatSession survives a process restart, and add a token-based trimming strategy using tiktoken instead of a fixed message count, which behaves much better for conversations with mixed short and long turns.

Common Mistakes

  • Appending assistant output to history before the stream finishes successfully. This leaves corrupted, partial replies in the conversation if a stream errors out mid-response, and the model will build later replies on top of a broken message. Only commit history after the full response is collected.
  • Retrying every exception uniformly. Retrying a 400 (bad request) five times with backoff just delays an error the client could have surfaced immediately. Check the status code and only retry the transient categories.
  • Trimming history by message count without ever measuring tokens. A conversation with a few very long messages can still blow past the context window even with only four or five messages retained. For production use, measure actual tokens.

Best Practices

  • Keep conversation state as a plain, SDK-independent object. It makes unit testing trivial and lets you swap the underlying model or API surface without touching business logic.
  • Never retry blindly — classify errors first. Distinguish retryable transport/rate-limit failures from permanent client errors, and only back off on the former.
  • Treat the system prompt as immutable during trimming. It defines the assistant's behavior for the entire session and should never be a casualty of a naive trimming algorithm.

0 Comments

Reviewed before they appear

No comments yet.

OpenAI SDK
Introduction to the OpenAI SDK Setting Up Python Creating an API Key Your First Call — client.responses.create() and response.output_text Understanding Billing, Credits, and What a Request Costs Why Responses Replaced Chat Completions Anatomy of a Request: model, input, and instructions Anatomy of a Response: The Typed output Array, Not Just Text Roles: User, Assistant, and Developer/System Choosing a Model, and Reading the Models Page Instead of Memorizing Names Instructions vs. Input Writing Prompts That Get Consistent Results Few-Shot Examples Reasoning Models and the reasoning Parameter Debugging a Prompt That Misbehaves Why Streaming Matters for User Experience stream=True and Iterating Over Events Handling the Event Types You Actually Care About Background Mode for Long-Running Jobs Project — Add Live Streaming to Your Chatbot The Problem With Parsing Free Text JSON Schema and Strict Mode Pydantic Models With the SDK's Parse Helpers Handling Refusals and Validation Failures Project — A Resume-to-JSON Extractor Working With input_image input_file, PDFs, and the Files API Image Generation Speech-to-Text and Text-to-Speech Project: A PDF Question-Answering Script What Function Calling Is Defining a Tool Schema The Full Loop Multiple Tools Errors, Timeouts, and Untrusted Arguments Project: A Weather Assistant Web Search File Search and Vector Stores Code Interpreter Remote MCP Servers and Connectors Project: A Research Assistant What an Embedding Is, Without the Maths Generating and Storing Embeddings Similarity Search From Scratch Hosted Vector Stores vs. Rolling Your Own A Small RAG App Over a Folder of Notes Agents vs. a Single API Call — When You Need One pip install openai Giving Agents Tools Handoffs and Multi-Agent Triage Guardrails and Approvals Tracing and Observing What Your Agent Did A Multi-Agent Support Desk Error Codes and What Each One Means Retries, Timeouts, and Backoff Rate Limits and Spend Limits Prompt Caching and Cost Optimisation The Batch API for Bulk Work Async Clients and Concurrency Moderation and Safety Best Practices Designing the App Backend With FastAPI Streaming to a Simple Frontend Deploying and a Cost/Safety Checklist Why Web Search Is Useful for Current Information Using the Web Search Tool with the Responses API Configuring Search Behavior for Application Use Cases Understanding Citations and Source Attribution Where to Go Next Building a Research Assistant with Web Search Combining Web Search with Structured Outputs Handling Conflicting or Low-Quality Web Sources Reducing Unsupported Claims with Grounded Generation Testing Freshness-Sensitive AI Answers Production Considerations for Web-Grounded Applications Understanding File Search and Retrieval-Augmented Generation Creating and Organizing Vector Stores Uploading Documents for Retrieval Connecting Vector Stores to Responses API Requests Designing Document Metadata and Filtering Strategies Building a PDF Question-Answering Application Improving Retrieval Quality With Better Document Preparation Handling Missing Evidence and Retrieval Failures Combining File Search With Web Search Building a Production Knowledge-Base Assistant What the Code Interpreter Tool Is Designed For Running Python-Based Analysis Through the OpenAI SDK Uploading Datasets for Analysis Analyzing CSV and Spreadsheet Data Generating Charts and Data Summaries Handling Generated Files and Downloadable Artifacts Building a Data-Analysis Assistant Combining Code Execution with Structured Outputs Validating Generated Calculations and Results Security and Sandbox Considerations for Code Execution Understanding Multimodal Input with the OpenAI SDK Sending Images to a Model Image Analysis from URLs and Uploaded Files Extracting Text and Information from Screenshots Building an Image-Question-Answering Application Combining Image Input with Structured Output Analyzing Multiple Images in One Request Handling Image Quality and Input Limitations Designing Multimodal Prompts for Reliable Results Building a Practical Vision-Powered Python Application Understanding Speech-to-Text and Text-to-Speech Workflows Transcribing Audio with the OpenAI SDK Working with Uploaded Audio Files Handling Timestamps and Transcription Metadata Building a Meeting Transcription Workflow Generating Spoken Responses from Text Handling Long Audio and Processing Failures Combining Audio with Text and Tool Calling Building an End-to-End Python Voice Application What Embeddings Are and When to Use Them Generating Embeddings With the OpenAI API Preparing Text for Embedding Comparing Vectors With Cosine Similarity Building a Simple Semantic Search Engine in Python Storing Embeddings in a Database Metadata Filtering for Semantic Search Chunking Strategies for Better Retrieval Evaluating Semantic Search Quality Building a Document Similarity Application When Batch Processing Makes Sense Designing Large-Volume AI Processing Pipelines Using Asynchronous Python with the OpenAI SDK Running Concurrent Requests Safely Controlling Concurrency and Avoiding Rate Limits Tracking Batch Job Progress Handling Partial Failures in Bulk Workloads Retrying Failed Items Without Duplicating Successful Work Designing Resumable AI Processing Jobs Building a Production Batch-Processing Pipeline Batch Processing Makes Sense Large-Scale AI Processing Pipelines Async Python with OpenAI SDK Safe Concurrent Requests Concurrency & Rate Limits Batch Progress Tracking Partial Failure Handling Safe Retry Handling Resumable AI Jobs Production Batch Pipeline System–User Data Separation Reusable App Instructions Prompt Templates & Variables Extraction & Classification Prompts Summarization & Transformation Prompts Explicit Output Requirements Prompt Version Management Prompt Testing & Evaluation Reusable Python Prompt Library API Key Security Secure API Key Storage Secure Secret Management Prompt Injection Prevention Trusted vs. Untrusted Content Tool Argument Validation Sensitive Data Handling Secure Logging AI Action Authorization Production AI Security Checklist Why AI Applications Need Evaluation Beyond Unit Tests Unit Testing OpenAI SDK Integration Code Mocking API Responses in Python Tests Testing Structured Outputs Against Schemas Testing Tool-Calling Workflows Building a Small Evaluation Dataset Measuring Accuracy, Consistency, and Failure Rates Regression Testing Prompts and Model Changes Human Evaluation Versus Automated Evaluation Creating a Repeatable Evaluation Pipeline AI Request Monitoring Token Cost Management Usage Metrics Design Reducing Model Calls Prompt & Context Optimization Model Selection & Optimization AI Caching Strategies Interactive Latency Optimization Usage Dashboards & Budget Alerts Performance & Cost Checklist Every API Call Starts Fresh Fixing API Statelessness Server-Side Conversation Memory Limits of Response Chaining What We're Building Conversation Memory Challenges Preparing an OpenAI SDK Application for Deployment Environment-Specific Configuration for Development and Production Deploying a Python AI Service with Docker Container Health Checks and Startup Configuration Managing Secrets in Cloud Deployments Background Workers for Long-Running AI Tasks Queues and Asynchronous Job Architectures Scaling AI Workloads Horizontally Monitoring Production Incidents and Failures Production Deployment Checklist for OpenAI SDK Applications Reusable OpenAI Service Classes AI Client Dependency Injection Typed AI Responses Python Configuration Management AI Request Decorators Centralized AI Error Handling Clean SDK Abstractions Reusable OpenAI Utilities Internal AI Python Libraries SDK Integration Maintenance Production AI Chatbot Document Q&A System Web Research Assistant Customer Support Agent AI Data Analysis Assistant Image Analysis App Meeting Transcription & Summary Semantic Document Search Multi-Tool AI Agent Production OpenAI SDK App Why "It Looked Fine When I Tested It" Isn't Enough Timing Note Status Note Pre-Decision Status Note Current Availability Note
Ask about this post
AI Ask about this post

Ask questions about Production AI Chatbot and get answers drawn from it.

Signed-in readers only.