AI & Automation

Building AI-powered chatbots with LangChain and GPT-4

Friendly AI robot representing an AI-powered chatbot

Build chatbots that reason, use tools and keep context — a complete guide.

Key takeaways
  • A useful business chatbot answers from your own data — product details, prices, policies — using retrieval-augmented generation (RAG).
  • LangChain provides building blocks for prompts, retrieval, tools and memory; LangGraph helps with multi-step agents.
  • Guardrails, evaluation and human handover matter as much as the model you choose.
  • Start with one use case, measure it, then expand to WhatsApp, more languages and more tasks.

Why businesses are building their own AI chatbots

Customers expect instant answers, at any hour, on the channel they prefer. Large language models such as OpenAI’s GPT-4-class models make it possible to build assistants that understand natural questions in English, Hindi and Hinglish — and reply like a helpful team member.

But a generic chatbot that “knows the internet” isn’t enough. Your customers want answers about your products, prices, delivery areas and policies. That’s where LangChain and retrieval come in.

How an LLM chatbot actually works

  1. The user asks a question on your website or WhatsApp.
  2. Retrieval searches your knowledge base — FAQs, product sheets, policies — for the most relevant passages.
  3. The prompt combines the question, the retrieved passages and instructions on tone and rules.
  4. The model writes an answer grounded in that context.
  5. Tools (optional) let the bot check order status, book appointments or create a lead in your CRM.

Building blocks in LangChain

  • Prompt templates — reusable instructions with placeholders for context and questions.
  • Document loaders and splitters — turn PDFs, web pages and spreadsheets into searchable chunks.
  • Embeddings and vector stores — store those chunks so the most relevant ones can be found quickly.
  • Retrievers — fetch the right context for each question.
  • Tools and agents — let the model call your APIs; LangGraph adds stateful, multi-step workflows.
  • Memory — keep the conversation context so users don’t repeat themselves.

A minimal example

Here’s the core idea in a few lines of Python: a prompt that tells the model to answer only from the context you provide.

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate

llm = ChatOpenAI(model="gpt-4o", temperature=0)

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are SpyCoder's support assistant. "
               "Answer only from the context. If unsure, offer to connect a human.\n\n"
               "Context:\n{context}"),
    ("human", "{question}"),
])

chain = prompt | llm
answer = chain.invoke({"context": docs_text, "question": "Do you build Shopify stores?"})
print(answer.content)

In production, docs_text comes from a retriever that searches your vector store, and the chain is wrapped with logging, rate limits and error handling.

Real-world use cases for Indian businesses

  • Retail and e-commerce: product questions, size guides, delivery areas and order tracking.
  • Clinics and hospitals: timings, doctor availability and appointment booking.
  • Schools and coaching institutes: admissions, fees, batch timings and documents required.
  • Manufacturers: product specifications, minimum order quantities and quotation requests.
  • Real estate: project details, site-visit bookings and lead qualification.
  • Internal teams: an assistant that answers HR, policy and process questions from company documents.

In each case the pattern is the same: the bot handles repetitive questions instantly and passes qualified leads or complex cases to a person.

Guardrails: keeping answers accurate and safe

  • Instruct the model to answer only from retrieved context and say when it doesn’t know.
  • Filter sensitive topics and personal data before anything reaches the model.
  • Set a clear handover to a human for complaints, payments and complex cases.
  • Log conversations (with consent) and review them regularly.
  • Build a small evaluation set of real questions and check answers after every change.

Connecting the chatbot to WhatsApp and your CRM

Most Indian customers prefer WhatsApp. Using the WhatsApp Business API, the same chatbot can answer questions, capture leads and send updates there. Connect it to your CRM so every conversation creates or updates a lead, and your sales team picks up exactly where the bot left off.

Controlling cost and performance

Use a smaller, faster model for simple questions and a larger model only when needed. Cache common answers, keep prompts concise, and set monthly usage limits. A well-designed chatbot stays fast and its running cost stays predictable.

Getting started

Pick one high-volume use case — for example, answering product and delivery questions — and launch a pilot in a few weeks. Measure how many questions it resolves and how many leads it captures, then expand.

SpyCoder builds production chatbots and automations through our AI & automation services. You can also hire an OpenAI specialist engineer to work with your team.

Need help with this?

Talk to the SpyCoder team for a free consultation and a fixed-price proposal within 48 hours.

Get a free quote
FAQs

Frequently asked questions

Still have a question? Call +91 85050 73668.

LangChain is a framework for building applications with large language models. It provides building blocks for prompts, retrieval from your own documents, tools, memory and agents.

Ready to start?

Let’s build something extraordinary.

Tell us what you need. You’ll get a free consultation and a detailed, fixed-price proposal within 48 hours — no commitment.

Get a free quote

✓ No commitment ✓ Free consultation ✓ 100% confidential