Integrating with LangGraph
Relevant source files
The following files were used as context for generating this wiki page:
LangGraph is a framework for building controllable agent workflows and stateful, multi-step applications powered by LLMs. It extends the declarative LangChain Expression Language (LCEL) by allowing developers to define flows as graphs, which can include cycles for more complex, agent-like behaviors. LangGraph is designed for advanced customization, agent orchestration, and building reliable agents that can handle complex tasks.
Sources: README.md:25-28, libs/core/README.md:21-22
LangGraph is a key part of the LangChain ecosystem, alongside LangChain Core for base abstractions and LangSmith for observability. It is particularly useful for building agents with customizable architecture, long-term memory, and human-in-the-loop workflows. The framework is trusted in production by various companies, indicating its stability and robustness.
Sources: README.md:46-52
This diagram illustrates where LangGraph fits within the overall LangChain stack, building upon the core abstractions.
Diagram based on concepts from libs/core/README.md.
Sources: libs/core/README.md:40-45
Core Features
LangGraph provides a low-level orchestration framework with several key features for building advanced LLM applications.
| Feature | Description | Source |
|---|---|---|
| Agent Orchestration | A low-level framework for building controllable agent workflows. | README.md:26-28 |
| Customizable Architecture | Offers customizable architecture for complex tasks. | README.md:50-51 |
| Stateful Workflows | Enables the creation of long-running, stateful workflows. | README.md:54-55 |
| Cyclic Graphs | Supports cyclic and recursive workflows, which is a primary differentiator from standard LCEL chains. | libs/core/README.md:43-45 |
| Human-in-the-loop | Natively supports human-in-the-loop workflows. | README.md:51 |
| Deployment Platform | Integrates with the LangGraph Platform for deploying and scaling agents. | README.md:53-56 |
Migrating from Chains to LangGraph
A practical way to understand LangGraph is to see how it replaces and improves upon older, more monolithic Chain implementations. The deprecated APIChain serves as a clear example of this migration.
Sources: libs/langchain/langchain/chains/api/base.py:82-90
Legacy APIChain
The APIChain was designed to make an API call and summarize the response. It involved two separate LLMChain instances: one to determine the API URL to call and another to process the response and answer the user's question.
Sources: libs/langchain/langchain/chains/api/base.py:68-70, libs/langchain/langchain/chains/api/base.py:228-230
LangGraph-based Replacement
The recommended replacement for APIChain is a graph built with LangGraph. This modern approach offers significant benefits:
- Tool Calling: Utilizes the LLM's native tool-calling features for more reliable and properly-formatted API requests.
- Streaming: Supports both token-by-token and step-by-step streaming.
- State Management: Natively supports checkpointing and memory for chat history.
- Extensibility: The graph structure is easier to modify and extend with additional tools or custom logic.
Sources: libs/langchain/langchain/chains/api/base.py:84-90
The following diagram shows the flow of the LangGraph-based API-calling agent.
This flowchart represents the state transitions and logic within the StateGraph example.
Sources: libs/langchain/langchain/chains/api/base.py:161-171
Implementation Example
The docstring for the deprecated APIChain provides a complete, self-contained example of building its replacement using LangGraph.
Key components of the implementation include:
StateGraph: The core of the graph, which manages the application's state.ChainState: ATypedDictthat defines the structure of the state passed between nodes, typically containing messages.- Nodes: Functions that represent steps in the graph (e.g., calling a tool, executing it, calling the final model).
- Edges: Connections that define the flow of control between nodes.
Sources: libs/langchain/langchain/chains/api/base.py:94-173
Here is the example code for the LangGraph implementation:
from typing import Annotated, Sequence
from typing_extensions import TypedDict
from langchain.chains.api.prompt import API_URL_PROMPT
from langchain_community.agent_toolkits.openapi.toolkit import RequestsToolkit
from langchain_community.utilities.requests import TextRequestsWrapper
from langchain_core.messages import BaseMessage
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.runnables import RunnableConfig
from langgraph.graph import END, StateGraph
from langgraph.graph.message import add_messages
from langgraph.prebuilt.tool_node import ToolNode
# ... (API spec definition)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
toolkit = RequestsToolkit(
requests_wrapper=TextRequestsWrapper(headers={}),
allow_dangerous_requests=ALLOW_DANGEROUS_REQUESTS,
)
tools = toolkit.get_tools()
api_request_chain = (
API_URL_PROMPT.partial(api_docs=api_spec)
| llm.bind_tools(tools, tool_choice="any")
)
class ChainState(TypedDict):
"""LangGraph state."""
messages: Annotated[Sequence[BaseMessage], add_messages]
async def acall_request_chain(state: ChainState, config: RunnableConfig):
# ... implementation ...
async def acall_model(state: ChainState, config: RunnableConfig):
# ... implementation ...
graph_builder = StateGraph(ChainState)
graph_builder.add_node("call_tool", acall_request_chain)
graph_builder.add_node("execute_tool", ToolNode(tools))
graph_builder.add_node("call_model", acall_model)
graph_builder.set_entry_point("call_tool")
graph_builder.add_edge("call_tool", "execute_tool")
graph_builder.add_edge("execute_tool", "call_model")
graph_builder.add_edge("call_model", END)
chain = graph_builder.compile()
Sources: libs/langchain/langchain/chains/api/base.py:118-173
Security and Stability
LangGraph is considered a stable and core part of the LangChain ecosystem. It is included in the scope for the official bug bounty program, which signifies its readiness for production use. Developers building with LangGraph should still follow security best practices, especially when creating agents that interact with external tools and APIs.
Sources: SECURITY.md:55-58
Conclusion
Integrating with LangGraph allows developers to move beyond simple, linear chains and build sophisticated, stateful, and cyclic applications. It provides the control and flexibility needed for complex agentic workflows, such as those requiring tool use, long-term memory, and human-in-the-loop interventions. By replacing older chain types with more robust and extensible graph-based implementations, LangGraph represents a significant architectural improvement for building advanced LLM-powered systems.
Sources: README.md:25-28, 49-52, libs/core/README.md:43-45