NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

StateFlow: Enhancing LLM Task-Solving through State-Driven Workflows

Yiran Wu, Tianwei Yue, Shaokun Zhang, Chi Wang, Qingyun Wu

TL;DR

Current LLM agent frameworks (like ReAct) rely on a single, monolithic prompt and trust the model to autonomously track progress and decide next steps across complex, multi-turn interactions. StateFlow reimagines task solving as a Finite State Machine (FSM), decoupling macro-level "process grounding" (states and deterministic/heuristic transitions) from micro-level "sub-task solving" (specialized prompts and tool execution within states). This structured approach boosts task success rates by up to 28% while slashing token inference costs by 3× to 5×.

What Problem Does This Paper Address?

When using LLMs to solve multi-step problems requiring environmental interaction (e.g., interactive coding, shell commands, or text-based games), standard practice involves prompting an agent in a single loop with instructions, few-shot examples, and cumulative execution history.

This approach suffers from two major pain points:

  1. Unreliable state tracking: Relying purely on the LLM’s internal reasoning to evaluate progress often leads to hallucinated assumptions, repetitive loops, and brittle failure recovery.
  2. High token overhead: Passing lengthy prompt templates and multi-step trajectories at every iteration rapidly blows up context length and API costs.

The authors ask: How can we exert precise, structured control over an LLM's task-solving trajectory without sacrificing flexibility?

Methods

StateFlow formalizes agent workflows as a transducer finite-state machine sextuple: $\langle S, s_0, F, \delta, \Gamma, \Omega \rangle$.

[Init] ---> [Observe] ---> [Solve] ---> [Verify] ---> [End]
               ^              |           |
               |              v           |
               +---------- [Error] <------+
  • States ($S$): Represent distinct stages of the problem-solving lifecycle (e.g., Observe, Solve, Verify, Error).
  • Output Functions ($\Omega$): Triggered upon entering a state. Instead of one giant prompt, StateFlow activates compact, state-specific instructions ($T_i$) and tool executions (e.g., running SQL/Bash commands or environment queries).
  • Transitions ($\delta$): State changes are governed either by static string matching (e.g., checking for syntax errors or Command Not Found), environment status flags, or targeted, lightweight LLM checks.
  • Composability: Because StateFlow acts as an execution engine, it is orthogonal to reflection-based frameworks and can easily integrate with methods like Reflexion to refine policies across multiple trials.

Datasets

The authors evaluated StateFlow across three interactive environments:

  1. InterCode-SQL: 1,034 MySQL task instances derived from the Spider dataset, hosted in Docker containers.
  2. InterCode-Bash: 200 OS and bash tasks curated from the NL2Bash dataset.
  3. ALFWorld: 134 text-based embodied household decision-making tasks across 6 categories (e.g., pick-and-place, cleaning, heating, cooling).

Key Insights and Ideas

  • Specialized Prompts Dramatically Cut Token Costs: Monolithic ReAct prompts often consume thousands of tokens per step (e.g., 2,043 tokens per call in SQL). StateFlow breaks this down into modular, state-specific prompts of under 400 tokens, leading to an immediate 3×–5× cost reduction.
  • Explicit Failure States Prevent Loops: Standard agents frequently spiral when encountering an error. StateFlow routes exceptions into dedicated Error states designed solely to inspect messages and gather diagnostics (e.g., inspecting database schema via DESCRIBE).
  • Hard Tasks Benefit Most from Explicit Flow Control: In InterCode-SQL, successful runs visited the Error state in 20% of extra-hard tasks compared to only 9% in easy tasks. StateFlow's success rate on extra-hard queries beat ReAct by over 20%.
  • State Granularity Matters: Further decomposing actions in ALFWorld (moving from a 7-state to a 10-state machine by separating navigation from item interaction) increased overall accuracy from 83.3% to 88.8% while reducing costs.

Key Results

  • InterCode-SQL: Using GPT-3.5-Turbo, StateFlow achieved 63.73% Success Rate (SR) compared to ReAct’s 50.68%, while reducing inference cost from $17.73 to $3.82 (a 4.6× reduction). With GPT-4-Turbo, StateFlow reached 69.34% (vs. 60.16% for ReAct) at a fraction of the cost ($36.00 vs. $147.00).
  • InterCode-Bash: StateFlow outperformed baselines with an SR of 36.0% (GPT-3.5) and 39.0% (GPT-4), needing significantly fewer environment turns (2.95 turns vs. 3.86+).
  • ALFWorld: StateFlow scored 83.3% on GPT-3.5-Turbo (surpassing ReAct at 55.5% and 3-agent ALFChat at 67.7%) at less than half the cost ($2.60 vs. $6.60). Open-source Llama-3-70B paired with StateFlow achieved a 94.0% success rate.
  • Integration with Reflexion: Combining StateFlow with Reflexion over 6 iterations boosted ALFWorld task completion from 84.3% to 94.8% at a total cumulative cost of $8.60 (compared to ReAct + Reflexion reaching only 74.6% at $27.90).

Limitations and Future Work

  • Manual Engineering Requirement: Current StateFlow workflows and state transition logic must be manually specified by human developers based on domain expertise.
  • Automated Workflow Synthesis: The authors highlight automated state machine construction—prompting LLMs or using active learning strategies to synthesize, prune, and tune states dynamically based on execution rewards—as the most critical next step.

Summary updated

BibTeX
@inproceedings{wu2024stateflow,
  author = {Yiran Wu and Tianwei Yue and Shaokun Zhang and Chi Wang and Qingyun Wu},
  title = {{StateFlow: Enhancing LLM Task-Solving through State-Driven Workflows}},
  booktitle = {COLM},
  year = {2024},
  url = {https://openreview.net/forum?id=3nTbuygoop},
}

Loading related content...

Figure