Building AI Agents with LangChain Tutorial: From Zero to Production
Complete tutorial for building production-grade AI agents with LangChain. Tools, memory, error handling, observability, and deployment patterns that work at scale.

Building AI Agents with LangChain Tutorial: From Zero to Production
LangChain has become the de facto framework for building AI agents that actually do things—agents that can search the web, query databases, call APIs, and chain together complex reasoning steps. But most LangChain tutorials stop at toy examples: "look, it can search Wikipedia!" Meanwhile, production teams struggle with reliability issues, cost explosions, and agents that work 80% of the time and catastrophically fail the other 20%.
This tutorial bridges that gap. We'll build a real AI agent that handles customer support inquiries by querying order databases, checking inventory, processing refunds, and escalating to humans when needed. You'll learn not just how to make it work, but how to make it reliable, cost-effective, and maintainable.
Building AI agents with LangChain requires understanding its core abstractions: chains, agents, tools, memory, and callbacks. More importantly, you need to know when to use LangChain's high-level abstractions versus when to drop down to raw LLM calls for better control.
What is LangChain?
LangChain is a Python/JavaScript framework that simplifies building applications with large language models. It provides:
- Chains: Sequences of LLM calls and data transformations
- Agents: Systems that decide which tools to use based on user input
- Tools: Functions the agent can call (search, databases, APIs)
- Memory: Conversation history and context management
- Callbacks: Logging, monitoring, and debugging hooks
The key insight: most AI agent applications follow similar patterns. LangChain provides battle-tested implementations so you don't reinvent the wheel.
Why Use LangChain for AI Agents?
Rapid prototyping: Go from idea to working demo in hours, not weeks. The abstractions handle boilerplate so you focus on business logic.
Production-tested patterns: Multi-step reasoning, error recovery, and tool calling are hard to get right. LangChain bakes in years of community learnings.
Ecosystem integration: Pre-built connectors for 200+ services (databases, APIs, vector stores). Don't write glue code.
Observability built-in: Callbacks and tracing show exactly what your agent is thinking and doing—critical for debugging.
That said, LangChain isn't perfect. It abstracts away details that sometimes matter. As you'll see, production systems often need to mix LangChain's high-level API with lower-level control.

