Codified Finite-state Machines for Role-playing
Preprint
Abstract
Modeling latent character states is crucial for consistent and engaging role-playing (RP) with large language models (LLMs). Yet, existing prompting-based approaches mainly capture surface actions, often failing to track the latent states that drive interaction. We revisit finite-state machines (FSMs), long used in game design to model state transitions. While effective in small, well-specified state spaces, traditional hand-crafted, rule-based FSMs struggle to adapt to the open-ended semantic space of RP. To address this, we introduce Codified Finite-State Machines (CFSMs), a framework that automatically codifies textual character profiles into FSMs using LLM-based coding. CFSMs extract key states and transitions directly from the profile, producing interpretable structures that enforce character consistency. To further capture uncertainty and variability, we extend CFSMs into Codified Probabilistic Finite-State Machines (CPFSMs), where transitions are modeled as probability distributions over states. Through both synthetic evaluations and real-world RP scenarios in established artifacts, we demonstrate that CFSM and CPFSM outperform generally applied baselines, verifying effectiveness not only in structured tasks but also in open-ended stochastic state exploration.Keywords
All Related Versions
This publication has 13 references indexed in Scilit:
- A Survey on Large Language Models for Code GenerationACM Transactions on Software Engineering and Methodology, 2026
- Identity-Driven Hierarchical Role-Playing Agents2025
- Large language models for generative information extraction: a surveyFrontiers of Computer Science, 2024
- InCharacter: Evaluating Personality Fidelity in Role-Playing Agents through Psychological Interviews2024
- TidyBot: personalized robot assistance with large language modelsAutonomous Robots, 2023
- Code as Policies: Language Model Programs for Embodied Control2023
- Recurrent Neural Language Models as Probabilistic Finite-state Automata2023
- Character-LLM: A Trainable Agent for Role-Playing2023
- Teaching finite state machines with case method and role play2013
- Finite state languagesInformation and Control, 1958