Skip to main content
Machine-assisted translation of the Hebrew original, awaiting the lecturer's review.

Session 4 — LangGraph

Session 4 of 13Theory and practiceDraft4 min read

Draft for the lecturer's approval. This is preliminary study material. It does not replace guidance published in Moodle, and it does not set any date or grade.

Goals​

  • Move the loop from session 3 into explicit state.
  • Distinguish a node that returns a change from code that changes state in place.
  • Add a different transition when retrieval does not return the needed source.
  • Stop before an action, save the state, and continue after approval.

Why a framework​

The loop from session 3 keeps everything in one list. It is hard to stop in the middle, go back to the same step, or test a node without running the whole story. LangGraph gives this a shape: a state object, nodes, and edges.

A node reads the keys it needs and returns only what changed. The framework merges. Changing the list in place breaks the ability to go back to a saved state.

The framework does not decide for you. The stop condition, the budget and the tool permissions stay in your code, exactly as in the direct loop.

A minimal example​

The state includes the task, the ids already retrieved, a step counter, and an answer. After the retrieval node, a conditional edge asks:

  • Both sources are there: continue to the calculation.
  • A source is missing and budget remains: go back to retrieval with a different query.
  • The budget ran out: an exit node that returns a stop, not a made-up answer.

The following snippet is a sketch of intent, not code the site installs for you:

def route(state):
    if state["answer"]:
        return "end"
    if state["step"] >= state["budget"]:
        return "stop"
    if "C:1" not in state["ids"]:
        return "retrieve"
    return "calculate"

And this is how the nodes are connected into a graph. This is a sketch too: the functions retrieve, calculate and stop are yours.

from langgraph.graph import StateGraph, START, END

graph = StateGraph(State)
graph.add_node("retrieve", retrieve)
graph.add_node("calculate", calculate)
graph.add_node("stop", stop)
graph.add_edge(START, "retrieve")
graph.add_conditional_edges("retrieve", route, {
    "retrieve": "retrieve", "calculate": "calculate", "stop": "stop", "end": END,
})
graph.add_edge("calculate", END)
graph.add_edge("stop", END)

Saving, stopping and resuming​

When you compile the graph with a checkpointer, the state is saved after every node, under a conversation id. You can ask the graph to stop before a given node, for example before a node that sends a message. A person reads the saved state and approves, and the run continues from the same point, without repeating the retrievals.

This stop is only useful if the node after it is the only one that performs the action. If an earlier node already sent, the approval comes too late.

What to check in the lab​

Run the same question over both sources. Save a snapshot of the state after every node. Turn off the edge that goes back to retrieval, and show that the process stops with one source. Then add a stop before the calculation node, resume the run, and check with the counter that retrieval did not run again.

Check your understanding​

The retrieval node adds an id with state["ids"].append and returns nothing. What is the problem?

Pick an answer to see the explanation. There is no grade here.

Reading​

The official documentation: LangGraph, the chapters on persistence and on interrupts. The package version will be published in Moodle before the graded work.

Summary​

Found a mistake on this page? Email the lecturer

Accessibility settings

These settings change how this site looks on this device only. They are saved in this browser.

Text

Colour

Light and dark mode are in the top bar, next to search.

Reading and navigation