Skip to content
kushal.st

Deterministic AI: Why Your Agents Need State Machines

4 min read AI Agents, State machines, LLMs, Architecture

Originally published on Medium - read it there ↗

The “honeymoon phase” of Generative AI is evolving. A year ago, we were mesmerized by the ability of Large Language Models (LLMs) to write poetry or debug code. Today, the focus has shifted from chatting to doing. We want AI Agents that can book flights, process insurance claims, or manage supply chains.

But there is a glaring problem: LLMs are inherently probabilistic. They are “guessers.” And while guessing is great for creative writing, it is a liability for business logic.

If you want your AI agents to be more than just expensive toys, you need to move toward Deterministic AI. You need to stop letting the LLM wander aimlessly and start wrapping it in a State Machine.

The Problem with the “Black Box” Approach

When we build a basic agent, we often give it a “system prompt” and a set of tools, then hope for the best. This is the ReAct (Reason + Act) pattern in its simplest form.

While powerful, this approach has three major flaws:

  1. Infinite Loops: The agent gets stuck trying the same failing tool over and over.
  2. Hallucinated Logic: The LLM decides to skip a crucial validation step because it “thinks” it knows the answer.
  3. Unpredictability: You can’t guarantee that the agent will follow the same path twice, making it impossible to audit or debug in a production environment.

In a world where 99.9% reliability is the standard for software, “usually works” isn’t good enough.

Enter the State Machine

A State Machine is a behavioral model that consists of a finite number of states, transitions between those states, and actions. In the context of AI, it means you define the “tracks” that your agent can run on.

Instead of asking the LLM, “What should I do next?”, you use the LLM to answer specific questions within a controlled environment: “Based on this customer’s email, should I move to the ‘Refund’ state or the ‘Technical Support’ state?”

By using state machines, you gain:

  • Reliability: The agent cannot jump from “Order Received” to “Shipping” without passing through “Payment Confirmed.”
  • Error Handling: You can explicitly define what happens if a tool fails (e.g., “If the API returns a 404, transition to the ‘Human Intervention’ state”).
  • Observability: You can see exactly which state the agent is in at any given millisecond.

Tools of the Trade: LangGraph and n8n

The industry is rapidly shifting toward frameworks that support this structured approach. Two of the most prominent players are LangGraph and n8n.

1. LangGraph (For the Coders)

Built on top of LangChain, LangGraph allows developers to create cyclical graphs. Unlike standard chains, it allows for loops and conditional logic that are essential for agentic behavior. It treats the agent’s “memory” as a shared state that is updated as it moves through the nodes of the graph.

2. n8n (For the Workflow Architects)

n8n is a low-code platform that has embraced “LangChain” nodes. It allows you to visually map out a state machine. You can use an LLM to “route” the workflow, but the actual execution of tasks is handled by deterministic, hard-coded nodes.

The Hybrid Model: “LLM as the Steering Wheel, Logic as the Engine”

The goal isn’t to remove the LLM’s intelligence, but to constrain it.

Think of it like a train. The LLM is the conductor - it makes decisions about when to stop and when to go - but the State Machine is the tracks. The train can go fast, and it can carry a lot of weight, but it can’t suddenly decide to fly off into a forest.

A typical deterministic workflow looks like this:

  1. Input State: User sends a request.
  2. Classification State: LLM identifies intent (Deterministic check: Is the intent valid?).
  3. Action State: A specific tool is called (Deterministic check: Did the tool succeed?).
  4. Review State: A second LLM “critic” checks if the output meets quality standards.
  5. Final State: Response sent to user.

Conclusion: Determinism is the Path to Production

The move from “probabilistic” to “deterministic” is the bridge between a cool demo and a production-ready product. By using state machines, you stop worrying about what the LLM might do and start defining what the agent must do.

If you are building agents today, ask yourself: Is my agent a free-roaming ghost, or is it a professional worker following a blueprint?