AskHandle

AskHandle Blog

Build a Simple Chess AI with an LLM

July 9, 2026Lillian Kim3 min read
  • Chess
  • Python
  • AI

Build a Simple Chess AI with an LLM

A large language model can help you write a simple chess AI faster, especially if you treat it as a coding partner rather than a magic chess engine. You can ask it to generate code, explain board logic, create evaluation rules, test legal moves, and improve weak parts step by step. The best result comes from combining your own goals with clear prompts, small tasks, and steady testing.

What Kind of Chess AI Are We Building?

A simple chess AI does not need to play like a grandmaster. The goal is to build a program that can:

  • Read a chess position
  • Generate legal moves
  • Score each position
  • Pick a move based on that score
  • Play against a human or another bot

This kind of AI usually uses a classic search method rather than machine learning. The most common approach is called minimax, often improved with alpha-beta pruning. The AI looks ahead a few moves, estimates which positions are good or bad, and chooses the move that leads to the best result.

An LLM can help write this code in Python, JavaScript, Java, C#, or another language. Python is a good choice for a first version because it is easy to read and has helpful chess libraries.

Choose Between Writing Everything or Using a Library

Before asking an LLM to generate code, decide whether you want to build all chess rules yourself or use an existing chess library.

Writing every rule from scratch can be educational, but it takes time. You need to handle pawn movement, castling, en passant, check, checkmate, stalemate, promotion, pins, legal move filtering, and board state changes.

Using a library is much easier. In Python, a common choice is python-chess. It can manage legal moves, board positions, checkmate detection, and move notation. Then your AI only needs to focus on picking a good move.

A useful prompt might be:

Write a simple Python chess AI using the python-chess library. It should use minimax with alpha-beta pruning and a basic material evaluation function. Include comments and a command-line loop so a human can play against it.

This prompt gives the LLM clear limits. It names the language, library, algorithm, evaluation method, and interface.

Start with a Material Evaluation Function

The evaluation function is the part of the AI that scores a chess position. A positive score can mean White is better, while a negative score can mean Black is better.

A basic material score might use these values:

  • Pawn: 100
  • Knight: 320
  • Bishop: 330
  • Rook: 500
  • Queen: 900
  • King: very high value, though kings are not captured in legal chess

The AI counts White’s pieces and Black’s pieces, then subtracts one side from the other. If White has an extra queen, White gets a large score. If Black has an extra rook, the score moves in Black’s favor.

You can ask the LLM:

Create a Python function that evaluates a chess board using material values. It should return a positive score for White advantage and a negative score for Black advantage.

The answer should be small enough to inspect. That matters. If the LLM writes a huge program all at once, bugs can hide inside it. Smaller pieces are easier to test.

After the evaluation function, the AI needs a way to look ahead. Minimax assumes both players are trying to make the best move. White tries to maximize the score. Black tries to minimize it.

A depth of 1 means the AI checks only its immediate moves. A depth of 2 means it checks its move and the opponent’s reply. A depth of 3 checks one more move after that. Higher depths usually play better but take more time.

A prompt for this stage could be:

Write a minimax function for python-chess. It should search to a fixed depth, use the evaluation function at leaf nodes, and return the best score for the side to move.

Once that works, ask for alpha-beta pruning:

Modify the minimax function to include alpha-beta pruning. Keep the code readable and add comments explaining alpha and beta.

Alpha-beta pruning skips branches that cannot affect the final choice. This makes the AI faster without changing the result.

Let the LLM Help with Move Selection

After minimax returns scores, you need a function that chooses the best move from all legal moves. The function should loop through board.legal_moves, make each move, score the result, undo the move, and track the best option.

A good prompt:

Write a choose_move function for a python-chess AI. It should examine every legal move, call minimax after pushing the move, then pop the move. It should return the best move for the current player.

Ask the LLM to include clear variable names. Chess AI code becomes confusing quickly when names are vague.

Add a Human Play Loop

A simple command-line game is enough for a first version. The program can print the board, ask the human for a move in UCI format such as e2e4, check whether the move is legal, then let the AI respond.

Prompt example:

Create a command-line game loop where the human plays White and the AI plays Black. Use UCI move input. Reject illegal moves and print the board after each turn.

This gives you a playable bot. It may not be strong yet, but it will work.

Improve the AI with Better Scoring

Material alone is not enough. A bot that only counts pieces may miss checkmate threats, weak king safety, and poor piece placement.

You can improve the evaluation function with:

  • Piece-square tables
  • Mobility bonuses
  • King safety penalties
  • Pawn structure scoring
  • Checkmate and stalemate handling
  • Bonuses for castling
  • Penalties for doubled pawns
  • Bonuses for central control

Piece-square tables are a great next step. They give pieces extra points for standing on useful squares. Knights, for example, usually prefer central squares over corners.

Ask:

Add simple piece-square tables to the evaluation function. Keep the code compact and explain how the table values affect the score.

Small improvements can make the AI feel much smarter.

Use the LLM to Debug Carefully

LLMs can produce code that looks right but fails in edge cases. Chess has many edge cases, so testing is important.

Ask the LLM to write tests such as:

Write unit tests for the chess AI evaluation function and move selection function. Include tests for checkmate, stalemate, material advantage, and legal move selection.

You should also test real positions. Try positions where one side can win a queen, deliver checkmate, or avoid immediate loss. If the AI chooses a strange move, copy the board position and ask the LLM to inspect the search logic.

A useful debugging prompt:

This chess AI chooses a bad move in the following FEN position. Analyze the code and suggest why minimax may be scoring the position incorrectly.

FEN strings are helpful because they describe exact chess positions in a compact format.

Keep the Project Simple at First

A common mistake is trying to build a full chess engine right away. Start with a working bot that searches two or three plies. Then improve one part at a time.

A practical roadmap looks like this:

  1. Create the board with a chess library
  2. Add material evaluation
  3. Add minimax
  4. Add alpha-beta pruning
  5. Add move selection
  6. Add a human play loop
  7. Add piece-square tables
  8. Add tests
  9. Tune depth and performance

Each stage gives you a working version. If something breaks, you know where to look.

Example Prompt Workflow

Here is a full sequence you can use with an LLM:

I want to build a simple chess AI in Python using python-chess. First, create a minimal program that prints a chess board and lists all legal moves.

Then:

Add a material evaluation function that scores the board from White’s point of view.

Then:

Add minimax search with a depth parameter.

Then:

Add alpha-beta pruning to the minimax function.

Then:

Create a choose_move function that picks the best move for the side to move.

Then:

Add a command-line loop where I can play as White against the AI as Black.

This step-by-step method gives cleaner code and fewer errors than one giant prompt.

Using an LLM to write a simple chess AI works best when you give it clear, limited tasks. Let it generate small functions, explain algorithms, add tests, and improve the program in stages. Start with a chess library, basic material scoring, minimax, and alpha-beta pruning. Once the bot can play legal games, improve its evaluation function and test it against tricky positions. The result will not defeat top chess engines, but it will teach you how game search works and give you a fun AI project you can keep improving.