Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Building an Agent State Graph: Edges and Cycles

00:00 Having created a workflow with nodes in the previous lesson, you’ll now add the connections between the nodes called the edges. There are two types of edges, as you might remember.

00:10 There are normal or fixed edges. They connect one node directly to another, and the workflow always follows this path after the source node finishes executing.

00:20 Now, this type of edge is represented by a solid arrow, and for example, the arrow from start to email_agent and from email_tools to email_agent.

00:31 There are also conditional edges, which path to choose is conditional on something, so they use a routing function to decide which node to visit next. There’s also cycles.

00:42 Earlier in the course, you saw that LangGraph was especially useful when modeling nonlinear processes, for example, with cycles and conditional branches.

00:52 Now, a cycle is where there are edges back and forth between two nodes. So in your example here, there’s a cycle between email_agent and email_tools.

01:02 And in your case, the cycle has both fixed and conditional edges. Okay, let’s go to the code.

01:11 So in your code, just like you added nodes, you’re now adding edges. For normal or fixed edges, you just enter the start and the end node of the edge. Now, on line 137, for example, you’re adding a normal edge from START to email_agent.

01:28 We’re using the .add_edge() method of the workflow object. We’re skipping 138 for a second, but in line 141, you’re adding the normal edge from email_tools to email_agent.

01:44 Now, back to line 138, that’s where you’re adding a conditional edge, and you use the .add_conditional_edges() method. And that needs three input parameters.

01:58 Those are on line 139. The first is the node to start from. The second is the routing function, which decides which node you visit next. And the third is then the path map, which maps what is returned by your routing function to the node name the process needs to go according to the model’s decision.

02:21 So in your case here, the routing function is route_agent_graph_edge, and that returns either the string email_tools or END.

02:32 Now, the string email_tools and END are the exact names of the nodes to go to. So in this case, you can use a list to gather these outcomes.

02:45 Later in the course, you will see that if the routing function returns strings that are not the exact names of the nodes, you will need a dictionary to map those outcomes to the node names, but that’s for later.

02:58 Okay, that’s your edges done. You see that you don’t have to explicitly implement a cycle. You’ve implemented the normal and the conditional edges that make up the cycle.

03:08 So that’s all you have to do with edges. And then finally, on line 143, you compile the workflow and store that graph in a variable called email_agent_graph.

03:22 Over the next few lessons, you’ll peel back a layer of the onion and start addressing the actions of the nodes. So that’s call_agent_model_node and tool_node.

03:36 And then the routing function route_agent_graph_edge. I’m skipping the underscores as it gets too long from line 139.

Become a Member to join the conversation.