Inferensys

Prompt

Agent Execution State Machine Prompt Template

A practical prompt playbook for defining and validating agent execution state machines in production AI workflows.
Developer designing multi-agent workflow on laptop, architecture diagram on screen, casual home office setup with afternoon light.
PROMPT PLAYBOOK

When to Use This Prompt

Defines the ideal use case, user, and boundaries for the Agent Execution State Machine prompt.

This prompt is for AI platform engineers and workflow builders who need to define a strict lifecycle for an agent. Use it when an agent must move through a known set of states (idle, running, waiting_for_tool, completed, failed) and you need the model to enforce valid transitions, execute entry and exit actions, and reject invalid state changes. The primary job-to-be-done is generating a machine-readable state machine definition that your orchestration layer can parse and enforce deterministically, removing ambiguity from the agent's execution contract.

The ideal user is implementing a production agent harness where the execution graph is managed by an external runtime (e.g., Temporal, Prefect, a custom orchestrator). The required context includes the list of valid states, the events or triggers that cause transitions, and the side effects (tool calls, logging, human approvals) that must occur on entry or exit. This prompt is not for ad-hoc agent loops or freeform reasoning where the model decides its own execution flow. It is for systems where the execution contract must be deterministic and auditable, such as financial transaction agents, healthcare data processors, or any workflow requiring SOC 2 or similar compliance evidence.

Do not use this prompt when the agent's path is exploratory, when states are discovered at runtime, or when you are prototyping a single-agent loop. In those cases, a planning or task decomposition prompt is more appropriate. Before using the generated state machine, validate it against your orchestration engine's schema, test every transition with a dry-run harness, and ensure that unreachable states and missing error transitions are caught by your eval suite. The output is a specification, not a runtime—your application code must enforce it.

PRACTICAL GUARDRAILS

Use Case Fit

Where the Agent Execution State Machine prompt works, where it fails, and what you must have in place before using it.

01

Good Fit: Deterministic Workflow Engines

Use when: you are building a production agent harness that must enforce explicit lifecycle states (pending, running, waiting_for_tool, completed, failed) with strict transition rules. Why: the prompt produces a machine-readable state machine definition that your executor can validate against, preventing illegal transitions like failed → completed.

02

Bad Fit: Ad-Hoc Chat Agents

Avoid when: you are building a conversational agent with free-form tool use and no formal execution contract. Why: the state machine prompt adds rigidity that fights the flexibility of open-ended chat. Use it only when you need enforceable execution guarantees, not for exploratory agent behavior.

03

Required Input: Agent Capability Contract

What to watch: the prompt cannot invent valid states without knowing what your agent can do. Guardrail: provide a typed capability contract listing available tools, their possible outcomes (success, timeout, permission_denied), and any external system constraints before generating the state machine.

04

Operational Risk: Unreachable States

What to watch: the generated state machine may include states that no valid transition path can reach, creating dead code in your executor. Guardrail: run a graph reachability check on every generated state machine before deployment. Flag any state with zero inbound transitions from the initial state.

05

Operational Risk: Missing Transition Coverage

What to watch: the prompt may omit transitions for error conditions like tool timeout, partial failure, or retry exhaustion. Guardrail: maintain a checklist of required error transitions (timeout, max_retries_exceeded, permission_denied, invalid_input) and validate that every generated state machine covers them.

06

Integration Dependency: Executor Validation Loop

What to watch: the state machine definition is useless without a runtime executor that enforces transitions. Guardrail: pair this prompt with an execution harness that validates every attempted transition against the generated definition, logs violations, and escalates illegal transitions before any side effect occurs.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A copy-ready system prompt that instructs a model to act as a strict agent execution state machine, enforcing valid states, transition guards, and error handling.

This template defines the core contract for an agent that must operate within a finite set of states. It is designed to be pasted directly into your system instructions or developer message. The prompt forces the model to track its current state, validate every transition against a predefined set of rules, and execute entry or exit actions. This is critical for workflows where an agent skipping a step or entering an invalid state would corrupt a multi-step process, such as a loan application review, a deployment pipeline, or a customer onboarding flow.

Below is the copy-ready template. Replace every square-bracket placeholder with the specific states, events, and constraints of your agent's lifecycle. The [STATE_DEFINITIONS] block is the heart of the machine; define each state with its valid transitions and associated actions. The [TRANSITION_GUARDS] block adds conditional logic that must be true before a transition is allowed. The [ERROR_HANDLING] block tells the agent what to do when an invalid transition is requested, preventing silent failures.

text
You are an Agent Execution State Machine. Your behavior is strictly governed by a finite set of states and transitions. You must never enter a state not defined below.

## State Definitions
Your current state is tracked in a `current_state` variable. You must output your state after every action.

