The Building Blocks of LangGraph (Part 0)
Author(s): Bessie Delight Kekeli Originally published on Towards AI. The Building Blocks of LangGraph (Part 0) For other parts of the series : Part 0 , Part 1 , Part 2 , Part 3 As Large Language Models (LLMs) have become more capable, developers have moved beyond simple chatbots and begun building systems that can reason, make decisions, use tools, retrieve information, interact with APIs, and collaborate with other AI agents. Building these systems introduces a new challenge: How do we coordinate and manage the flow of intelligence? This is the problem that LangGraph was created to solve. At its core, LangGraph is a framework for building stateful, controllable, and production-ready AI workflows. It allows developers to define how AI agents think, make decisions, communicate with tools, and move through complex tasks. If LangChain helps you connect AI components together, LangGraph helps you orchestrate how those components behave over time. LangGraph is an orchestration framework built by the team behind LangChain. It allows developers to model AI applications as a graph The Simplest Graph(agent flow) Let’s build a simple graph with 3 nodes and one conditional edge. The easiest way to understand nodes, edges, and state is to imagine a food delivery process. A node is simply a task or action that does something. For example: Receive Order is a node. Prepare Food is another node. Deliver Food is another node. Every time some work is performed, you are at a node. An edge is the path that tells the system where to go next. For example: Receive Order ↓Prepare Food ↓Deliver Food Those arrows are the edges. The edge is not doing any work itself. It simply says: “After this step finishes, go to that step.” Think of an edge as a road connecting two cities. The cities are the nodes, and the road is the edge. A state is the information that travels through the entire process. Imagine a customer orders: PizzaAddress: 123 Main StreetCustomer: John When the order is received, that information enters the system. As the order moves from: Receive Order ↓Prepare Food ↓Deliver Food the information moves along with it. That information is the state. Let’s build our first simple agent State Think of state as the graph’s shared memory. It is the information that travels through the workflow as it moves from one node to another. Every node can read the state, update it, and pass the updated version to the next node. In this example, the state contains a single piece of information called graph_state. First, define the State of the graph. The State schema serves as the input schema for all Nodes and Edges in the graph. Let’s use the TypedDict class from python's typing module as our schema, which provides type hints for the keys. from typing_extensions import TypedDictclass State(TypedDict): graph_state: strNodes Nodes A node is simply a function that performs some work. When a node runs, it receives the current state, does something with it, and returns an updated state. You can think of a node as a worker in a factory. The worker receives a package (the state), modifies it, and then passes it along. The first positional argument is the state, as defined above. Because the state is a TypedDict with schema as defined above, each node can access the key, graph_state, with state['graph_state']. 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. def node_1(state): print("---Node 1---") return {"graph_state": state['graph_state'] + "I am"}def node_2(state): print("---Node 2---") return {"graph_state": state['graph_state'] + "happy!"}def node_3(state): print("---Node 3---") return {"graph_state": state['graph_state'] + "sad!"} Edges An edge is simply a connection between nodes. It tells the graph where to go after a node finishes its work. A normal edge is a fixed path. After one node completes, the graph always moves to the same next node. For example, if a workflow has “Collect Data” followed by “Analyze Data,” the graph will always move from the first node to the second. A conditional edge is a decision point. Instead of always following the same path, the graph looks at the current state and decides where to go next. For example, after analyzing data, the graph might ask: “Do I have enough information?” If the answer is yes, it moves to “Generate Report.” If the answer is no, it moves back to “Collect More Data.” Conditional edges are implemented as functions that return the next node to visit based on some logic. import randomfrom typing import Literaldef 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'] # Here, let's just do a 50 / 50 split between nodes 2, 3 if random.random() < 0.5 # 50% of the time, we return Node 2 return "node_2" # 50% of the time, we return Node 3 return "node_3" Graph Construction Now, we build the graph from our components defined above. The StateGraph class is the graph class that we can use. First, we initialize a StateGraph with the State class we defined above. 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 perform a few basic checks on the graph structure. We can visualize the graph as a Mermaid diagram. from IPython.display import Image, displayfrom langgraph.graph import StateGraph, START, END#Build Graphbuilder = StateGraph(state)builder.add_node("node_1", node_1)builder.add_node("node_2", node_2)builder.add_node("node_3", node_3)#Logicbuilder.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)#Addgraph = builder.compile()#Viewdisplay(Image(graph.get_graph().draw_mermaid_png())) #OUTPUT 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 […]
