ReviseAlgo Logo

Building AI Agents

LangGraph & State Machines

Building cyclical, stateful agent workflows with graph-based state machines.

Interview: High - Most enterprise agent systems use graph-based state machines.

Why LangGraph?

Traditional agent executors are "while loops" that run until a stopping condition. They are hard to debug, hard to control, and prone to infinite loops. LangGraph models agent workflows as state machines (directed graphs).

Core Concepts

  • State: A Python dict or Pydantic model holding the current context. Passed between nodes.
  • Nodes: Functions that receive state, perform actions (LLM call, API call), and return updated state.
  • Edges: Rules dictating which node runs next.
  • Conditional Edges: Dynamic routing based on state — enables loops and branching.

Human-in-the-Loop (HITL)

LangGraph can pause execution and wait for human approval before proceeding. Combined with a durable Checkpointer (Postgres), execution state is saved before destructive actions. This unlocks native approval workflows for payments, deployments, and refunds.

DSPy: Compile Prompts, Don't Hack Them

Senior engineers use frameworks like DSPy to treat prompts as learnable parameters. DSPy separates logical execution from instructions, dynamically compiling and optimizing the highest-accuracy few-shot combinations against test schemas.

Use Cases

Customer support bots that escalate to humans for refunds or cancellations

Code generation workflows that compile, test, and loop back on errors

Research agents that search → synthesize → fact-check in cycles

Multi-agent supervisor graphs with LangGraph sub-graphs

Common Mistakes

Putting complex logic in generic agent executors instead of typed state graphs

Not defining the State schema clearly — leads to runtime KeyError crashes

Failing to set max_iterations on cyclic graphs — infinite loops will drain your budget

Not using checkpointing for any workflow with destructive side effects