How to Make an AI Chatbot
Learn how to plan, build, and test an AI chatbot, from choosing a model and connecting it to your data to selecting a deployment option.
Overview
To make an AI chatbot, define a narrow user need, choose a model and application approach, connect the model to only the data or actions it needs, then test the complete experience before release. The chatbot is a software application—not just a prompt. Its quality and safety depend on the model, user interface, backend logic, information retrieval, permissions, and ongoing monitoring.
For a small prototype, a hosted model API or visual builder can avoid operating model infrastructure. For answers based on private or frequently changing information, add retrieval-augmented generation (RAG). For actions such as checking an order or booking an appointment, connect tightly scoped application tools. Fine-tuning is usually a later option for recurring behavior or formatting problems, not a way to keep changing facts up to date.
What an AI chatbot is
A chatbot is an application that conducts a conversation through text, voice, or another interface. Earlier systems often matched keywords or followed scripted decision paths. Modern generative chatbots commonly use a large language model (LLM) to interpret a message and generate a response. The surrounding software supplies conversation context, retrieves relevant information, validates actions, and controls what the model can access.
Joseph Weizenbaum's ELIZA—A Computer Program For the Study of Natural Language Communication Between Man and Machine | Communications of the ACM, described a program that used transformation rules to simulate natural-language dialogue. The modern architecture is different: an LLM generates text from learned patterns and current input, while application code can supply external documents or tools. A 2020 research paper on Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks formalized an approach combining a pretrained language model with a retriever and external index for knowledge-intensive tasks.
A generative chatbot is not automatically the right design. If the task is a short, fixed process—such as collecting a few fields in a predictable order—a form, rules-based bot, or scripted flow may be easier to test and more deterministic. Use an LLM when natural-language variation, synthesis, or flexible question answering adds value that a simpler interface cannot provide.
Choose the build approach
The main options differ in how much of the application and model-serving infrastructure you operate. A no-code builder can be useful for validating a simple workflow, but check its data handling, integration limits, evaluation support, and ability to export or migrate the application before relying on it in production.
| Approach | Best fit | Main trade-off |
|---|---|---|
| Visual or no-code builder | A quick prototype or standard support flow | Fast to configure; less control over retrieval, permissions, and custom behavior |
| Hosted model API | A custom application without operating model-serving infrastructure | Simplifies inference operations; introduces provider dependency and data-handling questions |
| Self-hosted or open-weight model | Specific deployment, control, or data-residency needs | Greater control, but the team operates serving, scaling, updates, and security |
| Scripted or rules-based bot | Narrow tasks with predictable paths and exact outcomes | Easy to constrain; less flexible with varied language or unexpected requests |
No model or hosting option is best for every chatbot. Compare candidates against the same representative prompts, safety requirements, response-time target, and expected workload. Check current provider terms and pricing rather than relying on a cost estimate from a different task.
Start with a defined use case
Write down who will use the chatbot, what tasks it should complete, what information it may use, and what it must not do. “Answer customer questions” is too broad to test. A more useful initial scope might be: “Answer questions about the current return policy using approved policy documents, cite the relevant section, and offer a human support channel if the answer is not supported.”
Define an outcome that can be observed, such as correctly resolving a support question, routing a request to the correct team, or completing a form without asking for unnecessary information. Also specify the fallback: the chatbot should be able to ask for clarification, say it cannot answer from its sources, or hand the interaction to a person.
Core architecture
A custom chatbot usually has a client-facing interface, a backend that manages conversations, a model endpoint, and optional retrieval or tool integrations. Keep credentials and privileged services in the backend; a browser or mobile client should not receive secret API keys. The backend should control the model's context, validate requests, apply access rules, and decide whether a proposed action is permitted.
Figure: Typical request flow for an AI chatbot. The backend mediates model access to data and actions; retrieval and tools are optional, and should be added only when the use case requires them. The model can propose a tool call, but application code—not the model—executes the operation. This pattern is described in Function calling | OpenAI API and in Google's RAG infrastructure for generative AI using Agent Platform and Vector Search | Cloud Architecture Center | Google Cloud Documentation.
How to build an AI chatbot: development sequence
1. Specify requirements and boundaries
List common user requests, expected responses, unsupported topics, escalation conditions, languages, channels, and any accessibility requirements. For systems that handle personal, financial, health, or business-sensitive information, map where that data will travel and who can access it. Establish retention and deletion rules before collecting conversation history.
Separate low-risk informational responses from consequential actions. A chatbot that explains a refund policy has a different risk profile from one that issues refunds. For a consequential action, require an authenticated user, apply business rules in ordinary application code, and consider a confirmation step or human approval.
2. Choose a model and interface
A hosted model can simplify setup: the application sends a request to a provider's API and displays its response. A self-hosted model can give the operator more control over deployment, but requires the operator to manage inference infrastructure, capacity, patching, and availability. In either case, assess output quality on representative examples instead of selecting only by model size or public claims.
A minimal prototype can use a chat interface, a backend endpoint, and a model API. The backend sends the user message and a short instruction defining the bot's role and boundaries; it then returns the model response to the interface. Do not place API secrets in client-side code. Keep each interaction limited to the context the model needs rather than sending an unlimited conversation history.
3. Prepare the chatbot's knowledge
If the bot only needs general conversation, a model plus clear instructions may be sufficient. If it must answer from a company's policies, product manuals, or other approved materials, prepare those materials for retrieval. Remove obsolete or duplicate files, preserve source titles and URLs, divide long documents into coherent sections, and ensure users are authorized to see the information that search can return.
A RAG pipeline typically has two flows:
- Ingestion: parse approved material, divide it into passages, represent passages in a searchable index, and refresh or remove entries when source documents change.
- Answering: search for passages relevant to the user's question, combine the question and selected passages as model context, generate an answer, and return citations or links when available.
Search may use keywords, semantic or vector similarity, or a hybrid of these methods. A vector representation can find related text that does not repeat the exact query words, while keyword matching can be useful for exact names, identifiers, and phrases. Google's tutorial describes creating document embeddings and storing them in a vector database before answering questions through semantic search; Microsoft's Retrieval augmented generation (RAG) and indexes in Microsoft Foundry - Microsoft Foundry | Microsoft Learn describes keyword, vector, semantic, and hybrid retrieval options.
RAG improves access to selected information; it does not guarantee a correct answer. The index may retrieve the wrong passage, omit an important one, or return stale information. The model may also misread or contradict the retrieved text. Test retrieval quality independently from the generated answer, retain source metadata, and instruct the bot to acknowledge when the available material does not answer the question. Microsoft specifically cautions that errors can remain even when an answer is grounded in retrieved content.
4. Add application tools only when needed
A model can be given descriptions of functions such as lookup_order, check_appointment_slots, or create_support_ticket. The model returns a proposed function name and structured arguments. The application validates those arguments, checks the user's identity and permissions, executes the function, and can provide the result to the model to phrase a response.
This is distinct from letting the model directly access a database or execute arbitrary code. Define a small set of tools with explicit input schemas and narrow permissions. Validate identifiers and values on the server, enforce authorization in application code, set timeouts and usage limits, and require confirmation for actions that are irreversible or have financial or other material effects. Structured output can make the tool input easier to validate, but it does not replace authorization checks.
5. Manage conversation state deliberately
A chatbot may include recent turns in each model request so it can understand follow-up questions. Conversation history is not automatically the same thing as persistent memory: decide whether to keep it, for how long, and whether the user can inspect or delete it. Summarize older turns or retain only relevant details if context size or privacy requires it. Do not store passwords, access tokens, or other secrets as conversational memory.
6. Write and refine instructions
State the bot's role, audience, tone, allowed sources, limits, and escalation behavior in plain, testable language. For a knowledge assistant, instructions might say to answer using retrieved company policy, cite the source, ask a clarifying question when needed, and say when the source does not contain an answer. Instructions improve consistency but are not an access-control or security boundary; enforce sensitive restrictions in backend logic.
RAG, fine-tuning, and prompting solve different problems
These methods are not necessarily successive levels of sophistication. Select one based on the observed error. OpenAI's accuracy guidance describes prompt engineering, context optimization, and model optimization as distinct levers, and shows that adding retrieval can reduce quality if irrelevant context introduces noise in a behavior-focused task.
| Method | Changes | Use when | Limitation |
|---|---|---|---|
| Prompting | Instructions and examples supplied at request time | The behavior can be guided with clear instructions | Does not supply missing or current facts by itself |
| RAG | External passages retrieved and placed in the model context | The answer needs private, sourceable, or frequently updated knowledge | Retrieval can be incomplete, irrelevant, stale, or permission-inappropriate |
| Fine-tuning | Model behavior through additional training on examples | The model needs more consistent style, format, or task behavior | Requires representative training and evaluation data; changing facts need a separate update path |
| Tools or functions | Application capabilities exposed through constrained calls | The chatbot must query current systems or perform a defined action | Adds authorization, validation, and operational risk |
Microsoft's Retrieval augmented generation (RAG) and indexes in Microsoft Foundry - Microsoft Foundry | Microsoft Learn recommends retrieval for private or frequently changing information and fine-tuning for behavior, style, or task performance. OpenAI likewise recommends evaluating a prompt baseline first and choosing between context changes and model changes based on why the answer failed. A system can combine retrieval and fine-tuning, but the combination adds components that each need testing and maintenance.
Test before release
Create a test set from real or carefully constructed examples that represent how intended users will speak. Include misspellings, ambiguous requests, follow-up questions, requests outside the bot's scope, conflicting source documents, and cases where no answer should be given. Keep test examples separate from training or tuning examples when possible, so changes can be assessed against unseen cases.
For an informational chatbot, assess factual accuracy, source support, citation correctness, completeness, and whether it appropriately declines unsupported questions. For a task-oriented chatbot, measure task success, number of turns, errors, and successful handoff. For open-ended conversation, have people judge whether answers make sense, respond to context, and meet the expected tone. Peer-reviewed work on dialogue-system evaluation distinguishes task-oriented systems, which have measurable success conditions, from open-ended dialogue, where appropriateness is harder to automate; it also notes that human assessment remains necessary for many qualities.
| Test area | Example check |
|---|---|
| Normal requests | Does the answer address the user’s actual question? |
| Grounding | Does each factual claim match the retrieved source and citation? |
| Unknown answers | Does it admit the source is insufficient rather than inventing a policy? |
| Multi-turn use | Does a follow-up retain relevant context without importing unrelated details? |
| Tool safety | Does it reject invalid arguments and unauthorized actions? |
| Adversarial input | Do direct or document-embedded instructions fail to override application policy? |
| Performance | Are response time, availability, and usage within defined limits? |
Use automated checks for objective properties such as schema validity, required citations, task completion, and response limits. Use human reviewers for qualities that require judgment and for high-impact or sensitive use cases. A model-based evaluator can help compare outputs, but it is another model and should be checked against human judgments. Run the same tests after changing the model, prompt, retrieval settings, source data, or tool behavior. The Working with evals | OpenAI API describes evaluating model outputs against test data; broader dialogue evaluation literature emphasizes that measures should match the system's purpose.
Security, privacy, and responsible use
Treat prompts and retrieved material as untrusted input
Prompt injection occurs when instructions in a user message or external content attempt to alter the model's intended behavior. In a RAG system, a malicious instruction might appear inside a document or webpage that the chatbot retrieves. OWASP's LLMRisks – OWASP Gen AI Security Project includes prompt injection, sensitive-information disclosure, data and model poisoning, improper output handling, excessive agency, vector and embedding weaknesses, misinformation, and unbounded consumption.
Do not rely on a system prompt alone to prevent unauthorized access. Apply user permissions when retrieving documents and again when executing tools; treat retrieved text as data, not as trusted application policy; validate model output before using it in another system; and prevent the model from making high-impact changes without the application's approval. The Artificial Intelligence Risk Management Framework: Generative Artificial Intelligence Profile identifies risks including confidently stated false content (called confabulation), privacy impacts, harmful bias, human over-reliance, and information-security problems.
Minimize data exposure
Collect only information needed for the task. Decide whether conversation logs are necessary, who can access them, how long they are kept, and how deletion requests are handled. Review what user content is sent to a model provider or other vendors and the terms that apply. Redact sensitive information from logs where possible; protect retained data with access controls and appropriate safeguards. NIST's AI Risk Management Framework is a voluntary framework for incorporating trustworthiness into AI design, development, use, and evaluation; it does not replace applicable legal requirements.
Disclose the system's role and provide recourse
Tell users when they are interacting with an AI chatbot when appropriate, identify what it can and cannot do, and provide an accessible path to a person for unresolved or sensitive cases. The EU AI Act's Article 50: Transparency obligations for providers and deployers of certain AI systems | AI Act Service Desk includes an obligation to inform people when they interact directly with an AI system, unless that is obvious in context, alongside other transparency provisions and exceptions. The applicable rules depend on jurisdiction and use; this example is not a complete legal analysis.
Deploy and maintain the chatbot
Before release, secure model and tool credentials, set request and spending limits, handle timeouts and provider errors, and monitor latency, failed requests, user-reported problems, and task outcomes. Show a clear fallback when the model or an integration is unavailable. Use staged rollout where practical, so the team can detect failures before exposing the chatbot to every user.
For RAG, assign an owner to review document changes, remove outdated material, and verify that access permissions carry through to retrieval. For model or prompt updates, compare results against the fixed test set and review any regression before deployment. Sample production conversations only under an approved privacy and access policy; remove or protect personal data and use reports to identify recurring unanswered questions.
Operating cost depends on the whole application, not only model calls. Account for model input and output, retrieval or search, storage and indexing, hosting, monitoring, and human support. Long conversation histories and unnecessary retrieved passages increase the material processed by the model. Measure cost and latency using the intended traffic and realistic prompts, then control them with limits, concise context, caching where suitable, and a smaller or cheaper model where evaluation shows it is adequate.
Practical conclusion
The simplest reliable way to create an AI chatbot is to begin with a narrow job and a basic model-backed conversation, establish representative tests, and add features only to solve measured shortcomings. Use RAG when answers need approved, changeable information; use application tools when the chatbot must interact with live systems; and consider fine-tuning when consistent behavior remains a problem after prompting and evaluation. A production chatbot is ready only when it meets its task requirements, protects user data, constrains actions, handles uncertainty, and has a plan for testing and maintenance.