Prerequisites
Install LangChain and required dependencies:
pip install langchain langchain-openai langchain-community sqlalchemy
You'll need an OpenAI API key:
export OPENAI_API_KEY="sk-..."
Building Your First Agent: Customer Support Bot
We'll build an agent that can:
- Look up order status from a database
- Check product inventory
- Process refunds
- Escalate complex issues to humans
Step 1: Define Tools
Tools are functions your agent can call. Each tool has a name, description, and implementation.
from langchain.tools import tool
from sqlalchemy import create_engine, text
engine = create_engine('sqlite:///orders.db')
@tool
def lookup_order(order_id: str) -> str:
"""Look up order details by order ID. Returns order status, items, and shipping info."""
with engine.connect() as conn:
result = conn.execute(
text("SELECT * FROM orders WHERE order_id = :id"),
{"id": order_id}
).fetchone()
if not result:
return f"Order {order_id} not found."
return f"Order {order_id}: Status={result.status}, Items={result.items}, Ship Date={result.ship_date}"
@tool
def check_inventory(product_id: str) -> str:
"""Check current inventory for a product. Returns quantity available."""
with engine.connect() as conn:
result = conn.execute(
text("SELECT quantity FROM inventory WHERE product_id = :id"),
{"id": product_id}
).fetchone()
if not result:
return f"Product {product_id} not found."
return f"Product {product_id}: {result.quantity} units available"
@tool
def process_refund(order_id: str, reason: str) -> str:
"""Process a refund for an order. Requires order ID and reason."""
print(f"REFUND REQUEST: Order {order_id}, Reason: {reason}")
return f"Refund processed for order {order_id}. Confirmation email sent."
@tool
def escalate_to_human(issue: str) -> str:
"""Escalate complex issues to human support agent."""
print(f"ESCALATION: {issue}")
return "Issue escalated to human agent. They'll respond within 30 minutes."
Key point: Tool descriptions are critical. The LLM uses them to decide when to call each tool. Be specific about parameters and return values.
Step 2: Create the Agent
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
system_prompt = """You are a helpful customer support agent for an e-commerce company.
You have access to tools to look up orders, check inventory, process refunds, and escalate issues.
Guidelines:
- Always verify order details before processing refunds
- Be empathetic and professional
- If you're unsure or the issue is complex, escalate to a human
- Never make up order numbers or product IDs
"""
prompt = ChatPromptTemplate.from_messages([
("system", system_prompt),
MessagesPlaceholder(variable_name="chat_history", optional=True),
("human", "{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad")
])
llm = ChatOpenAI(model="gpt-4o", temperature=0)
tools = [lookup_order, check_inventory, process_refund, escalate_to_human]
agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True,
max_iterations=5,
early_stopping_method="generate"
)
Why these parameters:
max_iterations=5: Prevent infinite loopsearly_stopping_method="generate": If max iterations hit, generate a response instead of erroringverbose=True: See what the agent is thinking (critical for debugging)
Step 3: Add Memory for Multi-Turn Conversations
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True
)
response = agent_executor.invoke(
{"input": "I need help with my order"},
config={"configurable": {"session_id": "user_12345"}}
)
For production, use external storage (Redis, PostgreSQL):
from langchain.memory import RedisChatMessageHistory
def get_message_history(session_id: str):
return RedisChatMessageHistory(
session_id=session_id,
url="redis://localhost:6379"
)
Advanced Patterns
Error Handling & Retries
from langchain.callbacks import StdOutCallbackHandler
class ErrorHandlingCallback(StdOutCallbackHandler):
def on_tool_error(self, error: Exception, **kwargs):
print(f"Tool error: {error}")
# Log to monitoring system
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
callbacks=[ErrorHandlingCallback()],
handle_parsing_errors=True
)
Cost Control
from langchain.callbacks import get_openai_callback
with get_openai_callback() as cb:
response = agent_executor.invoke({"input": "Help with order 12345"})
print(f"Cost: ${cb.total_cost:.4f}")
print(f"Tokens: {cb.total_tokens}")
RAG Integration
Combine agent with knowledge base:
from langchain.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain.tools.retriever import create_retriever_tool
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.load_local("product_docs", embeddings)
doc_retriever_tool = create_retriever_tool(
vectorstore.as_retriever(),
"search_product_docs",
"Search product documentation and FAQs."
)
tools.append(doc_retriever_tool)
For deeper RAG understanding, see our RAG implementation guide.
Production Considerations
Observability
Use LangSmith for detailed tracing:
import os
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_API_KEY"] = "your_key"
For comprehensive monitoring, see AI agent monitoring best practices.
Security
Validate before executing destructive actions:
@tool
def process_refund(order_id: str, amount: float) -> str:
"""Process refund. Requires manual approval for amounts >$100."""
order = verify_order_ownership(order_id, current_user_id)
if not order:
return "Order not found or doesn't belong to you."
if amount > 100:
create_approval_request(order_id, amount)
return "Refund requires manager approval."
execute_refund(order_id, amount)
return f"Refund of ${amount} processed."
Performance Optimization
Tool selection with embeddings: When you have 50+ tools, filter relevant ones:
# Retrieve top-5 relevant tools based on user input
relevant_tools = retrieve_tools(user_input, tool_embeddings, k=5)
agent = create_openai_functions_agent(llm, relevant_tools, prompt)
Caching LLM calls:
from langchain.cache import RedisCache
import langchain
langchain.llm_cache = RedisCache(redis_url="redis://localhost:6379")
For cost optimization tactics, see AI agent cost optimization strategies.
Common Mistakes to Avoid
Too many tools. Start with 3-5 essential tools. Every additional tool reduces agent reliability.
Vague tool descriptions. "Search database" → bad. "Search customer orders by order ID, returns status and items" → good.
No max iterations. Agent enters infinite loop → API costs explode.
Ignoring failures. Agent will fail ~10% of the time. Build escalation flows.
No validation of tool outputs. Agent hallucinates that a tool succeeded when it didn't. Verify results.
Testing & Evaluation
Build a test suite:
test_cases = [
{
"input": "Order 12345 status?",
"expected_tools": ["lookup_order"],
"expected_outcome": "returns order status"
},
{
"input": "Refund order 12345",
"expected_tools": ["lookup_order", "process_refund"],
"expected_outcome": "confirms refund"
}
]
for test in test_cases:
result = agent_executor.invoke({"input": test["input"]})
validate_result(result, test)
Track metrics:
- Tool selection accuracy: Did agent choose correct tools?
- Success rate: What % of conversations achieve user goals?
- Cost per conversation: Optimize expensive workflows
- Escalation rate: How often does agent give up?
Scaling to Production
Async execution for concurrency:
import asyncio
async def handle_user_message(user_id: str, message: str):
response = await agent_executor.ainvoke({
"input": message,
"session_id": user_id
})
return response
await asyncio.gather(*[
handle_user_message(uid, msg)
for uid, msg in incoming_messages
])
Load balancing & rate limiting:
from ratelimit import limits, sleep_and_retry
@sleep_and_retry
@limits(calls=50, period=60)
def call_agent(message: str):
return agent_executor.invoke({"input": message})
Conclusion
Building AI agents with LangChain tutorial takes you from basic concepts to production-ready systems. The framework provides powerful abstractions that accelerate development, but production systems require attention to error handling, observability, cost control, and security.
The key lessons:
- Start simple (3-5 tools), iterate based on real usage
- Verbose logging is mandatory for debugging
- Validate tool outputs, never trust blindly
- Set budget limits and max iterations
- Build escalation flows for complex cases
LangChain is evolving rapidly. The patterns in this tutorial are current as of 2026, but stay updated with the official docs and community best practices. The fundamentals—clear tool definitions, robust error handling, and continuous evaluation—remain constant.
Your first agent won't be perfect. That's expected. Ship it, learn from failures, iterate weekly. The teams shipping great AI agents aren't the ones with the fanciest architecture—they're the ones that launch fast and improve continuously.
Build AI That Works For Your Business
At AI Agents Plus, we help companies move from AI experiments to production systems that deliver real ROI. Whether you need:
- Custom AI Agents — Autonomous systems that handle complex workflows, from customer service to operations
- Rapid AI Prototyping — Go from idea to working demo in days using vibe coding and modern AI frameworks
- Voice AI Solutions — Natural conversational interfaces for your products and services
We've built AI systems for startups and enterprises across Africa and beyond.
Ready to explore what AI can do for your business? Let's talk →
About AI Agents Plus Editorial
AI automation expert and thought leader in business transformation through artificial intelligence.



