How to Build a Simple Chatbot with an LLM API and Your Own Data

A chatbot with an LLM API can answer questions from your own documents instead of guessing from general training data. You send a user question to the model, add the passages that matter, and get an answer grounded in material you control. The setup is smaller than most people expect: a handful of files, one API key, and a search step over your content.

This guide walks through a simple version you can build in a weekend. It covers how the pieces fit together, how to prepare your data, and how to keep answers honest. You do not need a machine learning background, and you do not need to train a model. You need a clear use case, clean source material, and a habit of testing with real questions.

What a chatbot with an LLM API actually does

The model itself is only one part of the system. The API takes a prompt and returns text. Your code decides what goes into that prompt: the user question, a short set of instructions, and the most relevant passages from your documents. The quality of the answer depends far more on those inputs than on picking the newest model.

Think of the chatbot as three layers. The interface collects the question and shows the answer. The retrieval layer finds passages that might contain the answer. The model turns those passages into a reply in plain language. If any layer is weak, the whole bot feels weak, even when the model is strong. A clear system prompt, a small set of trusted documents, and a short feedback loop beat a large model fed with messy data.

Start with your data, not the model

Most chatbot projects fail on data, not on model choice. Before you write code, gather the documents your bot should use and read them the way a new support agent would. Look for outdated pages, conflicting instructions, and gaps where no document answers the question at all. Fix the source first, or the bot will repeat the same errors faster than a human would.

Keep the first collection small, perhaps 20 to 50 pages that cover the questions people actually ask. Support tickets, onboarding guides, and product docs are good starting points. Remove duplicates and mark which documents are current. If two pages disagree, decide which one wins and note it in the source file, so the retrieval step does not surface both as equals.

Split long documents into chunks of a few hundred words, with each chunk able to stand on its own. A chunk that starts mid-sentence or depends on a heading three pages back will confuse the model. Keep the title or section name with each chunk, and keep a link back to the original page so the bot can cite its source. A related walkthrough shows how to build a chatbot that answers from your own documents, which follows the same prepare-first pattern.

Retrieval augmented generation in plain terms

Retrieval augmented generation, often shortened to RAG, is the technique that connects your documents to the model. When a question arrives, the system searches your chunks for the ones closest in meaning, then pastes the best few into the prompt. The model answers from those passages instead of from memory alone. If no passage matches, the bot should say it does not know rather than invent an answer.

To search by meaning, you turn each chunk into an embedding, a list of numbers that captures what the text is about. Similar texts get similar numbers, so a question about refunds finds the refund policy even when it uses different words. You can compare embedding models before you pick one, but for a first version any mainstream embedding model will do. Consistency matters more than squeezing out the last point of accuracy: use the same model for your documents and for incoming questions.

Store the embeddings in a vector database, which is built to find the nearest matches quickly. For a small project, a local vector store or a lightweight hosted option is enough. You do not need a dedicated cluster for a few thousand chunks. Keep the original text next to each embedding, because the model needs the words, not the numbers. If you want a hands-on path, this guide to build a RAG pipeline in Python covers chunking, embeddings, and retrieval in working code.

Build it in five steps

The build itself is straightforward once the data is ready. Keep each step small and test it before moving on, so a bad answer points to one layer instead of the whole system.

  • Collect and clean the source documents. Pick one topic, remove duplicates, and fix outdated pages. Aim for a small set you trust.
  • Chunk the text and create embeddings. Split documents into short, self-contained passages and store each one with its source link.
  • Add a retrieval step. For each question, fetch the three to five most relevant chunks and pass them to the model with the question.
  • Write the system prompt. Tell the model to answer only from the supplied passages, cite its sources, and say when the answer is not in the material.
  • Ship a simple interface. A single web page with a text box is enough for the first version. Add history, feedback buttons, and admin controls only after people use it.

Before you choose a provider, compare LLM APIs on the factors that affect a small project: response quality on your own test questions, latency, rate limits, and how the provider handles your data. Prices change often, so check official pricing before you commit to a budget, and set a spending cap on the API key from day one.

Approach Best for Trade-off
Keyword search Exact terms, error codes, product names Misses paraphrased questions
Embedding search Questions asked in natural language Needs an embedding model and a vector store
Fine-tuning Consistent tone or format at scale Costs more and does not add new facts

Prompt engineering that keeps answers grounded

Prompt engineering for this kind of bot is mostly about limits, not clever wording. The system prompt should name the role, list the passages, and state the rules: use only the supplied material, quote or link the source, and admit when the material does not cover the question. Short rules work better than long lists, because the model follows a few clear constraints more reliably than a page of instructions.

Include two or three example exchanges in the prompt if the tone matters, for instance a short answer with a source link. Keep the conversation history short as well. Sending the full chat history on every turn burns tokens and lets old, wrong answers linger. Keep the last few exchanges, summarize older ones, or start fresh when the topic changes.

Guardrails, cost, and privacy

A chatbot that answers from your own data still needs guardrails. Filter inputs for prompt injection, where a user tries to make the bot ignore its instructions, and filter outputs for personal data that should never appear in a reply. Keep the API key on the server, never in browser code, and rotate it if it ever leaks. The same grounding and guardrails pattern that reduces invented answers also helps here: cite sources, cap the retrieved passages, and flag low-confidence replies for human review.

Cost is easy to control at this scale. The main drivers are the number of tokens in each prompt, which grows with the number of retrieved chunks, and the number of questions per day. Retrieve three to five chunks instead of ten, trim the system prompt, and cache answers for questions that repeat. Set usage alerts and a hard spending cap, and check the bill weekly during the first month.

Privacy deserves the same attention. Strip personal data from logs and source material, check the provider terms for data retention and training use, and keep sensitive documents out of the first version until a privacy review clears them.

Test before you ship

Build a test set of 30 to 50 real questions before launch, drawn from support tickets, search logs, and questions your team hears often. Include edge cases: questions with no answer in the documents, questions with two plausible answers, and questions phrased in slang or with typos. Run the set after every change to the prompt, the chunks, or the model, and record which answers changed.

Score each answer on three points: is it correct, does it cite the right source, and would a new user understand it. Ask a colleague who did not build the bot to try breaking it, because fresh users find the gaps the builder walks past.

Ship to a small group first and watch the logs daily. Most bad answers trace back to a missing page, a stale chunk, or a vague passage, not to the model, so fix the source and the bot improves with it.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.