AI Chatbot Python Tutorial: From a 20-Line Keyword Bot to Grounded AI Answers With a Safe Fallback
AI chatbot Python projects usually start with a single API call and stop there. This tutorial goes a little further, in three steps: a keyword bot with no dependencies, an AI bot that answers only from your own facts, and a fallback that keeps answering when the AI is unavailable. Along the way you’ll see the design decisions that separate a demo from something a business could trust.
Standard library only. Predictable, free.
A language model that answers from your facts.
AI first, keyword bot if anything fails.
⚡ In short
- Start with a keyword bot: it teaches the structure and gives you a free fallback later.
- Ground the AI: put your facts and rules in the instructions, and tell it to say when it’s unsure.
- Always have a fallback, so the bot never goes silent when the API fails.
- A production chatbot also needs a widget, inbox, handover and monitoring, which this tutorial doesn’t cover.

Before you start
What you need and what you’ll build
For this AI chatbot Python project you’ll need Python 3.10 or newer and a terminal. Step 1 uses only the standard library. Step 2 uses an AI provider’s Python SDK and an API key; the example uses Anthropic’s SDK, but the same pattern works with other providers’ SDKs, and the structure is what matters.
By the end you’ll have a command-line bot for a made-up bakery that answers delivery, hours and returns questions from a facts file, declines medical questions, admits when it doesn’t know, and falls back to keyword matching if the AI is unavailable. It’s a learning project, not a finished product, but the three ideas it demonstrates (grounding, limits and fallback) are exactly what makes business chatbots trustworthy.
Set up a project folder
Create a folder, a virtual environment, and a file called facts.md containing your business facts in short sentences: hours, delivery, prices, returns, contact. We’ll use it in step 2.
Step 1 · Keyword bot
Twenty lines, no dependencies
A tiny bot that matches questions to saved answers with fuzzy matching.
# step1_keyword_bot.py: a tiny keyword bot using only the standard library
import difflib
FAQ = {
"what are your opening hours": "We're open Monday to Saturday, 10 am to 7 pm.",
"how much is delivery": "Delivery is ₹60, free above ₹999.",
"how long does delivery take": "2 to 4 working days to most cities.",
"can i return an item": "Unused items can be returned within 7 days.",
}
def answer(question: str) -> str:
q = question.lower().strip()
match = difflib.get_close_matches(q, FAQ.keys(), n=1, cutoff=0.5)
if match:
return FAQ[match[0]]
return "I'm not sure about that. Want me to pass it to our team?"
if __name__ == "__main__":
while True:
text = input("You: ")
if text.lower() in {"quit", "exit"}:
break
print("Bot:", answer(text))
How it works
difflib.get_close_matches compares the visitor’s question with your stored questions and returns the closest match above a similarity threshold (cutoff=0.5). If nothing is close enough, the bot says it isn’t sure and offers the team.
Try it
Run python step1_keyword_bot.py and ask “how much is delivry” (with the typo); it matches the delivery answer. Then ask “do you deliver on sunday”. You’ll probably get the delivery cost answer, because the words look similar, even though the visitor asked about days. Then try “what’s your phone number” and you’ll see the polite fallback.
What it teaches
Keyword bots are predictable and free, but they only know the phrasings you stored, and similar-looking questions can get the wrong answer. That’s the core weakness every AI chatbot Python tutorial should show honestly. That’s why step 2 exists. Keep this file: it becomes your safety net in step 3.
Step 2 · Grounded AI
A language model that answers only from your facts
The key ideas are in the instructions, not in clever code.
# step2_grounded_ai.py: answers only from your facts, using an AI model
# pip install anthropic (or your provider's SDK)
import os
from anthropic import Anthropic
MODEL = os.environ.get("CHAT_MODEL", "your-model-id") # check your provider's docs for current model names
client = Anthropic() # reads ANTHROPIC_API_KEY from the environment
FACTS = open("facts.md", encoding="utf-8").read()
INSTRUCTIONS = f"""You are the website assistant for Crumb & Co, a bakery in Bengaluru.
Answer ONLY from the facts below. If the facts don't cover a question, say you're not sure
and offer to pass it to the team. Never give medical advice. Answer in 2-3 short sentences,
key fact first. Reply in the visitor's language.
FACTS:
{FACTS}"""
def answer(history: list[dict]) -> str:
response = client.messages.create(
model=MODEL,
max_tokens=300,
system=INSTRUCTIONS,
messages=history,
)
return response.content[0].text
if __name__ == "__main__":
history = []
while True:
text = input("You: ")
if text.lower() in {"quit", "exit"}:
break
history.append({"role": "user", "content": text})
reply = answer(history)
history.append({"role": "assistant", "content": reply})
print("Bot:", reply)
What makes it grounded
The facts file goes into the system instructions, and the instructions tell the model to answer only from those facts, to say when it isn’t sure, and to offer the team. Without that, the model will happily answer from general knowledge, which may not match your business.
Conversation memory
The history list keeps the conversation, so follow-ups like “and on Sunday?” make sense. In a real system you’d store history per visitor and trim old messages to control cost.
Model names
Model IDs change over time. Keep the model in an environment variable and check your provider’s documentation for current names rather than hard-coding one.
Writing the instructions
Where most of the quality comes from
In an AI chatbot Python project, it’s tempting to focus on code. But the instructions string does most of the work. A good set has six parts: role, scope, tone, limits, format and examples. Our AI chatbot design guide covers each in detail; here’s how they appear in the code above.
- Role: “You are the website assistant for Crumb & Co, a bakery in Bengaluru.”
- Scope: “Answer ONLY from the facts below.”
- Limits: “If the facts don’t cover a question, say you’re not sure and offer to pass it to the team. Never give medical advice.”
- Format: “Answer in 2-3 short sentences, key fact first.”
- Language: “Reply in the visitor’s language.”
Test like a customer
Ask questions with typos, mixed languages and two parts. Ask something not in the facts (“do you ship to Dubai?”) and check it says it’s unsure. Ask a medical question and check it declines. Every failure points to a line to add to the facts or instructions.
Keep facts separate from instructions
Prices and policies belong in facts.md, not in the instructions, so non-developers can update them without touching code.
Step 3 · Fallback
Never let the bot go silent
APIs fail: networks drop, keys expire, quotas run out. Plan for it.
# step3_fallback.py: try AI first, fall back to the keyword bot if it fails
from step1_keyword_bot import answer as keyword_answer
from step2_grounded_ai import answer as ai_answer
def reply(history):
try:
return ai_answer(history)
except Exception as err: # network error, quota, missing key...
print("AI unavailable:", err)
return keyword_answer(history[-1]["content"])
Why it matters
A customer doesn’t care why your bot stopped answering. A fallback keeps the conversation going with your stored answers, and your logs tell you to fix the AI side.
Production thinking
The same pattern is how hosted tools handle it. LiveChatBot, for example, falls back to a free keyword bot that answers from your saved replies whenever AI replies aren’t available, so the chat never goes quiet.
Going further
Add a timeout to the AI call, log every fallback, and alert yourself if fallbacks spike. That’s the start of monitoring.
Safety and data
Responsibilities that come with a real bot
Once your bot talks to real people, a few responsibilities apply.
Keys stay secret
Load API keys from environment variables or a secrets manager, never from code committed to a repository. Rotate keys if they leak.
Don’t collect what you don’t need
Your bot doesn’t need customers’ payment details, passwords or ID numbers. Instruct it never to ask for them, and don’t log them if users type them anyway.
Know where data goes
Messages you send to an AI provider are processed by that provider under its terms. Tell users in your privacy policy, and read the provider’s data terms, especially for business use.
Rate limits and cost
Each AI call costs money based on how much text is processed. Long histories and big fact files increase cost. Trim history, keep facts concise, and set spending alerts with your provider.
Abuse
Public bots attract people trying to make them say embarrassing things. Clear limits in the instructions, plus logging and the ability to block abusive users, reduce the risk. See keeping chats free of spam and abuse.
From script to product
What’s missing before customers can use it
You now have a bot that answers well in a terminal. A website chatbot needs much more, which is why most businesses use a hosted tool and why developers often take a middle path.
- A chat widget that works on every browser and phone.
- Storage for conversations, per visitor.
- A team inbox and real-time handover to people, plus phone alerts.
- Opening hours and offline messages, so visitors know when a person will reply.
- Knowledge editing for non-developers.
- Spam handling, blocking and monitoring.
Building all of that is a serious project; our make-vs-buy guide lays out the trade-offs honestly. If you want to see a finished system, LiveChatBot’s free plan gives you the widget, AI Agent, keyword fallback, inbox, Flows, tickets and Android app for two websites, plus an API and webhooks for custom work. Many developers prototype in Python to understand the ideas, then connect their own systems to a hosted chat through webhooks. Start on livechatbot.in to compare.
Exercises
Ideas to extend your AI chatbot Python project
If you’re learning, these extensions build useful skills.
- Per-visitor history. Store each visitor’s conversation in a dictionary keyed by an ID, and trim to the last ten messages.
- A web interface. Wrap the answer function in a small Flask or FastAPI endpoint and call it from a simple HTML page.
- Handover simulation. If the bot says it’s unsure twice, print “Passing to the team” and save the conversation to a file for a person to read.
- Test suite. Write twenty test questions with expected key facts, and check each AI answer contains the right number or phrase.
- Cost tracking. Record how many calls you make per day and estimate cost from your provider’s pricing page.
- Language test. Try Hinglish and regional-language questions and see how well the bot follows “reply in the visitor’s language”.
Each exercise mirrors a real feature of production chat systems, so you’ll understand them far better afterwards.
Frequently asked questions
Can I build an AI chatbot in Python without an API?
Yes, a keyword or retrieval bot like step 1 runs entirely locally. For natural, flexible answers you’ll usually use a language model via an API or a locally hosted model.
Which Python library is best for an AI chatbot?
Use your AI provider’s official SDK for model calls, and standard tools like Flask or FastAPI for a web endpoint. Libraries matter less than good instructions and facts.
How do I stop the bot making things up?
Ground it: put your facts in the instructions, tell it to answer only from them, and to say when it’s unsure. Then test with questions outside the facts.
Is this ready for a real website?
Not on its own. A real deployment needs a widget, storage, a team inbox, handover, monitoring and privacy controls.
What if the AI API is down?
Use a fallback, like step 3, that answers from stored replies and logs the failure for you to fix.
Keep reading
Related guides
AI Chatbot Online Free for Customer Feedback: Collect Opinions, Ideas and Reviews Through Your Website Chat
AI Chatbot Meaning, Word by Word: A Buyer’s Dictionary of 30 Terms You’ll Meet When Choosing One
AI Chatbot Maker or Build Your Own? An Honest Make-vs-Buy Guide for Small Teams and Developers
Prototype in Python, compare with a finished system
See grounding, fallback, inbox and handover working together, free on livechatbot.in.