ChatterBot Python: Moving to an LLM Backend
Last updatedchatterbotapi.com
ChatterBot Python projects answer by matching your input to stored phrases, so they cannot write anything new. Swapping the engine for a hosted LLM keeps your code shape but gives you real, flexible answers.

Summary
- ChatterBot selects replies from a trained corpus; an LLM generates them, so it can handle questions you never trained.
- A small class with a get_response method lets you keep the rest of your app unchanged.
- The API is OpenAI-compatible: base URL https://api.chatterbotapi.com/v1, model uncensored, pay per token.
- A hybrid with exact FAQ answers first and the LLM as fallback keeps cost and risk low.
How ChatterBot works, and where it stops
If you landed on this ChatterBot Python migration guide, you probably have a bot that works but feels stiff. That is by design. The ChatterBot library, an open-source Python project, trains on pairs of statements and, when you send a message, picks the stored response whose trigger best matches it. It stores them in a database, SQLite by default. It does not write sentences. If nobody trained an answer, it returns the nearest thing it has, which is often off-topic.
That makes it handy for fixed FAQ-style bots, and limiting for everything else. It cannot follow a long conversation, stay in a persona, adapt tone, or handle a question phrased in a new way. It also needs training data you must collect and curate. Library maintenance and supported Python versions change over time, so check the project's repository for current compatibility before you build on it.
A hosted language model flips the approach: the model already knows how to write, and you steer it with a system prompt and the conversation so far.
Your existing code
A typical ChatterBot script looks like this:
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
bot = ChatBot("Helper")
trainer = ListTrainer(bot)
trainer.train([
"Hi",
"Hello, how can I help?",
"What are your opening hours?",
"We are open 9 to 5, Monday to Friday.",
])
print(bot.get_response("when do you open"))
Everything your app needs from the bot is one call: get_response(text). To migrate with minimal changes, keep that interface and replace what is behind it.
The replacement: same method, new engine
The service behind this site is an OpenAI-compatible API for one open-weight model, model ID uncensored. It is not an official product of any model vendor. Install the official client with pip install openai, get a key from the API key page, and drop in this class:
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.chatterbotapi.com/v1",
api_key=os.environ["CHAT_API_KEY"],
)
class LLMBot:
def __init__(self, name, system, max_turns=12):
self.name = name
self.system = system
self.max_turns = max_turns
self.history = []
def get_response(self, text):
self.history.append({"role": "user", "content": text})
messages = [{"role": "system", "content": self.system}]
messages += self.history[-self.max_turns * 2:]
r = client.chat.completions.create(
model="uncensored",
messages=messages,
temperature=0.7,
)
answer = r.choices[0].message.content
self.history.append({"role": "assistant", "content": answer})
return answer
bot = LLMBot(
"Helper",
"You are a helpful assistant for a small shop. Opening hours: 9 to 5, Monday to Friday. "
"Answer briefly and say so when you do not know.",
)
print(bot.get_response("when do you open"))
print(bot.get_response("and on Saturday?"))
The second question works because the class resends the history. ChatterBot would have treated "and on Saturday?" as a standalone sentence. The returned value is a plain string, so if your code printed the object or took its text, no change is needed.
Move the training data into the prompt
Your old training list is not wasted. Facts that you trained one pair at a time become lines in the system prompt, and the model combines them freely. Keep the list short and factual. For larger knowledge, such as a hundred FAQ entries or a long policy text, paste what is relevant into the prompt for each request. The context window is 100,000 tokens, shared by prompt and completion, so there is room for a lot.
Persona and tone live in the same place. Where ChatterBot gave you whatever your corpus said, you can now write "answer like a calm night-shift engineer, two sentences maximum" and get that. For roleplay and character bots, the site's Character AI API alternative article goes deeper.
The model is tuned to answer without content refusals for lawful adult use. If your bot is public, add your own checks on input and output, since you are responsible for how it is used. Sexual content involving minors is never allowed.
A hybrid: exact answers first, LLM as the fallback
Not every question needs a model. Fixed answers with legal or business weight, such as prices or refund rules, should stay exact. The cheapest, safest design keeps a lookup for those and uses the model for the rest.
EXACT = {
"opening hours": "We are open 9 to 5, Monday to Friday.",
"refund policy": "Refunds are possible within 14 days with a receipt.",
}
def reply(text):
key = text.lower().strip(" ?.")
for phrase, answer in EXACT.items():
if phrase in key:
return answer
return bot.get_response(text)
This keeps the answers you must control deterministic, and the model handles small talk and odd phrasings. Log questions that fall through to the model; frequent ones are candidates for new exact entries.
Testing the swap before you switch
Run the old and new bots on the same inputs before you replace anything in production. Take the questions from your logs, 30 to 50 of them, and print both answers side by side. You are looking for three things: answers that are now wrong because the model lacks a fact (add it to the system prompt), answers that are too long or too chatty (tell the model to be brief), and questions the old bot handled with a fixed reply that you want to keep exact (move them to the lookup table).
Here are the issues that come up most often in migrations:
- Latency. A call to a hosted API takes longer than a local database lookup. If your interface blocks while waiting, stream the reply with
stream=Trueso text shows up as it arrives. - Invented facts. A language model fills gaps with plausible text. Tell it to say "I do not know" when the prompt has no answer, and keep critical facts in the lookup table.
- Shared state. The
LLMBotclass keeps one history. For a website with many visitors, create one instance per session, or store histories by session ID. - Limits. A full key allows 8 concurrent requests, so queue requests if many visitors write at once. On HTTP 429, wait five seconds and retry.
When you are confident, switch the import and leave the old ChatterBot code in version control for a while. Rolling back is then a single commit. The pricing page shows how to top up when the trial credit runs out.
Where the old approach still wins
If your bot has twenty fixed questions, runs offline, and must never say anything outside an approved list, the old library is a fair fit and a model adds risk without benefit. The move to an LLM pays off when the questions vary, when conversations have more than one turn, or when the bot needs a voice.
Cost and limits
ChatterBot costs nothing per message, but you host and maintain it. The hosted API charges per token: $0.15 per 1M input and $0.50 per 1M output, from prepaid credit, with failed or refused requests not charged. Suppose a shop bot gets 1,000 messages a day, each with about 1,500 tokens in (system prompt plus history) and 200 out. Per message that is 1,500 x $0.15 / 1M = $0.000225 plus 200 x $0.50 / 1M = $0.0001, so $0.000325. A day costs $0.325 and a 30-day month $9.75.
New users can claim a $0.50 trial on the key page, valid for 7 days. Top-ups run from $20 to $500 in crypto only (USDT on TRC20 or USDC on Base), with bonus credit from $50. A full key allows 8 concurrent requests and 300 per minute; on a 429 response, wait and retry. See the pricing page and the docs for details.
More guides
Common questions
Can I keep using ChatterBot for some answers?
Yes. A hybrid works well: keep exact answers for fixed questions and send everything else to the LLM through get_response.
Will my old training data still be useful?
Yes, as source material. Turn the facts into lines in the system prompt, or paste the relevant part into the prompt per request.
Does the bot remember the conversation?
Only what you send. The API is stateless, so the class above keeps a history list and sends the last turns with each call.
Which model ID and base URL do I use?
Use the model ID uncensored and the base URL https://api.chatterbotapi.com/v1 with the official openai package.