← writing

Variant: Chess Tree w/AI Coach

Background

I was playing chess the other day and lost a game where I didn’t think I’d made a single real blunder. For context, I am around 1200 on chess.com, and most of what I know comes from watching GothamChess. When I went through the chess.com analysis (which basically just uses Stockfish) I was very curious why certain positional moves were getting weighed much better than the moves I was making. The only way I saw to do this was to go down many diverging paths from the game I played following Stockfish’s recommendation.

Idea

This brought me to two wishes:

  1. I wish I could actually view all the different paths I tried out when reviewing a game (like a tree)
  2. I also wish I could ask a coach why certain moves are better and have explanations of the engine

This led me to building Variant!

What Variant Does

Variant allows you to import your games from chess.com, lichess, or paste your PGN. After doing so, it will run every position through Stockfish and label each of your moves as best, good, inaccuracy, mistake, or blunder. A normal length game takes about a minute, and you can watch the progress bar fill up as each position comes back.

Once it’s finishes, the game will show up as a tree!! The main line is the game you actually played, and at every mistake or blunder it branches off into the move Stockfish wanted instead.

Variant's tree view of a game, branching at each mistake into Stockfish's preferred move

From there you can explore. Clicking a node puts that position on the board, and you can try your own moves. Every move you try becomes its own branch, and Variant judges it the same way it judges the real game. So I never lose track of the lines I already tried, which was wish #1.

For wish #2, every position has its own chat with a coach. Any moves in the coach’s answer are clickable, so you can play them out on the board, and if a line is worth keeping you can add it to the tree.

Asking Variant's coach "Why is gxf3 a mistake?" and its answer with clickable lines

Technical Challenges

Making the tree fast and consistent

To build the tree, every position in the game needs a Stockfish evaluation. A 40 move game is 80 positions, and every branch adds more.

My first attempt ran Stockfish compiled to WebAssembly inside Convex, the backend/database I was using. It worked, but it was extremely slow: ~6-17 seconds per position. A full long game would take 10+ minutes, and the deep positions with many pieces on the board would hit the 90 second timeout and fail.

So I moved Stockfish into its own small service: native Stockfish 19 behind a tiny Node server, running in Docker on Fly.io. Now each position takes ~1-2.3 seconds (I have since switched to shared-cpu so metric might be off), and it runs several at once. The machine also shuts off when nobody is using the analyzing, so I only would have to pay for the time it actually spends analyzing.

I originally kept the WASM engine as a backup in case the Fly service went down, but I ended up deleting it. The WASM build is Stockfish lite, which evaluates positions differently than full Stockfish. Since every evaluation is saved to the same cache, a game analyzed partly by each engine could have moves labeled as mistakes/better alternatives just because the two engines disagreed. I decided it was better for the analysis to just fail and the user could retry another time.

I kept this same thought process for how I set up the Stockfish settings. Each Stockfish process runs on a single thread and gets reset before every search, because multithreaded Stockfish gives different answers each time you run it. Now the same position ALWAYS gets the same answer, which means results can be cached by position and shared across every game and every user. Common openings only ever get analyzed once.

Keeping the coach honest

The idea behind the “coach” was that it could explain to you why certain moves weren’t great, including positional reasons. LLMs are great at sounding like a chess coach, but of course are not built to play and give the best move.

I originally wanted to build this project with NVIDIA NIM’s free models, but the outputs weren’t useful. They skipped the function-calling tools I gave them and overall hallucinated a lot of the information on the board.

I tried two things that have been giving pretty good results. The first was a stronger model. With similiar prompt and tools, Claude Opus 5.5 checked lines and explained the real idea in every test position I tried.

The second was never letting the LLM decide what’s true. Stockfish and regular code decide the facts, and the LLM only explains them:

You can see this in the screenshot above. When I asked why 11. gxf3 was a mistake, the chips above the answer are the tool calls it made before writing anything. The stuff about the pawns comes from the fact sheet, and it even checked that 11…Qa5, which attacks both bishops, isn’t actually the problem: it’s only -0.68.

The downside to this is that an answer took much longer for the tools to resolve and cost a few cents.

Product Decisions

Who pays for the AI?

I wanted this project to be free, but I also didn’t want to pay for everyone’s LLM calls, so you bring your own API key (Anthropic or OpenRouter). I thought about keeping the key in the browser and calling the model from there, but the coach’s tool calls all run on the server (play_moves + evaluate), so I would have had to rewrite the chat, expose the key to the page, and the key would only work on one device. So I decided to store the key encrypted on the server and only use it for your own questions.

The downside is most people won’t have an API key lying around, so this is the biggest thing stopping someone from just trying the coach. Sidenote, if anyone is interested in the app and doesn’t have one, reach out to me! I have an API key loaded with some tokens ready for use. Only requirement is you have to give me your honest feedback.

What does the coach remember?

Every position has its own chat with the coach, but I noticed my questions usually followed one idea across several moves/lines. So when you ask a question, the coach also sees the chats from earlier moves on the same path. If you’re in a what-if branch, it sees the game’s chats up to where you split off, but never chats from other branches, since those positions never happened on your line.

I keep only the last 8 earlier exchanges and cut each answer at 1,200 characters, so the prompt doesn’t get too big. There’s also a rule telling the coach to re-check anything from an earlier chat against the current position, so a claim that was true three moves ago doesn’t get repeated after it stops being true.

Sharing a game

Every game has a Share button that makes a read-only link. Anyone with it can see the tree, the analysis, and the chats without signing in, but they can’t change anything. I included the chats on purpose, because the questions you asked are a big part of how you reviewed the game, and that’s what makes it useful to send to a friend or a coach.

Less UI

My first layout put the board, the tree, and the engine lines side by side, and it was way too busy. The move list also showed the same thing as the tree. Now it’s the board plus one panel with tabs (Position, Tree, Moves), and the tree can go full screen.

I also tried four different ways of drawing the tree before landing on the metro map style. It stays pretty readable on long games, and the turning points get cards with a mini board so you can see them without clicking.

Example game with 66 moves (131 counting both sides)

Importing games

Connecting Chess.com or Lichess only asks for your username, since Variant just reads your public games. No signing in to either site, which keeps importing a game down to a couple of clicks.

Sources

I took inspiration from a bunch of different sources for this project. The biggest one was Take Take Take’s AI Engineer talk. They had a similar approach of using an LLM to explain things to the user in natural language, but rather than only using Stockfish (like I did), they also used Maia and detectors.

Maia is a chess engine trained on human games, so instead of finding the best move it predicts what a player at your rating would actually play. Detectors are regular code that finds things like pins, forks and hanging pieces. Both fill gaps I ran into: my coach can still call something a pin when none of my tools actually detect pins, and as a 1200 player, “what would someone at my level play here” is often more useful than Stockfish’s best move. Detectors are probably the next thing I add.

Other things I looked at: