Introduction

If you are building AI-powered applications today, you might have noticed that calling an AI model directly is not always enough. You often need memory, data handling, workflows, and structured outputs. This is where LangChain becomes very useful.

LangChain helps developers build smarter AI applications by connecting large language models like OpenAI with tools, data, and logic. Instead of just asking questions, you can create applications that think, remember, and act.

In this guide, you will learn in simple words how to use LangChain with OpenAI to build custom AI applications step by step.

What is LangChain in Simple Terms?

LangChain is a framework that helps you build advanced AI applications using language models.

Instead of directly sending a prompt to OpenAI, LangChain allows you to:

Real-life example:

Without LangChain → You ask a question, AI replies, and forgets everything
With LangChain → AI remembers your conversation and gives better answers

Why Use LangChain with OpenAI?

When you combine LangChain with OpenAI, you unlock powerful capabilities for modern AI applications.

Here’s why developers prefer this combination:

Example:

Imagine building a customer support chatbot for a website in India. Instead of generic answers, your chatbot can read company documents and answer users accurately.

Prerequisites

Before starting, make sure you have:

Step 1: Setup Node.js Project

Create a new project folder and initialize it:

npm init -y

This creates a package.json file which manages your project.

Step 2: Install Required Packages

Install LangChain and OpenAI:

npm install langchain openai dotenv

Why these packages?

Step 3: Setup Environment Variables

Create a .env file:

OPENAI_API_KEY=your_api_key_here

This keeps your API key safe.

Step 4: Basic LangChain + OpenAI Setup

Now let’s write basic code.

import "dotenv/config";
import { ChatOpenAI } from "langchain/chat_models/openai";

const model = new ChatOpenAI({
  temperature: 0.7,
  openAIApiKey: process.env.OPENAI_API_KEY,
});

const response = await model.invoke("Explain Node.js in simple words");

console.log(response.content);

Explanation in simple words:

This is your first LangChain + OpenAI integration.

Step 5: Add Prompt Templates

Prompt templates help you structure input better.

import { PromptTemplate } from "langchain/prompts";

const prompt = new PromptTemplate({
  template: "Explain {topic} in simple words",
  inputVariables: ["topic"],
});

const formattedPrompt = await prompt.format({ topic: "AI Chatbots" });

const result = await model.invoke(formattedPrompt);
console.log(result.content);

Why this matters:

Instead of hardcoding prompts, you can reuse templates dynamically.

Step 6: Add Memory to Chatbot

Memory makes your chatbot smarter.

import { ConversationChain } from "langchain/chains";
import { BufferMemory } from "langchain/memory";

const memory = new BufferMemory();

const chain = new ConversationChain({
  llm: model,
  memory: memory,
});

await chain.call({ input: "Hi, my name is Rahul" });
await chain.call({ input: "What is my name?" });

Now the chatbot remembers your name.

Real-life example:

Like a human conversation, the AI does not forget previous messages.

Step 7: Connect External Data (Documents)

One powerful feature is connecting your own data.

Example use case:

Basic idea:

Simple understanding:

Instead of guessing answers, AI reads your data and responds.

Step 8: Build AI Agent

Agents allow AI to take actions.

Example:

import { initializeAgentExecutorWithOptions } from "langchain/agents";

const executor = await initializeAgentExecutorWithOptions([], model, {
  agentType: "zero-shot-react-description",
});

const result = await executor.run("What is the current weather in Delhi?");
console.log(result);

Agents make your application more dynamic and powerful.

Step 9: Real-World Application Ideas

Here are some practical AI applications you can build:

Example:

An Indian e-commerce site can use AI chatbot to answer product queries instantly, improving customer experience.

Step 10: Best Practices

To build a better AI application:

Advantages and Disadvantages

Advantages

Disadvantages

Summary

Using LangChain with OpenAI allows you to move beyond simple AI prompts and build fully functional, intelligent applications that can remember conversations, use external data, and perform actions. By following a structured approach—starting from setup, adding prompt templates, memory, and agents—you can create powerful real-world AI solutions for websites, businesses, and personal projects. As AI adoption is growing rapidly in India and globally, learning this combination gives you a strong advantage in building modern, scalable, and smart applications.