Real agents aren't linear sequences of steps: they branch, repeat actions, and need to remember context. LangGraph models this explicitly as a state machine.
LangGraph represents an agent as a directed graph where nodes are functions (or LCEL chains) that receive and modify a shared state, and edges determine which node runs next. Unlike an LCEL chain, edges can be conditional and can form cycles: the graph can return to an already-visited node as many times as the logic requires.
State is the central element. It's typically defined with `TypedDict`, and each node receives the full state, returning only the keys it modifies; LangGraph takes care of merging that update with the existing state.
Using `Annotated[..., operator.add]` is the least intuitive part for anyone coming from classic imperative programming: it tells LangGraph that when a node returns `{"messages": [new_message]}`, it should concatenate that list onto the existing state instead of overwriting it. Without that reducer, every node update would replace the entire history.
Note the explicit cycle: `respond` goes back to `analyze`, which can route back to `respond` again. This is exactly what a classic `AgentExecutor` did opaquely under the hood; LangGraph makes it visible and editable.
The `.stream()` method is particularly useful for debugging: it shows the full state after each node, making it visible exactly where in the cycle the agent made a wrong decision — something that's much harder to trace in a manual implementation with a `while` loop and boolean flags.
A `StateGraph` compiled with a checkpointer keeps state across calls, identified by a `thread_id`, which is the foundation of conversational memory (covered in detail in the next article in this series):
A state graph is the right tool when the agent's behavior depends on accumulated history, not just the last message: a support agent that needs to remember how many times it already tried to resolve an issue before escalating to a human, a research pipeline that iterates over searches until it gathers enough evidence, or any flow where "how many times have we already been here" changes the next decision. If your flow doesn't have this characteristic, a simple LCEL chain is still the more maintainable choice.
Carlos Montiel is an enterprise AI solutions architect. He implements LLMs, Agents, RAG, and orchestrators for companies across Guatemala and Latin America. Reach out for a consultation.
Contact Carlos Montiel