[STATE_DEFINITIONS]
# Example:
# IDLE: The initial state. Valid transitions: [RECEIVED_REQUEST].
#   Entry Action: Log "System ready."
# ANALYZING: The agent is processing the request. Valid transitions: [ANALYSIS_COMPLETE, ERROR_ENCOUNTERED].
#   Entry Action: Run the `decompose_task` tool.
#   Exit Action: Save the analysis result to `task_context`.
# COMPLETED: Terminal state. No valid transitions.
#   Entry Action: Run the `finalize_output` tool.

## Transition Guards
A transition is only valid if the guard condition evaluates to TRUE.

[TRANSITION_GUARDS]
# Example:
# IDLE -> ANALYZING: Guard `request_is_valid` must be TRUE.
# ANALYZING -> COMPLETED: Guard `analysis_confidence` must be greater than 0.9.

## Error Handling
If an invalid transition is requested, you must not execute it. Instead, transition to the `ERROR` state and report the violation.

[ERROR_HANDLING]
# Example:
# On Invalid Transition: Set state to `ERROR`, log the attempted transition from `current_state` with the invalid event, and output an `InvalidTransitionError` payload.

## Input
You will receive a series of events. For each event, determine the next state based on the current state and the event, validate the transition guard, execute entry/exit actions, and output the new state.

[INPUT]

After pasting the template, the most critical adaptation step is defining the [TRANSITION_GUARDS]. A state machine without guards is just a flowchart, and an LLM can easily hallucinate a transition if the guard isn't explicit. For high-risk workflows, the guard should reference a verifiable fact, such as a tool output or a field in the task_context. The [ERROR_HANDLING] block is your safety net; always define a default escalation path, such as transitioning to a HUMAN_REVIEW state, to prevent the agent from getting stuck in an error loop. Before deploying, run a suite of evals that deliberately request invalid transitions to confirm the agent refuses them and follows the error protocol.

IMPLEMENTATION TABLE

Prompt Variables

Required inputs for the Agent Execution State Machine Prompt Template. Each placeholder must be populated before the prompt is assembled and sent to the model. Missing or malformed variables will cause state machine validation failures.

PlaceholderPurposeExampleValidation Notes

[AGENT_ROLE]

Defines the agent's purpose, capabilities, and boundaries within the state machine

Customer Support Triage Agent: classifies inbound tickets, routes to specialist, or resolves Tier-1 issues

Must be a non-empty string. Should include scope boundaries. Validate against role registry if using multi-agent systems

[STATE_DEFINITIONS]

Ordered list of valid states with entry conditions, exit conditions, and permitted transitions

IDLE, CLASSIFYING, ROUTING, RESOLVING, ESCALATING, ERROR, COMPLETED

Must contain at least 2 states. Each state must have a unique name. Check for unreachable states by validating transition graph connectivity

[TRANSITION_RULES]

Explicit mapping of valid state transitions with guard conditions and triggers

IDLE->CLASSIFYING on input_received; CLASSIFYING->ROUTING when confidence >= 0.8; CLASSIFYING->ESCALATING when confidence < 0.5

Every transition must reference defined states. Validate that no transition targets an undefined state. Check for missing transitions that create dead-end states

[ENTRY_ACTIONS]

Actions executed when entering each state, such as tool calls, logging, or context updates

On enter CLASSIFYING: call classify_intent(input); On enter ERROR: log_error(state, reason); On enter COMPLETED: emit_completion_event()

Each entry action must reference a valid tool or internal function. Null allowed for states with no entry behavior. Validate tool availability before execution

[EXIT_ACTIONS]

Actions executed when leaving each state, such as cleanup, state persistence, or handoff preparation

On exit ROUTING: persist_routing_decision(); On exit ESCALATING: prepare_handoff_payload()

Each exit action must be idempotent or tolerate re-execution. Null allowed. Validate that exit actions do not block transitions on failure unless explicitly designed as a gate

[GUARD_CONDITIONS]

Boolean predicates that must evaluate to true before a transition is permitted

transition IDLE->CLASSIFYING guard: input is not null and input is not empty; transition CLASSIFYING->ROUTING guard: classification_confidence >= 0.8

Each guard must reference defined variables or tool outputs. Guards must be evaluable at runtime. Check for guards that can never be satisfied due to contradictory conditions

[INVALID_TRANSITION_HANDLER]

Behavior when an undefined or blocked transition is attempted, including error state routing and logging

On invalid transition: transition to ERROR state, log invalid_transition event with from_state, to_state, and timestamp, then retry from last valid state or escalate

Must define a concrete error path. Must not silently ignore invalid transitions. Validate that handler does not create infinite loops by re-attempting the same invalid transition

[TERMINAL_STATES]

States that represent completion of the agent's execution, with success or failure classification

COMPLETED (success terminal), ERROR_UNRECOVERABLE (failure terminal), ESCALATED (handoff terminal)

