Home/Blog/LangChain for Beginners: Build Your Firs...
Development

LangChain for Beginners: Build Your First AI Agent

LangChain is the framework that powers most production AI applications. This step-by-step guide shows you how to build a real AI agent using LangChain.

2026-06-2518 min readLearnGeni AI Academy

LangChain for Beginners: Build Your First AI Agent

LangChain has become the de facto standard framework for building production AI applications. While calling the OpenAI API directly gets you a simple chatbot, LangChain gives you the building blocks to construct sophisticated AI systems: agents that browse the web and run code, chatbots that remember previous conversations, document Q&A systems that know your company's knowledge base, and multi-step AI pipelines that orchestrate complex workflows.

This guide takes you from zero to a working AI agent — with real, runnable code at every step.

What is LangChain?

LangChain is an open-source Python (and JavaScript) framework for building applications powered by large language models. Released in late 2022 and rapidly adopted across the industry, it provides abstractions and composable components that make building complex AI systems dramatically faster than working with raw API calls.

By 2026, LangChain (and its extension LangGraph for stateful, multi-agent workflows) powers a significant share of enterprise AI applications worldwide. If you want to go beyond simple chatbots and build real AI systems, LangChain is where you start.

What Problems Does LangChain Solve?

Without a framework like LangChain, building a production AI application requires solving dozens of infrastructure problems from scratch:

  • How do you give the AI memory of previous conversations?
  • How do you connect the AI to your company's documents and knowledge base?
  • How do you give the AI tools to use — like searching the web or running code?
  • How do you chain multiple AI calls together into a pipeline?
  • How do you switch between different AI providers (OpenAI, Anthropic, Google)?
  • How do you evaluate whether your AI is actually working correctly?

LangChain solves all of these problems with standardized, composable components that work together.

Core Concepts

Before writing code, let's understand the key concepts:

Models

Model wrappers that provide a consistent interface to any LLM provider — OpenAI, Anthropic, Google, Cohere, or local models. Switching from GPT-4o to Claude is a one-line change.

Prompt Templates

Reusable prompt structures with variable placeholders. Instead of hardcoding prompts, templates let you define prompt patterns once and fill in context dynamically.

Chains (LCEL)

LangChain Expression Language (LCEL) lets you compose complex pipelines using a simple pipe (|) syntax: prompt | model | output_parser. Chains define how data flows through your AI application.

Memory

Components for storing and retrieving conversation history. Without memory, every conversation starts fresh. With memory, the AI can reference previous exchanges and build on them.

Tools

Functions that AI agents can call: searching the web, running Python code, querying a database, calling an API, or any custom function you define. Tools transform AI from a question-answerer to an action-taker.

Agents

The most powerful LangChain component. An agent is an LLM that decides which tools to use, in what order, and how to interpret the results. Agents can handle complex, multi-step tasks that require reasoning and tool use.

Retrievers and Vector Stores

Components for semantic search over documents. Given a question, a retriever finds the most relevant chunks from a large document corpus — enabling AI to answer questions about specific knowledge bases.

Setting Up LangChain

Prerequisites

  • Python 3.9 or higher
  • An OpenAI API key (from platform.openai.com)
  • Basic Python familiarity

Installation

pip install langchain langchain-openai langchain-community faiss-cpu pypdf

Environment Setup

export OPENAI_API_KEY="your-api-key-here"

Or in Python:

import os
os.environ["OPENAI_API_KEY"] = "your-api-key-here"

Your First LangChain Application: A Simple Chain

Let's start with the simplest possible LangChain application — a chain that takes a topic and writes a blog introduction about it:

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

# 1. Initialize the model
llm = ChatOpenAI(model="gpt-4o", temperature=0.7)

# 2. Create a prompt template
prompt = ChatPromptTemplate.from_messages([
    ("system", """You are an expert content writer. Write engaging, 
    well-structured content for the specified audience."""),
    ("human", """Write an introduction for a blog post about: {topic}
    Target audience: {audience}
    Length: approximately 150 words
    Tone: {tone}""")
])

# 3. Create the output parser
output_parser = StrOutputParser()

# 4. Chain everything together using LCEL
chain = prompt | llm | output_parser

# 5. Run the chain
result = chain.invoke({
    "topic": "The impact of AI on creative industries",
    "audience": "creative professionals",
    "tone": "inspiring and forward-looking"
})

print(result)

This is LCEL — LangChain Expression Language. The | operator pipes the output of each component into the input of the next. This simple syntax composes into arbitrarily complex pipelines.

Adding Memory: A Conversational AI Assistant

