How does Stockfish work? (the short version)
Stockfish searches the lines that follow each move, scores the positions at the end of them with a neural network (NNUE), and plays the move that leads to the best score. The number you see, +3.2 for example, is that evaluation in pawns from White’s point of view.
What is Stockfish?
Stockfish is the strongest open-source chess engine. Professionals and amateurs use it, and game analysis sites run on it, chess.rodeo among them. The rest of this article covers how it searches for a move and where the evaluation comes from.
How a chess engine picks a move
A chess engine picks a move by search, in four steps:
- It lists every move that is legal in the position.
- It goes through the lines that follow each of those moves.
- It gives a numerical evaluation to the positions at the end of the lines.
- It picks the move that leads to the best evaluation.
Search
Alpha-beta pruning
The search is built on minimax: the engine assumes that both sides choose what is best for themselves on every move. With alpha-beta pruning it stops calculating a line as soon as that line can no longer affect the choice of move. In the same time Stockfish gets much deeper than it would by going through every line in full.
Depth and speed
At depth 20 Stockfish goes through on the order of a million positions, and the exact number depends on the position. On a fast multi-core processor it examines tens of millions of positions per second.
Selective search
Stockfish searches different moves to different depths. It looks furthest at:
- captures and checks;
- moves after which the evaluation changed sharply;
- lines that turned out strong on earlier passes of the search.
Evaluation
Classical evaluation
In older versions of Stockfish the evaluation function was written by hand. It was made up of several parts:
- Material. Each piece has its own value, and the value in the middlegame differs from the value in the endgame. The often-quoted table “pawn 100, knight 320, bishop 330, rook 500, queen 900” is not Stockfish’s: it comes from Tomasz Michniewski’s Simplified Evaluation Function, which was written for teaching.
- Positional features. Piece activity and mobility, pawn structure (doubled, isolated and passed pawns), king safety, control of key squares and piece coordination.
- Game phase. Different features matter in the opening and in the endgame. In the middlegame a king in the center comes under attack, and in the endgame the center is where you bring it.
NNUE: neural network evaluation
Since version 12 Stockfish has evaluated positions with NNUE (Efficiently Updatable Neural Network), and current versions have no hand-written evaluation at all.
NNUE is trained on billions of positions and built so that the evaluation is fast to compute. The position on the board is converted into input features, the features pass through the layers of the network, and an evaluation comes out. After a move the network recalculates only the part the move touched, which is where “efficiently updatable” in the name comes from.
The network evaluates more accurately than the hand-written function did. It finds patterns too complex to write down as rules, and it takes into account positional subtleties that players knew about but could not put a number on.
The network is trained on positions from engine games, Leela Chess Zero’s among them. Each position comes with the evaluation a search gave it and the result of the game it occurred in.
Reading Stockfish’s output
Evaluation in centipawns
Stockfish computes the evaluation in centipawns, hundredths of a pawn. On screen it is usually shown in pawns:
- +1.00: White has an advantage of one pawn;
- -2.50: Black has an advantage of two and a half pawns;
- 0.00: roughly equal.
Mate scores
When a forced mate is found, the number of moves to mate is shown in place of the evaluation:
- M5: White mates in 5 moves;
- -M3: Black mates in 3 moves.
Principal variation (PV)
The principal variation, or PV, is made up of the moves Stockfish considers best for both sides:
+0.25 Nf3 d5 e3 Nf6 d4 e6 Bd3 c5Search depth
What depth means
Depth is measured in plies. A ply is a half-move: one move by one side. Depth 10 is 5 moves for each side, and depth 20 is 10. The number is nominal: the engine drops weak lines early and searches captures and checks further.
Deeper is more accurate
As depth grows, Stockfish finds longer tactics and evaluates the position more accurately, because the search reaches consequences of moves that it did not get to at a lower depth.
Past depth 20 the evaluation changes little
The engine finds the main things in the first 15–20 plies. Between depth 20 and depth 40 the evaluation rarely changes sharply, except in sharp tactical positions.
Where Stockfish goes wrong
The horizon effect
The engine does not see what lies beyond its search depth. A tactical shot can look winning at depth 20 and be refuted by a counter-shot that only shows up at depth 25.
Fortresses
Stockfish can evaluate a drawn fortress as a win: it does not see that the stronger side has no way to make progress.
Long plans
There are positions where everything depends on a plan of twenty-odd moves with no forcing line in it. The engine handles such plans worse than it handles tactics.
How chess.rodeo uses Stockfish
Where the analysis runs
For free, Stockfish 18 analyzes a game in your browser at depth 15. Positions already in our evaluation base come from it without a search. With Pro you can raise the depth to 22 or hand the search to the server.
Several lines
The engine panel shows several lines, five by default. You can change the number in settings.
Move labels
A move gets its label from how many percentage points (pp) of win probability it lost compared with the best move:
| Label | Win probability lost |
|---|---|
| Best | up to 1 pp |
| Excellent | 1–2 pp |
| Good | 2–5 pp |
| Inaccuracy | 5–10 pp |
| Mistake | 10–25 pp |
| Blunder | more than 25 pp |
Book, Brilliant, Great and Miss follow their own rules, which are described in the article on accuracy.
How to use engine analysis
- Work out what makes the best move better than yours. Look at the threat it creates and at what it stops your opponent from doing.
- Look past the first line. The engine’s second and third moves are often almost as good as the first, and you can play them too. It is worth seeing where they lose ground.
- Take the state of the game into account. In a lost position, a move the engine calls a mistake can be your best practical chance: the engine’s first line loses as well, only more slowly.
- Do not chase depth. Depth 15–20 is enough for going over your own games. Depth 30 and more is for opening novelties and for positions with long forcing lines.
Import a game and see how Stockfish evaluates your moves.