Must include at least one success and one failure terminal state. Validate that all non-terminal states have a path to at least one terminal state. Check for livelock where agent cycles without reaching a terminal state

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the state machine prompt into a reliable agent execution harness with validation, retries, and observability.

The state machine prompt template is not a standalone artifact—it is a configuration generator that must be consumed by an execution harness. The harness reads the generated state machine definition (states, transitions, guards, entry/exit actions) and enforces it at runtime. This means the prompt output must be machine-parseable JSON conforming to a strict schema, and the harness must validate that schema before using the definition to drive agent behavior. Treat the prompt as a compile step: it produces the state machine spec, and the harness is the runtime that executes it.

Wire the prompt into your workflow with these concrete checks. Schema validation: Parse the model output against a JSON Schema that requires states (array of objects with name, type, entry_actions, exit_actions), transitions (array of objects with from, to, guard, priority), and initial_state. Reject any output missing required fields or containing transitions that reference undefined states. Unreachable state detection: After parsing, run a graph traversal from initial_state and flag any state with zero inbound transitions as a warning—these may indicate dead code in the agent lifecycle. Missing transition coverage: For each state, verify that every possible exit condition has a defined transition. If a state has an error exit in its documentation but no corresponding transition, surface this as a coverage gap before deployment. Retry logic: If the model produces invalid JSON or fails schema validation, retry up to 2 times with the validation errors injected into the prompt as feedback. After 3 total attempts, escalate to a human reviewer with the raw output and validation report. Logging: Record the prompt version, model, raw output, validation result, and any warnings in an audit log. This is critical for debugging state machine drift across prompt iterations.

Model choice matters here. Use a model with strong JSON mode and structured output support (GPT-4o, Claude 3.5 Sonnet, or Gemini with response_schema). Avoid models that struggle with complex nested schemas—a malformed state machine definition can cause the agent harness to enter undefined states at runtime. For high-risk workflows where incorrect state transitions could trigger irreversible side effects (e.g., sending customer communications, modifying production data), add a human approval gate after validation but before the harness loads the state machine. The reviewer checks for semantic correctness that automated validators miss: guard conditions that are too permissive, missing timeout transitions, or entry actions that violate business rules. Finally, version-lock the generated state machine definition alongside the prompt template. When you update the prompt, run a regression suite of known agent workflows through the new state machine in a dry-run harness before promoting to production.

IMPLEMENTATION TABLE

Expected Output Contract

The state machine definition must conform to this schema. Use these fields to validate the model's output before wiring it into an execution engine.

Field or ElementType or FormatRequiredValidation Rule

states

Array of objects

Must contain at least 2 states. Each state object must have a unique 'id' field.

states[].id

string (snake_case)

Must match pattern ^[a-z]+(_[a-z]+)*$. No whitespace or special characters.

states[].label

string

Human-readable label. Must not be empty or identical to another state's label.

states[].type

enum: 'initial' | 'intermediate' | 'terminal'

Exactly one state must be 'initial'. At least one state must be 'terminal'.

transitions

Array of objects

Must contain at least 1 transition. Each transition must reference valid state IDs in 'from' and 'to'.

transitions[].from

string

Must match an existing state ID in the 'states' array.

transitions[].to

string

Must match an existing state ID in the 'states' array. Must not create a self-loop unless explicitly allowed by [ALLOW_SELF_LOOPS].

transitions[].guard

string or null

If present, must be a non-empty string describing the condition. If null, the transition is unconditional.

PRACTICAL GUARDRAILS

Common Failure Modes

State machine prompts fail in predictable ways. These are the most common production failure modes and the guardrails that catch them before they corrupt agent execution.

01

Invalid Transition Execution

What to watch: The model invents a transition that doesn't exist in the defined state machine, jumping from IDLE directly to COMPLETED without passing through required intermediate states. This happens when the prompt lists states but doesn't enforce transition rules strictly enough. Guardrail: Include an explicit valid_transitions map in the output schema and validate every state change against it before accepting the transition. Reject and retry any transition not present in the map.

02

Unreachable State Drift

What to watch: The state machine definition contains states that no valid transition path can reach, or the model converges on a subset of states while ignoring others entirely. This creates dead code paths and confuses downstream agents expecting those states. Guardrail: Run a static reachability check on every generated state machine before deployment—verify that every non-start state has at least one inbound transition and every non-terminal state has at least one outbound transition.

03

Missing Transition Coverage

What to watch: The model defines happy-path transitions but omits error, timeout, cancellation, and recovery transitions. When an agent hits an unhandled condition, it either stalls indefinitely or invents a transition. Guardrail: Require the prompt to produce transitions for at least these categories: success, failure, timeout, cancellation, and retry. Validate coverage by checking that every state has an outbound transition for each category or an explicit declaration that the category is not applicable.