Let's build a chatbot that remembers the conversation:

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.messages import HumanMessage, AIMessage
from langchain_core.output_parsers import StrOutputParser

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

# Prompt with a placeholder for conversation history
prompt = ChatPromptTemplate.from_messages([
    ("system", """You are a helpful AI tutor specializing in machine learning 
    and AI. Be encouraging, clear, and thorough in your explanations.
    Remember what the student has told you about their background and adapt accordingly."""),
    MessagesPlaceholder(variable_name="history"),
    ("human", "{input}")
])

chain = prompt | llm | StrOutputParser()

# Maintain conversation history manually
history = []

def chat(user_message: str):
    response = chain.invoke({
        "history": history,
        "input": user_message
    })
    
    # Add this exchange to history
    history.append(HumanMessage(content=user_message))
    history.append(AIMessage(content=response))
    
    return response

# Example conversation
print(chat("Hi! I'm Sara. I have a Python background but I'm new to ML."))
print(chat("What should I learn first?"))
print(chat("Can you give me a code example for linear regression?"))
# The AI will remember Sara's name and Python background throughout

Building a Document Q&A System (RAG)

Retrieval-Augmented Generation (RAG) is one of the most valuable LangChain patterns. It enables AI to answer questions about your specific documents, knowledge base, or proprietary data:

from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_community.vectorstores import FAISS
from langchain.chains import RetrievalQA
from langchain_core.prompts import ChatPromptTemplate

# Step 1: Load documents
# Load a single PDF
loader = PyPDFLoader("company_handbook.pdf")

# Or load an entire directory of PDFs
# loader = DirectoryLoader("./documents", glob="**/*.pdf")

documents = loader.load()
print(f"Loaded {len(documents)} document pages")

# Step 2: Split into chunks
text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=1000,      # Characters per chunk
    chunk_overlap=200,    # Overlap to maintain context at boundaries
    length_function=len,
    separators=["\n\n", "\n", " ", ""]
)
chunks = text_splitter.split_documents(documents)
print(f"Created {len(chunks)} text chunks")

# Step 3: Create embeddings and vector store
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
vectorstore = FAISS.from_documents(chunks, embeddings)

# Optionally save for later use
vectorstore.save_local("./vectorstore")

# Step 4: Create a retriever
retriever = vectorstore.as_retriever(
    search_type="similarity",
    search_kwargs={"k": 5}  # Retrieve 5 most relevant chunks
)

# Step 5: Create the RAG chain with a custom prompt
rag_prompt = ChatPromptTemplate.from_messages([
    ("system", """You are a helpful assistant answering questions based on 
    the provided context. If the context doesn't contain enough information 
    to answer the question, say so clearly rather than making things up.
    
    Context:
    {context}"""),
    ("human", "{question}")
])

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

# Build the complete RAG chain
def format_docs(docs):
    return "\n\n".join(doc.page_content for doc in docs)

rag_chain = (
    {"context": retriever | format_docs, "question": lambda x: x}
    | rag_prompt
    | llm
    | StrOutputParser()
)

# Step 6: Query your documents
answer = rag_chain.invoke("What is the company's vacation policy?")
print(answer)

answer = rag_chain.invoke("How do I request a performance review?")
print(answer)

This pattern is transformative for organizations: any collection of documents, policies, knowledge base articles, or proprietary data becomes queryable through natural language.

Building a Real AI Agent with Tools

Agents are where LangChain truly shines. An agent can reason about a problem, decide what actions to take, use tools to gather information, and synthesize a final response:

from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.tools import tool
from langchain_community.tools import DuckDuckGoSearchRun
import requests
import json

# Define custom tools
@tool
def get_stock_price(ticker: str) -> str:
    """Get the current stock price for a given ticker symbol."""
    # In production, use a real financial API
    return f"${ticker} is currently trading at $150.25 (simulated data)"

@tool
def calculate(expression: str) -> str:
    """Evaluate a mathematical expression. Use Python syntax."""
    try:
        result = eval(expression, {"__builtins__": {}}, {})
        return str(result)
    except Exception as e:
        return f"Error: {str(e)}"

@tool
def get_weather(city: str) -> str:
    """Get current weather for a city."""
    # In production, use OpenWeatherMap or similar API
    return f"Current weather in {city}: 22°C, partly cloudy (simulated)"

# Combine tools
search = DuckDuckGoSearchRun()
tools = [search, get_stock_price, calculate, get_weather]

