Course contentsShow
← AI Engineering
Lesson 1781 of 1,88643. Workflow Orchestration and State MachinesFree lesson

Defining States and Transitions for AI Agents

Practical patterns for defining agent states (planning, tool-calling, reflecting) and transition conditions based on LLM outputs.

Defining States and Transitions for AI Agents

What you'll learn: How to structure AI agent behavior using discrete states and explicit transition rules based on LLM outputs.

Core Concept

Think of an AI agent as a traffic light—it can be in specific states (red, yellow, green) and transitions between them follow clear rules. In AI workflows, common agent states include:

  • Planning: Agent decides what to do next
  • Tool-calling: Agent executes a function or API call
  • Reflecting: Agent evaluates its own output or results
  • Responding: Agent generates final user-facing output
  • Waiting: Agent pauses for external input

The magic lies in transition conditions—rules that determine when to move between states. These are typically based on LLM output patterns.

Practical Pattern

When an LLM returns structured output, you examine it to decide the next state:

Current State: Planning
LLM Output: {"action": "search", "query": "weather"}
Transition Rule: IF action exists → Tool-calling state
Next State: Tool-calling

Current State: Tool-calling
Tool Result: {"temp": 72, "condition": "sunny"}
Transition Rule: IF tool succeeded → Reflecting state
Next State: Reflecting

Current State: Reflecting
LLM Output: {"sufficient": true}
Transition Rule: IF sufficient=true → Responding state
Next State: Responding

You can implement transition logic by parsing JSON fields, checking for specific keywords, or using regex patterns on the LLM's text output. The key is making transitions explicit and deterministic rather than implicit—if the agent needs information, it enters "tool-calling"; if it needs to think, "reflecting"; if it's done, "responding."

Key Takeaway: State machines for AI agents require clearly defined states (planning, tool-calling, reflecting) and explicit transition conditions based on parseable LLM outputs, making agent behavior predictable and debuggable.