04

Entry/Exit Action Hallucination

What to watch: The model generates plausible-sounding entry or exit actions that reference tools, APIs, or side effects that don't exist in the agent's actual capability set. The state machine looks correct but fails at runtime when actions can't execute. Guardrail: Constrain entry/exit actions to a pre-declared tool manifest. Validate every action name against the manifest before accepting the state machine. Reject any action not in the allowed set and request regeneration with the constraint.

05

Terminal State Ambiguity

What to watch: Multiple states are marked as terminal without clear differentiation, or a state is terminal in some paths but not others. Downstream agents can't determine whether execution actually finished or is stuck in a non-terminal state that looks terminal. Guardrail: Require exactly one terminal state classification per state (is_terminal: true/false) and enforce that terminal states have zero outbound transitions. Validate this constraint programmatically before accepting the state machine.

06

Guard Condition Collapse

What to watch: Transition guard conditions are written in ambiguous natural language that the model interprets inconsistently across calls. A guard like "if the result is good enough" produces different behavior on every execution. Guardrail: Require guard conditions to reference specific, measurable fields from the agent's output schema (e.g., confidence_score > 0.8, validation_errors.length == 0). Validate that every guard expression references only fields present in the defined output schema.

IMPLEMENTATION TABLE

Evaluation Rubric

Criteria for testing the quality and correctness of a generated agent execution state machine definition before integrating it into a production workflow engine. Use these checks to validate state coverage, transition safety, and error handling.

CriterionPass StandardFailure SignalTest Method

State Completeness

All required lifecycle states (e.g., IDLE, RUNNING, WAITING, COMPLETED, FAILED, CANCELLED) are present in the definition.

Missing a core state like FAILED or CANCELLED; agent has no terminal error state.

Schema check: verify the set of defined states against a required state enum list.

Transition Coverage

Every non-terminal state has at least one valid outgoing transition defined. Every transition has a guard condition.

An orphan state exists with no way to exit; a transition is defined without a guard condition.

Graph analysis: compute out-degree for each state. Assert out-degree > 0 for all non-terminal states.

Unreachable State Detection

No states are unreachable from the initial state [INITIAL_STATE] via any sequence of valid transitions.

A defined state has an in-degree of 0 and is not the initial state.

Graph traversal: run BFS/DFS from [INITIAL_STATE]. Assert all defined states are visited.

Invalid Transition Handling

An explicit handler or default policy is defined for any transition not listed in the valid transition table.

The state machine definition is silent on what happens when an invalid transition is requested.

Parse check: confirm the output contains an 'on_invalid_transition' field or equivalent default policy.

Entry/Exit Action Completeness

Every state that requires a side effect (e.g., logging, tool call, notification) has a defined entry_action or exit_action.

A state like RUNNING has no entry_action to initiate the primary agent task.

Schema check: iterate all states and assert that states with known side effects have non-null action fields.

Terminal State Finality

Terminal states (COMPLETED, FAILED, CANCELLED) have zero outgoing transitions.

A terminal state contains an outgoing transition, allowing the agent to restart without explicit re-initialization.

Graph analysis: assert out-degree == 0 for all states listed in a [TERMINAL_STATES] set.

Guard Condition Specificity

All transition guards reference concrete, evaluable conditions (e.g., task_status == 'done', retry_count < [MAX_RETRIES]).

A guard condition is a vague string like 'if it works' or 'on error' without a machine-readable predicate.

Regex/parse check: verify guard strings match a supported predicate pattern or reference defined state variables.

Deterministic Conflict Resolution

When multiple transitions from a single state have overlapping guard conditions, a priority or tie-break rule is defined.

Two guards can evaluate to true simultaneously with no resolution strategy, causing non-deterministic behavior.

Static analysis: for each state, pairwise compare guard conditions for potential overlap. Assert a priority field exists if overlap is detected.

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Start with the base state machine prompt but relax strict validation. Define only the core states ([IDLE], [EXECUTING], [COMPLETED], [FAILED]) and essential transitions. Skip entry/exit actions and focus on getting the state flow correct. Use inline comments to mark where guards will be added later.

Prompt modification

code
Define a state machine for agent execution with these states:
- [IDLE]
- [EXECUTING]
- [COMPLETED]
- [FAILED]

Transitions:
- IDLE -> EXECUTING: when task is assigned
- EXECUTING -> COMPLETED: when task finishes successfully
- EXECUTING -> FAILED: when task encounters an error
- FAILED -> IDLE: on reset

Output a JSON object with states and transitions arrays.

Watch for

  • Missing error states like [TIMED_OUT] or [CANCELLED]
  • No guard conditions on transitions
  • States without exit paths (dead ends)
  • Overly broad instructions that produce inconsistent JSON shapes
Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.