Building a Chatbot With Dify and an AI API

By Darus S Alfatah · 2025-09-02 · Tutorial · 8 menit

Building a Chatbot With Dify and an AI API

Author note: In Dify, we place a retrieval step before the model so answers are grounded, not invented.

Start With the Knowledge Base

Chatbot quality is decided mostly by documents, not by the model. Before uploading anything, clean the material: remove expired pages, split long documents by heading, and keep tables intact. Messy input produces messy answers no matter how good the model is.

After the base is ready, test with twenty real customer questions. Record which are answered correctly, which are wrong, and which trigger invented answers. Fix the documents first; eight out of ten problems come from data, not prompts.

Structuring Instructions

Write explicit rules: language, answer length, topics allowed, and what to do when a question falls outside scope. Add escalation triggers, for example “if the customer mentions refund or complaint, hand over to a human”.

Role: support assistant for [shop]
Scope: catalogue, shipping, hours, returns
Limit: max 3 sentences, no promises about delivery dates
Fallback: "Let me connect you to our team"

Handling Missing Information

Instruct the bot to admit gaps. “I do not have that information yet; would you like me to connect you to the team?” is far better than a confident wrong answer, especially about prices and schedules.

Conversation Variables

Store user name, order number, and conversation stage as variables. Then answers can be contextual without resending the whole history, which saves tokens and improves accuracy.

Evaluating Before Launch

Keep twenty fixed questions and their expected answers. Run them after every change to the knowledge base or prompt. This is the cheapest quality control available and it prevents silent regressions.

Frequently Asked Questions

How much data do I need? Often thirty to fifty well-organised documents are enough for a support bot.

Can it answer in local slang? Yes, but set the tone explicitly and test on real customers.

What if the answer changes weekly? Update the knowledge base on a schedule and note the owner.

Is a human still needed? Always, for complaints, negotiations, and anything sensitive.

How do I measure success? Self-service rate, first response time, and satisfaction after the chat.

Conclusion

A good chatbot is mostly clean data and clear boundaries. Keep the knowledge base current, test with real questions, and make handover to humans easy.

Step-by-Step Setup

  1. Collect the twenty questions customers ask most.
  2. Write an official answer for each, in plain language.
  3. Prepare documents and remove duplicates or outdated pages.
  4. Create the app, attach the knowledge base, and set the instructions.
  5. Test with the twenty questions and note every wrong answer.
  6. Fix documents, retest, and only then connect the channel.

This order matters. Teams that connect the channel first end up debugging in front of customers, which damages trust faster than a slow launch does.

Keeping It Healthy Over Time

Review failed conversations weekly and add missing answers to the knowledge base. Assign one owner so updates do not depend on memory. Once a month, read twenty conversations yourself; metrics show what happened, reading shows why.

Track the share of conversations that end with an unanswered question. That number is the clearest signal of where your documentation is thin, and it usually points to policies nobody wrote down.

An example RAG in Dify: Upload product documentation to the knowledge base, enable retrieval, then test with a question whose answer exists only in the document.

Frequently Asked Questions

Can beginners follow this tutorial?

Yes. The steps are explained in order with code examples, suitable for beginners and experienced developers alike.

Is BankAI’s endpoint compatible with the library I already use?

Yes, BankAI is 100% OpenAI-compatible, so libraries such as the OpenAI SDK and LangChain work without code changes beyond the Base URL and API key.

Where do I get an API key to follow this tutorial?

You get the API key (and Secret Token) after registering and completing payment in the BankAI dashboard.

Sources & References

About the author

Darus S Alfatah — Founder & Writer

Darus S Alfatah is the founder of BankAI. He builds and operates the LLM API gateway that serves Indonesian developers, and writes about LLM integration, gateway architecture, and using AI practically and cost-effectively. Every guide is tested in a real environment — including code, configuration, and token calculations — before it is published, and updated when provider documentation or pricing changes.

Editorial policy

Related articles

🚀 Ready to Use the All-in-One API Key?

Sign up, top up with QRIS, and start building with ChatGPT, Claude, DeepSeek and every other model through one key.

🔑 SIGN UP NOW · Pricing