# Create the agent prompt
prompt = ChatPromptTemplate.from_messages([
    ("system", """You are a helpful research and analysis assistant.
    Use your tools to gather accurate, up-to-date information.
    Always cite which tools you used and be transparent about limitations.
    Think step by step before taking actions."""),
    ("human", "{input}"),
    MessagesPlaceholder(variable_name="agent_scratchpad"),
])

# Initialize the agent
llm = ChatOpenAI(model="gpt-4o", temperature=0)
agent = create_openai_tools_agent(llm, tools, prompt)
agent_executor = AgentExecutor(
    agent=agent, 
    tools=tools, 
    verbose=True,           # See the agent's reasoning
    max_iterations=10,      # Prevent infinite loops
    handle_parsing_errors=True
)

# Run the agent
result = agent_executor.invoke({
    "input": "What's the weather in Tokyo? Also, if I invest $5,000 in NVDA at the current price, how many shares would I get?"
})

print("\nFinal Answer:")
print(result["output"])

Watching the agent's verbose output is instructive — you see it reasoning about what information it needs, calling tools in sequence, and synthesizing the results. This is fundamentally different from a simple LLM response.

LangGraph: Stateful, Multi-Agent Workflows

For complex applications requiring multiple agents or precise state management, LangGraph provides a graph-based approach:

from langgraph.graph import StateGraph, END
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
from typing import TypedDict, Annotated, Sequence
import operator

# Define the state
class AgentState(TypedDict):
    messages: Annotated[Sequence, operator.add]
    next_step: str

# Define specialized agents
researcher_llm = ChatOpenAI(model="gpt-4o", temperature=0)
writer_llm = ChatOpenAI(model="gpt-4o", temperature=0.7)

def researcher_node(state: AgentState):
    """Research agent that gathers information"""
    messages = state["messages"]
    response = researcher_llm.invoke(
        [HumanMessage(content=f"Research this topic thoroughly: {messages[-1].content}")]
    )
    return {"messages": [response], "next_step": "writer"}

def writer_node(state: AgentState):
    """Writer agent that creates content from research"""
    messages = state["messages"]
    research = messages[-1].content
    response = writer_llm.invoke(
        [HumanMessage(content=f"Write a compelling article based on this research: {research}")]
    )
    return {"messages": [response], "next_step": "end"}

# Build the graph
workflow = StateGraph(AgentState)
workflow.add_node("researcher", researcher_node)
workflow.add_node("writer", writer_node)

workflow.set_entry_point("researcher")
workflow.add_edge("researcher", "writer")
workflow.add_edge("writer", END)

graph = workflow.compile()

# Run the multi-agent pipeline
result = graph.invoke({
    "messages": [HumanMessage(content="The impact of AI on the creative economy in 2026")],
    "next_step": "researcher"
})

print(result["messages"][-1].content)

When to Use LangChain vs. Direct API Calls

Use direct API calls when:

  • Simple, single-turn queries
  • Minimal complexity — one prompt, one response
  • Performance is critical and you want maximum control
  • You're experimenting and don't need infrastructure

Use LangChain when:

  • You need conversation memory
  • You're building agents with tool use
  • You're implementing RAG over documents
  • You need to chain multiple AI calls
  • You want provider flexibility (easily switch between OpenAI, Anthropic, etc.)
  • You need standardized evaluation and testing
  • Building production applications that will scale

Performance and Cost Optimization

Use Caching

LangChain has built-in caching that returns stored responses for identical prompts:

from langchain.globals import set_llm_cache
from langchain_community.cache import SQLiteCache

set_llm_cache(SQLiteCache(database_path=".langchain.db"))
# Identical prompts will return cached responses instantly

Choose the Right Model

For most tasks, gpt-4o-mini is sufficient and costs 10–15x less than gpt-4o. Reserve the more powerful model for tasks requiring complex reasoning.

Limit Context

Long prompts are expensive. Use summarization to compress conversation history for long interactions. Optimize chunk sizes in RAG systems to retrieve only what's needed.


Ready to go deeper? LearnGeni's dedicated LangChain guide covers LangGraph for multi-agent systems, production deployment patterns, custom tool creation, advanced RAG architectures, and LangSmith for evaluation and monitoring — all in your language, as part of the 30-guide AI Mastery Program.

G

Earn Your International AI Certificate

This article is part of the LearnGeni AI Mastery Program — 30 comprehensive guides in 50+ languages. Complete all 30 and earn a certificate backed by WhatsGeni (Official Meta AI Partner), recognized in 50+ countries.

🌍
50+ Languages
Learn in your native language
🏆
Official Certificate
Backed by Meta AI Partner
💡
30 Guides
$5 each or $50 for the full bundle