# Command: Update State and Route Together — LangGraph Agents & Multi-Agent Systems

Source: https://www.geekswithgeeks.com/en/langgraph-agents/f-command

> Let a node decide where to go next while updating state.

## Routing inside the node

A node can return a **`Command(update=..., goto=...)`** instead of a plain dict: it updates the state **and** names the next node. This is useful when the node that did the work is best placed to decide what happens next, and it is the basis of **hand-offs** between agents (an agent routes to another agent with the data it gathered). Annotate the return type, for example `Command[Literal["writer", "__end__"]]`, so the graph can be drawn and validated. Conditional edges and `Command` can coexist; use `Command` for dynamic hand-offs, edges for fixed structure.

## A supervisor routing with Command, run

I ran this offline with langgraph 1.2.12 and langchain-core 1.6.6 in a Python virtual environment. No API key or model is needed because plain Python functions stand in for the model, so the output is repeatable. The `supervisor` sends research tasks to `researcher` and others to `writer`; each worker updates `result` and hands control back, and the supervisor ends the run once a result exists.

```python
from typing import Literal, TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.types import Command

class State(TypedDict):
    task: str
    result: str

def supervisor(state) -> Command[Literal["researcher", "writer", "__end__"]]:
    if not state.get("result"):
        return Command(goto="researcher" if "find" in state["task"] else "writer")
    return Command(goto=END)

def researcher(state) -> Command[Literal["supervisor"]]:
    return Command(update={"result": "facts about " + state["task"]}, goto="supervisor")

def writer(state) -> Command[Literal["supervisor"]]:
    return Command(update={"result": "draft for " + state["task"]}, goto="supervisor")

g = StateGraph(State)
g.add_node("supervisor", supervisor); g.add_node("researcher", researcher); g.add_node("writer", writer)
g.add_edge(START, "supervisor")
app = g.compile()
for task in ("find the 2025 leave policy", "write a welcome note"):
    print(task, "->", app.invoke({"task": task, "result": ""})["result"])

```

Output:

```
find the 2025 leave policy -> facts about find the 2025 leave policy
write a welcome note -> draft for write a welcome note
```

## Annotate the Command return type

Literal node names in the annotation let LangGraph draw and validate the possible destinations.

**Quiz:** What does returning Command(update=..., goto=...) do?

- [ ] Calls the model twice
- [ ] Deletes the graph
- [x] Updates the state and chooses the next node in one step
- [ ] Sets the recursion limit

*Answer:* Updates the state and chooses the next node in one step. It combines a state write with a routing decision.
