Systems Library / Customer Service / How to Build an AI Customer Support Chatbot
Customer Service chatbots

How to Build an AI Customer Support Chatbot

Deploy an AI chatbot that handles 80% of support questions automatically.

Jay Banlasan

Jay Banlasan

The AI Systems Guy

An ai customer support chatbot on your website can handle 80% of incoming questions without a human touching them. I run these for service businesses where the same 20 questions make up most of the support volume. The setup takes a few hours and the payoff is immediate.

The key is not building a general-purpose chatbot. You build one trained on your specific FAQs, product docs, and policies. That is what makes it actually useful.

What You Need Before Starting

Step 1: Structure Your Knowledge Base

Collect every support question you answer regularly. Put them in a structured format:

knowledge_base = [
    {
        "question": "What is your return policy?",
        "answer": "You can return any item within 30 days of purchase for a full refund. Items must be in original packaging."
    },
    {
        "question": "How do I track my order?",
        "answer": "Log into your account and click Order History. Your tracking number appears once the order ships, usually within 24 hours."
    },
]

Step 2: Build the Chat Endpoint

Create an API that takes a user message and returns an AI response grounded in your knowledge:

import anthropic
from flask import Flask, request, jsonify
import os

app = Flask(__name__)
client = anthropic.Anthropic()

SYSTEM_PROMPT = """You are a customer support agent for [Your Company].
Answer questions using ONLY the information provided below.
If you don't know the answer, say: "Let me connect you with our support team."
Never make up information.

KNOWLEDGE BASE:
{knowledge}"""

@app.route("/chat", methods=["POST"])
def chat():
    user_message = request.json["message"]
    knowledge = load_knowledge_base()

    response = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=500,
        system=SYSTEM_PROMPT.format(knowledge=knowledge),
        messages=[{"role": "user", "content": user_message}]
    )

    return jsonify({"reply": response.content[0].text})

Step 3: Add Conversation History

Single-turn bots feel broken. Store conversation history per session:

from collections import defaultdict

sessions = defaultdict(list)

@app.route("/chat", methods=["POST"])
def chat():
    session_id = request.json["session_id"]
    user_message = request.json["message"]

    sessions[session_id].append({"role": "user", "content": user_message})

    response = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=500,
        system=SYSTEM_PROMPT.format(knowledge=load_knowledge_base()),
        messages=sessions[session_id]
    )

    reply = response.content[0].text
    sessions[session_id].append({"role": "assistant", "content": reply})
    return jsonify({"reply": reply})

Step 4: Add the Handoff Trigger

When the bot cannot answer, route to a human. Detect this from the bot's own response:

def needs_human_handoff(reply):
    handoff_phrases = [
        "connect you with our support team",
        "transfer you to",
        "let me get someone"
    ]
    return any(phrase in reply.lower() for phrase in handoff_phrases)

When triggered, create a support ticket or send a Slack notification with the full conversation so the agent has context.

Step 5: Deploy the Widget

A simple frontend widget loads on your site and connects to your chat endpoint:

<div id="chat-widget">
  <div id="messages"></div>
  <input type="text" id="user-input" placeholder="Ask a question..." />
  <button onclick="sendMessage()">Send</button>
</div>
<script>
async function sendMessage() {
  const input = document.getElementById("user-input");
  const response = await fetch("/chat", {
    method: "POST",
    headers: {"Content-Type": "application/json"},
    body: JSON.stringify({message: input.value, session_id: getSessionId()})
  });
  const data = await response.json();
  appendMessage("assistant", data.reply);
  input.value = "";
}
</script>

What to Build Next

Add analytics tracking. Log every conversation, flag the ones where the bot could not answer, and use those gaps to expand your knowledge base weekly. The bot gets smarter every week without touching the code.

Related Reading

Want this system built for your business?

Get a free assessment. We will map every system your business needs and show you the ROI.

Get Your Free Assessment

Related Systems