Artificial Intelligence has changed the way we build software.
Today, developers can create applications that can:
- Answer questions
- Generate content
- Summarize documents
- Translate languages
- Write code
- Analyze data
- Build intelligent chatbots
Most of these applications are powered by Large Language Models (LLMs) such as OpenAI GPT and Google Gemini.
While these models are powerful, integrating them directly into applications can become repetitive and difficult to manage as projects grow.
This is where LangChain.js comes in.
LangChain provides a consistent and modular way to build AI-powered applications using different language models and tools.
In this article, we'll understand what LangChain is, why it's useful, and build our first AI application using both OpenAI and Google Gemini.
Let's get started!
What is Artificial Intelligence (AI)?
Artificial Intelligence (AI) is a branch of computer science that enables machines to perform tasks that normally require human intelligence.
Examples include:
- Understanding natural language
- Answering questions
- Recognizing images
- Translating languages
- Generating text
- Writing code
Modern AI applications often rely on language models to understand and generate human-like text.
What is an LLM?
LLM stands for Large Language Model.
An LLM is trained on vast amounts of text data and learns patterns in language. It can generate responses, answer questions, summarize information, and much more.
Some popular LLM providers are:
- OpenAI (GPT models)
- Google (Gemini models)
- Anthropic (Claude)
- Mistral AI
- Groq
- Ollama (local models)
Each provider exposes its own API, but interacting with each one directly can lead to duplicated code.
What is LangChain.js?
LangChain.js is an open-source framework for building AI-powered applications in JavaScript and TypeScript.
Instead of working directly with different AI provider SDKs, LangChain provides a common interface that makes it easier to switch between models and compose AI workflows.
Think of LangChain as a toolkit that helps you connect prompts, models, output parsers, document loaders, retrievers, and more into complete applications.
Why Do We Need LangChain?
Imagine you're building an AI chatbot.
Without LangChain, your application might look like this:
Application
│
▼
OpenAI SDK
│
▼
OpenAI API
If you later decide to use Gemini, you'll need to change parts of your integration.
With LangChain:
Application
│
▼
LangChain
│
├────────► OpenAI
│
├────────► Gemini
│
└────────► Other Providers
Your application code stays mostly the same while the model provider changes.
LangChain Architecture
A typical LangChain application looks like this:
User Input
│
▼
Prompt
│
▼
Language Model
│
▼
Output
As your application grows, you'll introduce additional components such as output parsers, document loaders, retrievers, and tools.
Prerequisites
Before learning LangChain.js, you should be comfortable with:
- JavaScript (ES6+)
- Node.js
- npm
- async/await
- Promises
You don't need prior AI experience to get started.
Create a New Project
Create a new folder:
mkdir langchain-introduction
Move into the folder:
cd langchain-introduction
Initialize a Node.js project:
npm init -y
This creates a package.json file.
Install Dependencies
Install the required packages:
npm install langchain @langchain/openai @langchain/google-genai dotenv
Project Structure
langchain-introduction/
│
├── index.js
├── .env
├── package.json
└── node_modules/
Configure Environment Variables
Create a .env file:
OPENAI_API_KEY=your_openai_api_key
GOOGLE_API_KEY=your_google_api_key
Never hardcode API keys in your source code or commit them to version control.
Load Environment Variables
At the top of your application:
import "dotenv/config";
This loads the values from the .env file into process.env.
Initialize OpenAI
Create an OpenAI model instance:
import { ChatOpenAI } from "@langchain/openai";
const openAIModel = new ChatOpenAI({
model: "gpt-4.1-mini",
temperature: 0.7,
});
Explanation
modelspecifies which OpenAI model to use.temperaturecontrols response creativity. Lower values produce more deterministic responses, while higher values allow more variation.
Initialize Gemini
Create a Gemini model instance:
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
const geminiModel = new ChatGoogleGenerativeAI({
model: "gemini-2.5-flash",
temperature: 0.7,
});
Again, the initialization is very similar, which is one of the strengths of LangChain.
Your First AI Application (OpenAI)
Create a file named index.js:
import "dotenv/config";
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({
model: "gpt-4.1-mini",
});
const response = await model.invoke(
"What is Node.js?"
);
console.log(response.content);
Run the application:
node index.js
Example output:
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine...
Congratulations! 🎉 You've built your first LangChain application.
Your First AI Application (Gemini)
Now switch to Gemini.
import "dotenv/config";
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
const model = new ChatGoogleGenerativeAI({
model: "gemini-2.5-flash",
});
const response = await model.invoke(
"What is Node.js?"
);
console.log(response.content);
Run the file:
node index.js
The application works the same way. Only the model changes.
Understanding the Response Object
The invoke() method returns an AI message object, not just a plain string.
const response = await model.invoke(
"Hello"
);
console.log(response);
You'll notice that the response contains metadata in addition to the generated text.
To access only the generated content:
console.log(response.content);
In most applications, you'll work with response.content.
Why This Is Powerful
Notice how similar the code is:
OpenAI:
const model = new ChatOpenAI({
model: "gpt-4.1-mini",
});
Gemini:
const model = new ChatGoogleGenerativeAI({
model: "gemini-2.5-flash",
});
The rest of your application remains almost identical. This provider-agnostic approach is one of the biggest advantages of LangChain.
Real-World Use Cases
LangChain is commonly used to build:
- AI Chatbots
- Customer Support Assistants
- AI Code Assistants
- Document Search Applications
- Resume Analyzers
- Content Generators
- AI Research Tools
- Retrieval-Augmented Generation (RAG) Systems
- AI Agents
We'll build several of these applications throughout this series.
Best Practices
When starting with LangChain:
- Store API keys in environment variables.
- Keep model configuration separate from business logic.
- Start with simple examples before introducing advanced concepts.
- Choose the model that best fits your use case and budget.
- Read the provider's documentation for model capabilities and limitations.
Common Beginner Mistakes
Here are some common mistakes to avoid:
- Hardcoding API keys.
- Using outdated LangChain examples from older blog posts.
- Forgetting to load environment variables.
- Expecting the response object to be a plain string.
- Trying to learn advanced concepts like Agents or RAG before understanding the basics.
Final Thoughts
LangChain.js provides a consistent, modular way to build AI applications. Instead of writing provider-specific code for each language model, you can use a unified API that works across multiple providers.
In this article, you learned:
- What AI and LLMs are
- What LangChain.js is
- Why LangChain is useful
- How to set up a LangChain project
- How to integrate OpenAI
- How to integrate Google Gemini
- How to build your first AI application
- How to work with the response object
This is the first step in your LangChain journey. As we progress through this series, you'll learn how to build increasingly powerful AI applications—from simple prompts to production-ready chatbots, RAG systems, and AI agents.
Happy Coding! 🚀