Module 4 · Human-in-the-loop

Editing state mid-execution

5 min read

Approving or rejecting isn't always enough. The supervisor sees that the agent picked the wrong item, that an order number was misextracted, or that a draft answer contains a mistake: they want to correct, then let the graph continue. LangGraph offers two ways to modify the state of a running execution: through the answer to an interrupt, planned in the code, or through a direct update of the state, from outside.

First way: editing planned by the node

The safest is to plan editing in the interrupt's contract. That is what Relay does with the edit decision:

answer = interrupt(question)   # {"decision": "edit", "amount": 349.0, "reviewer": "alice"}
...
amount = float(answer["amount"]) if answer["decision"] == "edit" else refund["amount"]

The node controls what can be changed (the amount), validates the change (never above the proposed amount), and draws the consequences (skipping finance approval if the amount drops below the threshold). Editing is part of the business logic, tested like everything else.

This pattern generalizes: for a human to correct a draft answer, the approval node presents the draft in the question and accepts a corrected version in the answer. To correct tool calls before they run, the node presents the planned tool_calls and accepts modified arguments.

Second way: update_state from outside

Sometimes the correction wasn't planned. An operator notices, during a pause, that a state field is wrong. They can correct it directly with update_state, then resume:

snapshot = await graph.aget_state(config)
await graph.aupdate_state(config, {"order_id": "ORD-1001"})
await graph.ainvoke(None, config, context=ctx)   # resume after a static breakpoint

This is the fork mechanism (lesson 3.4) applied to the latest checkpoint: the update goes through the reducers, it is attributed to a node (as_node), and it creates a new checkpoint with source update, visible in the history.

Editing the state of a paused subgraph

In Relay, the pause happens inside the refund subgraph: that is where the state to correct (refund) lives. An update on the parent's configuration wouldn't reach it. You must target the subgraph's checkpoint, obtained with subgraphs=True:

parent = await graph.aget_state(config, subgraphs=True)
sub = parent.tasks[0].state                      # the subgraph's state, paused on supervisor_review
refund = dict(sub.values["refund"])
refund["lines"] = [line for line in refund["lines"] if line["sku"] == "CHAIR-7"]   # remove the desk
refund["amount"] = 349.0
await graph.aupdate_state(sub.config, {"refund": refund})

# Then the supervisor approves: the corrected amount no longer requires finance
result = await graph.ainvoke(Command(resume={"decision": "approve", "reviewer": "alice"}), config,
                             context=ctx, version="v2")
# result.value["status"] == "refunded", amount refunded: €349

sub.config contains the subgraph's namespace (checkpoint_ns): the update is applied in the right place, and the resume happens normally on the parent's configuration.

Comparing the two ways

Planned editing (answer to interrupt)External update_state
What can be changedWhat the node acceptsAny state key
ValidationIn the node, testedUp to the caller
Business consequencesDrawn by the node (e.g. skip finance)Only if the graph reads the value again later
TraceabilityIn business state (approvals) and the auditAn update checkpoint in the history
UseThe normal case, in productionExceptional corrections, level-2 support, debugging

The rule: what happens regularly must be planned in the graph. If operators often correct the same field via update_state, it's a sign an option is missing from the question asked to the human.

The risks of direct editing

update_state is powerful, hence dangerous. Three precautions:

  • Respect invariants. The graph assumes consistencies: in Relay, refund["amount"] must equal the sum of the retained lines. An update changing one without the other creates an inconsistent state the rest of the graph won't detect. In the example above, we update both.
  • Understand as_node. By default, the update is attributed to the last node that ran. If you attribute it to another node, that node's outgoing edges apply, and the graph may take another path. For a simple value correction during a pause, keep the default.
  • Control access. An API that lets you modify a graph's state lets you bypass all business logic (approvals included). Reserve it for internal roles, and log each change with its author.

Correcting the agent's actions before they go out

A very common editing case deserves a mention: reviewing tool calls before they run. With an interrupt_before=["tools"] breakpoint, the state's last message contains the planned tool_calls. To change an argument, replace the message with a corrected version with the same id — the add_messages reducer then replaces the message instead of appending one:

snapshot = await graph.aget_state(config)
last = snapshot.values["messages"][-1]
fixed_calls = [{**call, "args": {**call["args"], "order_id": "ORD-1001"}} for call in last.tool_calls]
await graph.aupdate_state(config, {"messages": [last.model_copy(update={"tool_calls": fixed_calls})]})
await graph.ainvoke(None, config, context=ctx)

It's a remarkable debugging tool. In production, for sensitive tools, prefer an explicit approval with interrupt() in a dedicated node that presents the planned calls and accepts corrected arguments.

Key takeaways

  • Two ways: planned editing in the answer to interrupt(), and external updates with update_state.
  • Planned editing is validated and tested: it's the normal case in production.
  • For a paused subgraph, target its checkpoint: aget_state(config, subgraphs=True) then aupdate_state(sub.config, ...).
  • update_state must respect state invariants, be reserved for internal roles and be logged.
  • A message with the same id replaces the original: that's how you correct tool calls before they run.

Practice

  1. Reproduce the subgraph edit above in a test, and check the refunded amount and the audit log.
  2. Add a remove_line decision to _ask_reviewer that removes an item (by SKU) and recomputes the amount.
  3. With interrupt_before=["tools"], correct the order number of a lookup_order call before it runs.

Resources