What is state machines in AI Agents? A Guide for engineering managers in lending

By Cyprian AaronsUpdated 2026-04-21
state-machinesengineering-managers-in-lendingstate-machines-lending

State machines are a way to model an AI agent as a set of named states, with rules that control when it can move from one state to another. In AI agents, a state machine keeps the agent’s behavior predictable by limiting what it can do next based on the current step in the workflow.

How It Works

Think of a state machine like a lending application checklist.

A loan application does not jump randomly from “submitted” to “approved.” It moves through defined stages: intake, document verification, credit check, underwriting, decision, and funding. Each stage has clear entry conditions and exit conditions.

That is exactly how a state machine works in an AI agent:

  • The agent is always in one state
  • Each state has allowed actions
  • Events or outputs trigger transitions to the next state
  • Invalid transitions are blocked

For an engineering manager, the value is control. You are not asking the model to “figure it out” every turn. You are telling it: “You are in document review now. Only do document review tasks until the required checks pass.”

A simple example:

StateWhat the agent doesNext transition
IntakeCollect borrower detailsApplication complete
VerificationCheck ID, income, bank statementsDocuments valid
Risk ReviewSummarize risk signalsRisk acceptable
Decision DraftingPrepare approve/decline recommendationHuman review done
ClosedFinalize outcome and log decisionEnd

This matters because AI agents are not reliable when they improvise across open-ended workflows. Lending workflows are regulated, auditable, and full of edge cases. A state machine gives you a guardrail system around the model.

A useful analogy: think of a train network.

The train can only go where the track allows. The driver does not decide to teleport to another station because the passenger asked nicely. In the same way, your AI agent should not skip underwriting because it “feels confident.” The current state determines what comes next.

Why It Matters

  • It reduces workflow drift

    • Without state control, agents start mixing tasks: summarizing files, making decisions, and sending emails in the wrong order.
    • In lending, that creates compliance risk and broken customer experiences.
  • It makes audit trails easier

    • Every transition can be logged: who or what moved the case from verification to underwriting.
    • That is useful for model governance, dispute handling, and internal audits.
  • It improves human-in-the-loop design

    • You can force escalation at specific states like adverse action drafting or exception handling.
    • Managers get predictable review points instead of random interruptions.
  • It helps engineers separate concerns

    • The LLM handles language-heavy work.
    • The state machine handles orchestration.
    • That separation makes systems easier to test and maintain.

Real Example

Consider a mortgage prequalification agent at a lender.

The business goal is simple: help borrowers submit clean applications faster without letting the model make unsupported decisions.

Here is how a state machine could structure the agent:

  1. Start

    • Borrower starts chat or uploads documents.
  2. Collect Information

    • Agent asks for income, employment status, property type, debt obligations.
    • If required fields are missing, stay in this state.
  3. Validate Documents

    • Agent checks whether pay stubs, bank statements, and ID documents are present.
    • If OCR confidence is low or files look incomplete, transition to Request More Docs.
  4. Preliminary Risk Summary

    • Agent summarizes debt-to-income ratio inputs and flags obvious issues.
    • It does not approve or decline yet.
  5. Human Review

    • A loan officer reviews exceptions or borderline cases.
    • If approved for next step, move forward; otherwise route back for more information or close out.
  6. Decision Packet Drafting

    • Agent prepares an internal summary for underwriting or prequal communication.
    • This includes extracted data, missing items, and key risk notes.
  7. End

    • Case is handed off or closed with logging completed.

A simplified version in code looks like this:

from enum import Enum

class State(Enum):
    START = "start"
    COLLECT_INFO = "collect_info"
    VALIDATE_DOCS = "validate_docs"
    RISK_SUMMARY = "risk_summary"
    HUMAN_REVIEW = "human_review"
    END = "end"

def next_state(current_state, event):
    transitions = {
        (State.START, "begin"): State.COLLECT_INFO,
        (State.COLLECT_INFO, "info_complete"): State.VALIDATE_DOCS,
        (State.VALIDATE_DOCS, "docs_valid"): State.RISK_SUMMARY,
        (State.VALIDATE_DOCS, "docs_missing"): State.COLLECT_INFO,
        (State.RISK_SUMMARY, "needs_review"): State.HUMAN_REVIEW,
        (State.HUMAN_REVIEW, "approved"): State.END,
        (State.HUMAN_REVIEW, "rejected"): State.END,
    }
    return transitions.get((current_state, event), current_state)

The point is not the code itself. The point is that the agent’s behavior becomes deterministic enough to operate inside lending workflows where mistakes have cost.

Without this structure, an LLM might:

  • ask for already-collected documents again
  • skip mandatory disclosures
  • generate approval language too early
  • fail to escalate exceptions

With a state machine:

  • each step is explicit
  • retries are controlled
  • approvals require the right conditions
  • compliance checks happen in sequence

Related Concepts

  • Finite State Machines

    • The classic computer science version of this pattern.
    • Useful when you want strict workflow control with limited branching.
  • Workflow Orchestration

    • The broader system that coordinates tools, models, humans, and external APIs.
    • State machines often sit inside orchestration layers.
  • Human-in-the-Loop

    • A design pattern where people review high-risk or ambiguous steps.
    • Common in lending for exceptions and adverse decisions.
  • Guardrails

    • Constraints that prevent unsafe or non-compliant model behavior.
    • State machines are one form of guardrail.
  • Event Sourcing / Audit Logging

    • Recording every meaningful transition in a case lifecycle.
    • Important for traceability in regulated financial services.

Keep learning

By Cyprian Aarons, AI Consultant at Topiax.

Want the complete 8-step roadmap?

Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.

Get the Starter Kit

Related Guides