LangChain remains one of the most widely adopted frameworks for building AI agents. Its composable architecture — built around the LangChain Expression Language (LCEL) — lets you wire together LLMs, tools, memory, and retrieval into coherent reasoning pipelines. But reading the documentation only goes so far. Seeing real examples is what makes the patterns click.
This guide covers six concrete LangChain agent examples, each with a working architecture description and realistic code snippets. Whether you're building your first agent or looking to expand your pattern library, these examples provide a practical reference you can adapt immediately.
To understand the underlying concepts, read What is an AI Agent and then come back here for the implementation details.
Example 1: ReAct Research Agent with Web Search#
Use Case: A general-purpose research agent that can answer questions requiring current information by searching the web, reading snippets, and synthesizing an answer.
Architecture: ChatOpenAI model + TavilySearchResults tool + create_react_agent + AgentExecutor. The agent follows the Reasoning + Acting (ReAct) loop: it reasons about what to search, calls the search tool, reads the result, and iterates until it can produce a final answer.
Key Implementation:
from langchain_openai import ChatOpenAI
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain.agents import create_react_agent, AgentExecutor
from langchain import hub
llm = ChatOpenAI(model="gpt-4o", temperature=0)
tools = [TavilySearchResults(max_results=3)]
# Pull the ReAct prompt from LangChain Hub
prompt = hub.pull("hwchase17/react")
agent = create_react_agent(llm=llm, tools=tools, prompt=prompt)
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True,
max_iterations=5
)
result = agent_executor.invoke({
"input": "What are the top AI agent frameworks released in 2025?"
})
print(result["output"])
Outcome: The agent performs 1–3 search calls, synthesizes the results, and returns a well-structured answer. Setting verbose=True lets you observe each reasoning step for debugging. Learn how to build this end-to-end in the LangChain agent tutorial.
Example 2: Tool-Calling Customer Support Agent#
Use Case: A customer support agent for a SaaS product that can look up order status, check account details, and create support tickets — all via tool calls, not free-form text responses.
Architecture: ChatAnthropic model with bind_tools + custom tool functions + LCEL runnable chain. Uses structured tool calling (function calling) rather than the text-scraping approach of ReAct.
Key Implementation:
from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool
from langchain_core.messages import HumanMessage
@tool
def get_order_status(order_id: str) -> dict:
"""Look up the current status of an order by its ID."""
# In production: query your orders database
return {"order_id": order_id, "status": "shipped", "eta": "2026-03-02"}
@tool
def create_support_ticket(subject: str, description: str, priority: str = "medium") -> dict:
"""Create a new support ticket in the helpdesk system."""
# In production: call your helpdesk API
return {"ticket_id": "TKT-9823", "status": "created", "priority": priority}
llm = ChatAnthropic(model="claude-3-5-sonnet-20241022")
tools = [get_order_status, create_support_ticket]
llm_with_tools = llm.bind_tools(tools)
messages = [HumanMessage(content="My order #A1042 hasn't arrived. Can you check it and open a ticket?")]
response = llm_with_tools.invoke(messages)
# Parse tool_calls from response.tool_calls and execute them
Outcome: The model returns structured tool call requests that you execute deterministically, giving you full control over side effects. This pattern is safer than ReAct for customer-facing systems.
Example 3: Document Q&A Agent with Retrieval#
Use Case: An internal knowledge base agent that retrieves relevant documents from a vector store and answers employee questions using retrieved context.
Architecture: FAISS vector store + OpenAIEmbeddings + create_retriever_tool + create_react_agent. The agent decides when to retrieve and when it already knows the answer.
Key Implementation:
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.tools.retriever import create_retriever_tool
from langchain.agents import create_react_agent, AgentExecutor
from langchain import hub
# Build vector store from documents (done once at startup)
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_texts(
texts=["Our refund policy allows returns within 30 days...",
"Enterprise pricing starts at $500/month for 10 seats..."],
embedding=embeddings
)
retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
retriever_tool = create_retriever_tool(
retriever=retriever,
name="search_knowledge_base",
description="Search the company knowledge base for policies, pricing, and procedures."
)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
agent = create_react_agent(llm=llm, tools=[retriever_tool], prompt=hub.pull("hwchase17/react"))
agent_executor = AgentExecutor(agent=agent, tools=[retriever_tool])
result = agent_executor.invoke({"input": "What is the enterprise pricing?"})
Outcome: Grounded answers drawn from your own documents, with the model citing retrieved passages. See the full Agentic RAG tutorial for a more advanced version with query routing.
Example 4: Multi-Step Data Processing Agent#
Use Case: An analyst agent that accepts a dataset description, writes and executes Python code to analyze it, and returns structured insights.
Architecture: ChatOpenAI + PythonREPLTool + custom output parser. The agent writes pandas/matplotlib code, executes it in a sandboxed REPL, reads the stdout output, and iterates.
Key Implementation:
from langchain_openai import ChatOpenAI
from langchain_experimental.tools import PythonREPLTool
from langchain.agents import create_react_agent, AgentExecutor
from langchain import hub
llm = ChatOpenAI(model="gpt-4o", temperature=0)
python_tool = PythonREPLTool()
agent = create_react_agent(
llm=llm,
tools=[python_tool],
prompt=hub.pull("hwchase17/react")
)
agent_executor = AgentExecutor(
agent=agent,
tools=[python_tool],
max_iterations=8,
verbose=True
)
result = agent_executor.invoke({
"input": (
"I have a CSV at /data/sales_q4.csv with columns: date, product, revenue, region. "
"Calculate total revenue by region and identify the top-performing product."
)
})
Outcome: The agent writes pandas code, runs it, reads the output, and synthesizes a business-ready summary. For production, sandbox the REPL using Docker or E2B. See the Data Analyst Agent tutorial for a complete setup.
Example 5: Conversational Agent with Long-Term Memory#
Use Case: A personal assistant agent that remembers user preferences and past conversations across sessions using persistent chat history.
Architecture: ChatOpenAI + ConversationSummaryBufferMemory + SQLChatMessageHistory for persistence + AgentExecutor.
Key Implementation:
from langchain_openai import ChatOpenAI
from langchain.memory import ConversationSummaryBufferMemory
from langchain_community.chat_message_histories import SQLChatMessageHistory
from langchain.agents import create_react_agent, AgentExecutor
from langchain import hub
from langchain_community.tools.tavily_search import TavilySearchResults
chat_history = SQLChatMessageHistory(
session_id="user_42",
connection_string="sqlite:///./chat_history.db"
)
memory = ConversationSummaryBufferMemory(
llm=ChatOpenAI(model="gpt-4o-mini"),
chat_memory=chat_history,
max_token_limit=2000,
memory_key="chat_history",
return_messages=True
)
llm = ChatOpenAI(model="gpt-4o", temperature=0.3)
tools = [TavilySearchResults(max_results=2)]
agent = create_react_agent(llm=llm, tools=tools, prompt=hub.pull("hwchase17/react-chat"))
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
memory=memory,
verbose=True
)
# Session 1
agent_executor.invoke({"input": "I prefer concise bullet-point answers."})
# Session 2 (memory persisted)
agent_executor.invoke({"input": "What are the latest AI agent frameworks?"})
# Agent remembers user prefers bullet points
Outcome: The agent maintains context across restarts, personalizes responses, and avoids repeating itself. For deeper exploration of memory patterns, review What is an AI Agent.
Example 6: Parallel Tool Execution Agent#
Use Case: A research aggregator that fires multiple tool calls in parallel — fetching from APIs, searching the web, and querying a database simultaneously — to minimize latency.
Architecture: ChatOpenAI with parallel_tool_calls=True + multiple tools + custom execution loop that runs tool calls concurrently using asyncio.
Key Implementation:
import asyncio
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from langchain_core.messages import HumanMessage, ToolMessage
@tool
async def search_web(query: str) -> str:
"""Search the web for current information."""
await asyncio.sleep(0.1) # Simulate API call
return f"Web results for: {query}"
@tool
async def query_database(sql: str) -> str:
"""Run a read-only SQL query against the analytics database."""
await asyncio.sleep(0.1) # Simulate DB call
return f"DB results for query: {sql}"
llm = ChatOpenAI(model="gpt-4o").bind_tools([search_web, query_database])
async def run_agent(user_input: str):
messages = [HumanMessage(content=user_input)]
response = await llm.ainvoke(messages)
if response.tool_calls:
# Execute all tool calls concurrently
tool_tasks = []
for tc in response.tool_calls:
if tc["name"] == "search_web":
tool_tasks.append(search_web.ainvoke(tc["args"]))
elif tc["name"] == "query_database":
tool_tasks.append(query_database.ainvoke(tc["args"]))
results = await asyncio.gather(*tool_tasks)
print("Parallel results:", results)
asyncio.run(run_agent("Compare our Q4 sales vs industry benchmarks"))
Outcome: Latency drops significantly when multiple independent data sources are queried simultaneously. This pattern is critical for production agents where tool calls are the primary bottleneck. Compare this approach with other frameworks in LangChain vs CrewAI.
Choosing the Right LangChain Pattern#
The six examples above cover the most common LangChain agent architectures. Use ReAct for general-purpose research agents where the reasoning trace matters. Use tool calling (bind_tools) when you need deterministic control over side effects. Add retrieval tools when grounding the agent in private knowledge. Layer in memory when sessions span multiple turns. And reach for parallel async execution when latency is a constraint.
The ReAct reasoning pattern is the foundation most LangChain agents build on, so understanding it deeply pays dividends as your complexity grows.
Getting Started#
The fastest path to a working LangChain agent is the official LangChain agent tutorial, which walks through environment setup, tool configuration, and running your first AgentExecutor. From there, the Agentic RAG tutorial shows how to add document retrieval, and LangChain vs CrewAI helps you decide when to reach for a multi-agent framework instead.
For production deployments, explore LangGraph — LangChain's stateful agent orchestration layer — which adds checkpointing, branching, and human-in-the-loop support on top of the patterns shown here.
Frequently Asked Questions#
The FAQ section renders from the frontmatter faq array above.