The Simplest Graph
- This is a simple graph with 3 nodes and 1 conditional edge.

Key Components
- State
- This object will encapsulate the graph being traversed during excecution.
- The core of our application, defining the flow of the application.
- A custom class representing the states of our planning process.
- The State schema usually serves as the input schema for all Nodes and Edges in the graph.
from typing_extenstions import Typed Dict
class State(TypedDict):
graph_state: str
- Nodes
- In LangGraph, nodes are typically python functions.
- There are two main nodes we will use for our graph:
- The agent node: responsible for deciding what (if any) actions to take.
- The tool node: This node will orchestrate calling the respective tool and returning the output.
- Each node returns a new value of the state key graph_state.
- By default, the new value returned by each node will override the prior state value.
NOTE: Because the state is a TypedDict with schema as defined above, each node can access the key; graph_state, with state['graph_state'].
def node_1(state):
return {"graph_state": state['graph_state'] + " I am"}
def node_2(state):
return {"graph_state": state['graph_state'] + " happy!"}
## def node_3(state):
return {"graph_state": state['graph_state'] + " sad!"}
- Edges
- Defines how the logic is routed and how the graph decides to stop.
- Defines how your agents work and how different nodes communicate with each other.
- There are a few key types of edges:
- Normal Edges: Go directly from one node to the next.
- Conditional Edges: Call a function to determine which node(s) to go to next.
- Entry Point: Which node to call first when user input arrives.
- Conditional Entry Point: Call a function to determine which node(s) to call first when user input arrives.
import random
from typing import Literal
def decide_mood(state) -> Literal["node_2", "node_3"]:
# Often, We will use state to decide on the next node to visit
user_input = state['graph_state']
print("user input: ", user_input)
# Here, let's just do a 50 50 split between nodes 2, 3
if random.random() < 0.5:
return "node_2"
return "node_3"
-
LLM Integration: Utilizing a language model to generate the final itinerary.
-
Memory Integration: Utilizing long term and short term memory for conversations
Graph Construction
- We build the graph from our components (basically with State, Nodes, and Edges)
- The StateGraph class is the graph class that we can use.
- First, we initialize a StateGraph with the State class we defined.
- Then, we add our nodes and Edges.
- We use the START node, a special node that sends user input to the graph, to indicate where to start our graph.
- The END node is a special node that represents a terminal node.
- Finally, we compile our graph to perfrom a few basic checks on the graph structure.
- We can visualize the graph as a Mermaid diagram.
from IPython.display import Image, display
from langgraph.graph import StateGraph, START, END
# Build graph
builder = StateGraph(State)
builder.add_node("node_1", node_1)
builder.add_node("node_2", node_2)
builder.add_node("node_3", node_3)
# Logic
builder.add_edge(START, "node_1")
builder.add_conditional_edges("node_1", decide_mood)
builder.add_edge("node_2", END)
builder.add_edge("node_3", END)
# Add
graph = builder.compile()
# View
display(Image(graph.get_graph().draw_mermaid_png()))

Graph Invocation
- The compiled graph implements the runnable protocol.
- This provides a standard way to execute LangChain components.
- invoke is one of the standard methods in this interface.
- The input is a dictionary {"graph_state": "Hi, this is Lance."}, which sets the initial value for our graph state dict.
- When invoke is called, the graph starts execution from the START node.
- It progresses through the defined nodes (node_1, node_2, node_3) in order.
- The conditional edge will traverse from node 1 to node 2 or 3 using a 50 50 decision rule.
- Each node function receiveds the current state and overrides it.
- The execution continues until it reaches the END node.
graph.invoke({"graph_state": "Hi, this is Lance."})
{'graph_state': 'Hi, thisis Lance. I am happy!'}