Understanding LangGraph Components
LangGraph is built on two fundamental components that work together to create intelligent workflows:
Nodes
Nodes are Python functions that perform specific tasks. Each node:
- Receives state as input
- Processes information
- Returns updated state
- Can call external tools or APIs
Edges
Edges connect nodes and define the flow of execution:
- Normal Edges: Direct connections between nodes
- Conditional Edges: Route based on logic or state
Think of it like a flowchart where nodes are the boxes and edges are the arrows connecting them.
In a customer service workflow, what would a node typically represent?
Building Your First LangGraph
Let's build a simple graph step by step. We'll create a basic workflow that processes user input and makes decisions.
from langgraph.graph import StateGraph, State, START, END
# Step 1: Define the state structure
class State(TypedDict):
messages: list
user_input: str
decision: str
# Step 2: Create node functions
def process_input(state: State) -> State:
# Process the user input
state["decision"] = "processed"
return state
def make_decision(state: State) -> State:
# Make a decision based on processed input
if len(state["user_input"]) > 10:
state["decision"] = "complex"
else:
state["decision"] = "simple"
return stateWhat does the State class represent in this code?
Connecting Nodes with Edges
Now let's connect our nodes to create a functioning graph:
# Step 3: Build the graph
builder = StateGraph(State)
# Step 4: Add nodes
builder.add_node("process", process_input)
builder.add_node("decide", make_decision)
# Step 5: Add edges
builder.add_edge(START, "process") # Start → Process
builder.add_edge("process", "decide") # Process → Decide
builder.add_edge("decide", END) # Decide → End
# Step 6: Compile the graph
graph = builder.compile()
# Step 7: Run the graph
result = graph.invoke({"user_input": "Hello AI", "messages": []})In this example, what is the execution order of nodes?
Working with Tools in LangGraph
One of LangGraph's powerful features is the ability to integrate tools. Tools are functions that agents can call to perform specific tasks:
# Define a tool
def calculator(expression: str) -> float:
"""Evaluate a mathematical expression"""
return eval(expression) # Note: Use safe evaluation in production
def weather_api(city: str) -> dict:
"""Get weather for a city"""
# In real implementation, this would call an API
return {"city": city, "temp": 72, "condition": "sunny"}
# Create a node that uses tools
def tool_using_node(state: State) -> State:
user_query = state["user_input"]
if "weather" in user_query.lower():
result = weather_api("New York")
state["messages"].append(f"Weather: {result}")
elif "calculate" in user_query.lower():
result = calculator("2 + 2")
state["messages"].append(f"Result: {result}")
return stateTools in LangGraph are most useful when:
Implementing Conditional Routing
Conditional routing allows your graph to make decisions about which path to take based on the current state:
# Define a routing function
def route_based_on_input(state: State) -> str:
user_input = state["user_input"].lower()
if "urgent" in user_input:
return "high_priority"
elif "question" in user_input:
return "faq_handler"
else:
return "standard_process"
# Add conditional routing to the graph
builder.add_conditional_edges(
"intake_node",
route_based_on_input,
{
"high_priority": "urgent_handler",
"faq_handler": "faq_node",
"standard_process": "normal_flow"
}
)What does conditional routing determine in a LangGraph?