A minimal, visual example demonstrating the core concepts of LangGraph: nodes and edges. Visit lastmileagents.com to visualize and learn how it works.
LangGraph is a framework for building stateful, multi-step applications with language models. It enables you to create intelligent agents that can:
- Break down complex tasks into manageable steps
- Maintain context and state throughout a conversation
- Make decisions based on accumulated information
- Follow structured workflows while remaining flexible
By connecting nodes (processing units) with edges (connections), LangGraph allows you to build sophisticated agent workflows that can handle complex tasks while maintaining a clear, traceable execution path.
The state is the central data structure that flows through the graph. Think of it as a container that holds all the information needed by your application.
class ConversationState(TypedDict):
messages: List[Dict]
current_step: str
thought_process: List[str]Key Points about State:
- State is immutable within each node
- State flows through the graph, accumulating information
- State provides context for each node's processing
- State can be used to track progress and maintain context
Nodes are the processing units of the graph. Each node is a function that:
- Takes the current state as input
- Processes the state
- Returns the updated state
def greet(state: ConversationState) -> ConversationState:
state["thought_process"].append("I should greet the user first")
state["current_step"] = "greeting"
return stateKey Points about Nodes:
- Nodes are pure functions (same input always produces same output)
- Nodes can only access the state passed to them
- Nodes can't communicate directly with other nodes
- Nodes can't modify state outside their scope
Edges define the possible transitions between nodes. They create the structure of your graph.
workflow.add_edge("greet", "ask_question")Key Points about Edges:
- Edges define the flow of execution
- Edges are one-way connections
- Edges determine the sequence of node execution
- Edges can be conditional (though not in this simple example)
In this simple example, we have a linear flow:
[State] → [greet] → [State] → [ask_question] → [State] → [provide_help] → [State] → [summarize] → [Final State]
Each node:
- Receives the current state
- Processes it
- Returns an updated state
- The next node in the sequence receives the updated state
This example demonstrates a simple linear flow. It cannot:
- Make decisions based on state
- Branch into different paths
- Handle complex workflows
- Process multiple paths simultaneously
- State must be serializable
- Large state objects can impact performance
- State updates must be explicit
- No shared state between nodes
- Nodes can't maintain internal state
- Nodes can't communicate directly
- Nodes must be pure functions
- No async operations
This simple linear pattern is best for:
- Straightforward, sequential processes
- Clear, predictable workflows
- Simple state transformations
- Learning and understanding LangGraph basics
- Create a virtual environment:
# Create virtual environment
python -m venv venv
# Activate virtual environment
# On macOS/Linux:
source venv/bin/activate
# On Windows:
# .\venv\Scripts\activate- Install requirements:
pip install -r requirements.txt- Run the example:
python simple_langgraph_demo.pyThe example provides visual feedback about:
- Graph creation and compilation
- State initialization
- Node execution with visual indicators
- State updates at each step
- Final state after graph execution
The output shows:
- Graph setup process
- Initial state creation
- Node execution sequence
- State updates at each step
- Final state summary
Each node execution shows:
- When the node starts
- What state changes it makes
- When the node completes
After understanding this simple example, you might want to explore:
- Conditional branching
- Complex state management
- Error handling
- Async operations
- More complex workflows
Feel free to:
- Add more examples
- Improve documentation
- Add visualization features
- Report issues
- Suggest improvements
This project is licensed under the MIT License.