TL;DR
Instead of using Large Language Models (LLMs) to play games directly—which often leads to illegal moves and shallow strategy—this paper proposes using them to synthesize executable Python "Code World Models" (CWMs). By combining these LLM-generated rule engines with classical search algorithms like Monte Carlo Tree Search (MCTS), the authors achieve superior strategic depth and verifiability across both perfect and imperfect information games.
The Problem: Fragile Direct Policies
While LLMs show impressive reasoning capabilities, their performance in complex games is often fragile. When used as a direct policy (asking the model to simply pick the next move based on a prompt), they rely heavily on implicit pattern matching. This leads to two major issues:
- Illegality: Models frequently suggest moves that violate game rules because they don't truly "understand" the underlying mechanics.
- Strategic Shallowness: They lack the multi-step lookahead (Kahneman’s "System 2" thinking) required for high-level play, especially in novel games not found in their training data.
The challenge is even steeper in Imperfect Information Games (like Poker), where an agent must estimate hidden states (e.g., the opponent's hand) while planning. Previous attempts at building "world models" usually assumed the environment was fully observable or deterministic, leaving a significant gap in general game-playing capabilities.
Methods: From Player to Programmer
The authors introduce a framework that shifts the LLM's role from a direct player to a programmer.
- Code World Model (CWM) Synthesis: The LLM receives natural language rules and a few sample trajectories (game logs). It then generates Python code following the OpenSpiel API, including functions for state transitions, legal move enumeration, and termination checks.
- Iterative Refinement: The system automatically generates unit tests from the sample trajectories. If the synthesized code fails, the LLM receives the stack trace and error message to iteratively debug the CWM until it achieves perfect transition accuracy.
- Inference as Code: For games with hidden information, the LLM synthesizes "inference functions." These functions act as samplers that estimate plausible hidden histories based on the observations seen so far.
- Heuristic Value Functions: To make search more efficient, the LLM writes code to evaluate the "goodness" of a non-terminal board state, replacing the computationally expensive random rollouts typically used in MCTS.
In a novel contribution, the authors also show that LLMs can learn game rules even when hidden states are never observed in the training data (the "closed deck" scenario). They achieve this by treating the CWM and inference functions as a regularized autoencoder, where the inference function maps observations to latent histories, and the CWM reconstructs the observations.
Datasets and Evaluation
The researchers evaluated their agent on 10 diverse games:
- Perfect Information: Tic-tac-toe, Connect Four, and Backgammon.
- Imperfect Information: Leduc Poker, Bargaining, and Gin Rummy.
- Out-of-Distribution (OOD) / Novel Games: To prevent the LLM from simply "remembering" games from its training set, the authors created four novel games: Generalized Tic-tac-toe (6x6), Generalized Chess (5x5), Quadranto, and Hand of War.
Key Insights
- Meta-Tasking is Easier: It is often easier for an LLM to translate a rulebook into code than it is to play the game perfectly. By focusing on the "meta-task" of code generation, the model's knowledge is grounded in a verifiable, executable format.
- Verifiability via Symbolic Execution: Because the CWM is code, the agent can algorithmically guarantee that it only considers legal moves. The "hallucination" problem is moved from the move-generation phase to the code-generation phase, where it can be caught by unit tests.
- Search > Intuition: Combining LLM semantic understanding with the raw "crunching" power of MCTS allows for strategic depth that an LLM acting alone cannot match.
Key Results
The CWM + MCTS approach proved highly effective, outperforming or matching Gemini 2.5 Pro (acting as a direct policy) in 9 out of 10 games.
- Strategic Depth: In perfect information games, the CWM-based agent was often indistinguishable from an agent using the "ground truth" game code.
- Generalization: The method was highly effective on OOD games (like Generalized Chess and Generalized Tic-tac-toe), proving that the LLM was actually "understanding" the rules provided in the prompt rather than just recalling memorized strategies.
- Efficiency: LLM-generated heuristic value functions significantly improved win rates in complex games like Bargaining by providing better "intuition" at the leaves of the search tree.
Limitations and Future Work
While powerful, the approach has limitations. The model struggled with Gin Rummy, which features intricate, multi-step scoring subroutines (knocking, laying off melds, etc.). This suggests a current limit to the procedural complexity LLMs can synthesize into a single CWM.
Additionally, the current system learns the model "up-front." The authors suggest moving toward active/online learning, where the agent updates its CWM as it encounters new, unexpected states during actual play. Finally, expanding this text-based framework to handle visual inputs or open-world environments with free-form text interactions remains a logical next step for future research.
Summary updated