What Are Deep Agents in LangChain?
Deep agents represent the next evolution in AI agent design within the LangChain ecosystem. Unlike basic agents that perform simple tool calls based on direct prompts, deep agents incorporate multi-layered reasoning, long-term memory, hierarchical planning, and dynamic tool integration. They excel at handling intricate workflows, such as multi-step research, code generation with debugging, or automated data analysis pipelines.
These agents leverage LangChain's core components like chains, tools, retrievers, and the emerging LangGraph for stateful, graph-based execution. By simulating human-like deliberation—breaking tasks into sub-tasks, reflecting on intermediate results, and adapting—they achieve superior performance on benchmarks like AgentBench or GAIA.
In practice, deep agents shine in scenarios like enterprise automation, where a single prompt might trigger research, synthesis, verification, and reporting. This guide walks you through building them from scratch, adding depth layer by layer.
Prerequisites and Setup
Before diving in, ensure you have:
- Python 3.10+
- Familiarity with LangChain basics (LLMs, prompts, tools)
- API keys for an LLM provider (e.g., OpenAI, Anthropic, or Grok)
Install the required packages:
pip install langchain langchain-openai langgraph langchain-community tavily-python
Set environment variables:
import os
os.environ["OPENAI_API_KEY"] = "your-key-here"
os.environ["TAVILY_API_KEY"] = "your-tavily-key" # For search tool
We'll use OpenAI's GPT-4o as the base model, but you can swap it for others via LangChain's unified interface. Key repos for reference: LangChain main repo and LangGraph for advanced flows.
Step 1: Construct a Basic ReAct Agent
Start with a foundational ReAct (Reason + Act) agent, which alternates between thinking and tool use.
Define tools:
from langchain.tools import tool
from langchain_community.tools.tavily_search import TavilySearchResults
@tool
def multiply(a: int, b: int) -> int:
"""Multiplies two integers."""
return a * b
search = TavilySearchResults(max_results=3)
tools = [multiply, search]
Build the agent:
from langchain_openai import ChatOpenAI
from langchain.agents import create_react_agent, AgentExecutor
from langchain import hub
llm = ChatOpenAI(model="gpt-4o", temperature=0)
prompt = hub.pull("hwchase17/react")
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
result = agent_executor.invoke({"input": "What is 5*12? Search for confirmation if unsure."})
print(result["output"])
Output: Agent reasons, calls multiply, confirms via search. This is shallow—single loop.
Step 2: Introduce Memory for Stateful Behavior
Deep agents remember past interactions. Use conversation memory:
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory, verbose=True)
Now, follow-up queries like "Double that result" retain context, enabling persistent sessions.
Real-world app: Customer support bot tracking user history across queries.
Step 3: Add Hierarchical Planning with Sub-Agents
Elevate to deep structure using a supervisor agent overseeing specialized sub-agents (e.g., researcher, coder, verifier).
Leverage LangGraph for this graph-based orchestration:
from langgraph.prebuilt import create_react_agent
researcher = create_react_agent(llm, [search])
coder = create_react_agent(llm, [multiply]) # Extend with code tools
verifier = create_react_agent(llm, tools)
Define a planning graph:
from langgraph.graph import StateGraph, END
from typing import TypedDict, List
class AgentState(TypedDict):
input: str
plan: List[str]
results: dict
workflow = StateGraph(AgentState)
# Add nodes for supervisor, sub-agents
workflow.add_node("supervisor", supervisor_func)
workflow.add_node("researcher", researcher.invoke)
# ... edges based on routing
app = workflow.compile()
Supervisor decides: "Research first, then code, verify last." This creates depth—agents call sub-agents recursively.
Example task: "Build a script to analyze stock trends."
- Supervisor plans: Research → Code → Verify.
- Researcher fetches data.
- Coder generates Python with pandas.
- Verifier tests output.
Check LangGraph examples repo for full hierarchies.
Step 4: Integrate Retrieval-Augmented Generation (RAG)
Deep agents query custom knowledge bases for grounded responses.
Setup:
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
# Load docs, split, embed
splitter = RecursiveCharacterTextSplitter(chunk_size=1000)
docs = splitter.split_documents(your_docs)
vectorstore = FAISS.from_documents(docs, OpenAIEmbeddings())
retriever = vectorstore.as_retriever()
Toolify retriever:
@tool
def query_docs(query: str):
return retriever.get_relevant_documents(query)
Agents now pull internal data, reducing hallucinations. App: Legal research agent citing case law.
Step 5: Reflection and Self-Improvement Loops
True depth comes from critique. Implement reflection:
def reflect(state):
prompt = f"Critique this plan: {state['plan']}\
Improve it."
return llm.invoke(prompt).content
# Insert reflection node in graph
Multi-round: Agent proposes, reflects, revises. Boosts accuracy by 20-30% on complex tasks per LangChain benchmarks.
Step 6: Tool Expansion and Custom Tools
Scale with 10+ tools: math, code exec (via E2B), web nav (Playwright), databases (SQLAgent).
Custom example—GitHub analyzer:
@tool
def analyze_repo(url: str):
# Clone, analyze README, stars, etc.
pass
tools.append(analyze_repo)
See LangChain tools hub.
Deployment and Monitoring
Deploy via LangServe:
pip install langserve
langserve build agent-app
Monitor with LangSmith: Trace executions, debug failures. Sign up at langsmith.com.
Production tip: Rate limits—use async agents for parallelism.
Real-World Applications
- Automated Reporting: Agent researches market data, generates charts, emails report.
- Code Review Bot: Analyzes PRs, suggests fixes, runs tests.
- Personal Assistant: Plans trips—searches flights, books via API, budgets.
Example output for trip planning:
- Input: "Plan a 3-day Tokyo trip under $2000."
- Deep agent: Searches hotels/flights, calculates costs (multiply tool), verifies reviews, outputs itinerary.
Best Practices and Pitfalls
- Prompt Engineering: Use few-shot examples in hub prompts.
- Error Handling: Wrap tools in retries.
- Cost Control: Limit max iterations (default 15).
- Security: Sandbox tools, validate inputs.
- Pitfall: Infinite loops—set strict termination.
Extend further with multi-agent colonies.
This setup positions you to build production-grade deep agents. Experiment, iterate, and share your builds!
(Word count: ~1250)
<div style="text-align: center; margin-top: 2rem;"> <a href="https://www.analyticsvidhya.com/blog/2025/11/langchains-deep-agent-guide/" target="_blank" rel="noopener noreferrer" class="view-full-resource-btn" style="display: inline-block; background-color: #f97316; color: white; padding: 12px 24px; border-radius: 8px; text-decoration: none; font-weight: 600; transition: background-color 0.2s;">View Full Resource</a> </div>
Stay ahead of the AI curve
The most important updates, news, and content — delivered in one weekly newsletter.