Skip to content

Chess

chess is a built-in world: any two agents can play a full game of chess, talk while they play, and be watched move by move. It needs no extra package. The rules are in SPEC-024, and ADR-0013 explains why AgentPlay has its own rules engine.

Play a game

Watch two random players first. This needs no model and no key:

agentplay: 1
name: chess-random
world: {use: chess}
agents:
  - {id: white, use: scripted/random}
  - {id: black, use: scripted/random}
limits: {max_steps: 200}
agentplaykit play chess-random.yaml --live

Then let two models play and talk with examples/chess/scenario.yaml. It sets up qwen2.5 and llama3.2 on Ollama, with a persona each:

agentplaykit play examples/chess/scenario.yaml --live

The replay and live pages draw the board, and the chat panel shows what the players say to each other.

Settings

All settings are optional:

Setting Default What it does
players [white, black] The two agent ids, white first. They must match the scenario's agents.
max_moves 60 Moves per side before the game stops; null for no limit
at_limit draw What a stopped game counts as: draw, or material (more material wins)
fen the normal start Start from any position, written in FEN
talk false Ask each player to send its opponent one short message every move

Moves

On its turn a player sees legal_moves, its legal moves in SAN (standard algebraic notation): e4, Nf3, exd5, O-O, e8=Q. It replies with one of them, for example {"action": "Nf3"}.

The world also accepts UCI (g1f3, e7e8q) and common slips, the same way for every agent: - any letter case in UCI; - a dash or space in UCI (E2-E4); - 0-0 for O-O; - a missing or extra +, # or = (e8Q).

The log records what was sent and how it was read.

A move that is not legal is rejected with the legal moves, so the player can try again:

"Ke2" is not a legal move; legal moves: "Nc3", "Na3", "Nh3", ... Reply like {"action": "Nc3"}

After limits.max_illegal rejected moves in a row (default 3), the player forfeits.

What each player sees

{
  "you_play": "white (capital letters)",
  "opponent": "llama",
  "board": ["8 r n b q k b n r", "7 p p p p p p p p", "...", "1 R N B Q K B N R", "  a b c d e f g h"],
  "fen": "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
  "your_turn": true,
  "in_check": false,
  "last_moves": [],
  "legal_moves": ["Nc3", "Na3", "Nh3", "Nf3", "a3", "a4", "...", "h4"],
  "material": {"you": 39, "opponent": 39},
  "your_moves_left": 60
}

Capital letters are white pieces and small letters black: K king, Q queen, R rook, B bishop, N knight, P pawn, and . an empty square. squares holds the same board as 64 squares, for the viewer, drawn with chess symbols (♔ white, ♚ black).

With talk: true, each player also gets a talk note. The note gives the exact way to address the opponent, "say": [{"to": "llama", "text": "..."}]. Messages go to agent ids: a model that writes to "llama3.2" instead of "llama" has its message dropped as an unknown recipient. In a persona, call the rival by its id.

How a game ends

Ending Result
checkmate the player who gave mate wins
stalemate draw
the same position three times draw
50 moves by each side with no capture or pawn move draw
not enough material to checkmate draw
max_moves reached draw, or with at_limit: material whoever has more material wins
limits.max_illegal rejected moves in a row forfeit: the other player wins

A win scores 1, a draw 0.5 for each player and a loss 0. The outcome names the ending, for example "checkmate: llama wins". Score a player with the outcome evaluator:

evaluators:
  - {use: outcome, config: {agent: qwen}}

Judge the moves

Add the strategy evaluator to see how well each player played, move by move:

evaluators:
  - {use: outcome, config: {agent: qwen}}
  - use: strategy
  strategy: llama accuracy 0.41, 6 blunders; qwen accuracy 0.38, 7 blunders (approximate, depth 2)

Chess's analyst is approximate: it looks two half-moves ahead (the move and the reply), then follows captures. So it finds a piece left to be taken, a capture that loses material, and mate in one. It does not see deep plans. A blunder is a move that loses 3 pawns or more (a knight, a bishop or worse). The replay lists the blunders and outlines the best move on the board. It takes under a tenth of a second per move. The strategy guide explains every number.

Play against a scripted opponent

scripted/chess is a computer player with three levels. It needs no model, costs nothing and plays a different game with each seed, so a model can be measured against a known strength:

agents:
  - {id: white, use: ollama, config: {model: "qwen2.5:7b", constrain: true}}
  - {id: black, use: scripted/chess, config: {level: 2}}
Level Looks ahead Sees Misses Time per move
1 its move, then captures a piece it can take, a capture that loses material, mate in one any threat in the reply about 5 ms
2 its move and the reply, then captures a piece left to be taken, a reply that wins material mate in two, two-move plans about 50 ms
3 three half-moves, then captures mate in two, a threat that needs a reply deep plans and endgames about 0.4 s

Level 3 is about ten times slower than level 2, so a 40-move game takes it about half a minute. Each level is clearly stronger than the one below. Over 16 games of 40 moves each, with a stopped game going to whoever has more material, the stronger side scored (a win counts 1, a draw 0.5):

Game Score of the stronger side
level 1 against scripted/random 1.00
level 2 against level 1 0.84
level 3 against level 2 0.81

Settings: - level: 1, 2 or 3 (default 1). - margin: how many pawns worse than the best move a move may be and still be played (0 to 2, default 0.3). It picks among those moves at random, from the run's seed, so it does not play the same game every time. With margin: 0 it plays only the best moves.

Its reasoning in the log reads, for example, value 1.0, best 1.0 (level 2). It sends no messages, and reports list it by level: scripted/chess:level-2.

To play it yourself, in the terminal or by clicking on the board in the browser:

agentplaykit play examples/chess/vs-level-1.yaml --human white --live

See Play it yourself.

The ladder

examples/chess/ladder.yaml is an experiment that plays a model against all three levels, as each colour, with four seeds. The levels also play each other, so the ratings place the model among them. Set your model as my-model, then:

agentplaykit experiment plan examples/chess/ladder.yaml
agentplaykit experiment run examples/chess/ladder.yaml --workers 4
agentplaykit report runs/chess-ladder

Read the report's head-to-head table: each row is a pair, with wins, draws, losses and the score of the first (the higher-rated; when the model is second, its score is 1 minus that). A model that scores well above 0.5 against level 1 has learned the rules and takes free pieces; one that holds level 2 also guards its own. Above level 3 is a strong result for a small model. The ratings put all four players on one scale.

Tips for small models

Chess is hard for small models, so most of their mistakes are moves that are not legal. These settings help: - constrain: true, so the server only lets the model reply with a legal move (see small models); - wrap: [format_hints], so a rejected move is followed by an example of a valid reply built from the legal moves; - limits.max_illegal: 5, to give more tries before a forfeit; - a lower temperature, such as 0.3, for steadier moves (higher is livelier talk).

agentplaykit model check plays a few short games and tells you which of these works best for your model. To compare many models over many games, use an experiment or a benchmark suite.