Blog 1 of 2: I Built Snake with Codex to See How AI Decision Models Play
Repo Link: https://github.com/rjavedv/snake-ai
This is the first blog in the two blog series.
1. How the idea started
Well it was some weeks ago or some months ago, my youtube timeline just exploded with Videos about “System One Models and Jev” from TypeSafe AI. And as usual, the hype trend generated many amazing headlines like ‘Is it the end for Claude’ and the Model to end all other models etc…. I watched some videos, went to the TypeSafeAI webpage (https://typesafe.ai/blog/introducing-system-one-models-and-jev)and saw the Doom demo that was using JEV for game play and making ten decisions a second. Wow! That was some speed.
That got me very excited and naturally I tried to get the API key to try some stuff for myself. Unfortunately, at that time it was hard to get the API key for Jev, i think the sign up was invitation only or a waitlist. So with no chance to get an API key, I quickly lost interest but I still couldn’t forget that Doom demo.
And as the trend is, within days we have many open source decision models on Hugging Face and GitHub, most claiming to be Jev killer :-).
So this got me thinking if i can have a game that can be played by these decision models, would be wonderful. Although the Doom demo was very tempting to try, I wanted something simple to start with.
My first thought was to try TicTacToe but it was kinda static, i mean not much movement in terms of gameplay. So my second thought was to try a snake game. It is quite visual (a snake or a line running around, consuming food tiles) and comparatively simple to develop.
2. Building the first playable version with Codex
Game design for a human player was quite clear (in my head), what I was not clear was how to involve a decision model in game play. What should I send to model, what am I supposed to get back and how to map that to the game render loop. This is where I decided to use Codex as my development partner.
My first prompt was quite wayward. I generally use Python so I already created a project and a virtual environment inside a folder. I created a new project in codex with reference to this folder and then asked it to create a snake using pygame. For AI gameplay, my prompt was like the following
The game you will design and develop will have two modes. One mode for human use where a human can play the game using arrow keys. Other game mode will be for AI where we want AI to play this game. For this purpose
1: AI Models: we have Ollama on this machine with a decision model downloaded on this machine as following. Documentation and sample usage url is also given with the model and you can check the sample code on the url.
Model Name: kev:27b-nvfp4
Documentation: https://ollama.com/library/kev
2: In the AI mode, the game should pass a state object to the AI model which describes the snake position, food positions, and wall positions. System should ask the AI that based on the given game state, which arrow key should be pressed (highest or most likely probability)
3: Based on this input, the selected arrow key code should be passed to game to simulate the key press. Based on the arrow key, the game should move to a new state where the snake would have moved to a new location based on the arrow key. this state should again pass to AI, asking for the next decision.
You should start with a plan phase. Ask me questions if any further information is needed. Once all the required information is provided, proceed to develop the Game in python. Log all requests and responses to a decision model in a log file using json format.
As expected, Codex did ask me a few questions and created what I wanted. Since I want to keep things simple, I used a .env file to store the Ollama local url and the local model to use. So in the main game window, we have very simple key controls to navigate. Pressing H key will start a game for human player while pressing A will start the AI game play. Esc key will cause the game to quit.
For the AI gameplay, the games initially read .env (i later changed it to a .config file) to read the Ollama local url and the model name to run.
All of this is implemented in three files.
engine.py is about game defaults and game rules.
app.py is where the actual game rendering happens using pygame.
ai.py is where all requests/response cycles to decision model happens.
3. Handing the arrow keys to a decision model
This is where I really appreciate Codex. It came up with a pretty solid request/response structure for AI gameplay. Later on, I tweak the prompt that is sent to the decision model to make it more clear.
The idea is to send the game board and snake data to the Decision model along with food coordinates. We tell the decision model to avoid walls and snake body and move towards the food. In its core it’s a choice question where we present the above information along with direction keys to the decision model. The model then returns the choice dictionary with direction key and probability. Obviously the direction key with highest probability is what the model is suggesting as the next move. All these requests and responses were logged in a logfile for later review.
One interesting thing here is that initially we were asking the model to choose from all four direction keys. Strange enough we do get the response that is totally in the opposite direction, like the snake is moving downwards and the model returned UP as the most likely choice. Which might be logically correct but not possible in game context. As in the Snake game, you cannot move opposite to your movement direction. So the request format was adjusted to ask the model to choose from three possible directions instead of four. This improved the gameplay a little bit.
Now the issue with log files was that they were just files. I mean I tried reading files to understand how a model is playing the game but it gets hard to visualize gameplay from just numbers.
In the screenshot below, we can see a request and response from the log file. There are some additional ‘event types’ are also put in log file for example ‘session_start’ holds information about model, game speed, initial state of snake, food, score and steps. Similarly we have an event type ‘transition’ that shows the board state after we have acted upon ‘response’ showing the snake new state and which key was selected.
4. Building the log viewer to see what happened
As apparent from the above screenshot of the log file, it was hard to visualize what was going on with the game. To solve this issue, I prompted Codex to build a log viewer where we can ‘play’ a log file on game board using up and down arrow keys. These two keys are used to move to different rows in log file.
As per the prompt, Codex generated a solution where the game window was shown on the left and log file was shown on the right side of screen. User can use up/down arrow keys to navigate the log file and the ‘current’ row is then rendered on game screen. Game window was pygame window while file preview window was rendered using tkinter.
Refer to the above screenshot, following are the key areas for this log file viewer
1: Name of the log file currently being viewed
2: Current row number of the log file
3: Row and Column numbers were added to help in visualize the log file entries.
4: Snake as list of coordinates.
5: Food Location
All of the replay logic is held in replay.py file
5. OpenRouter joins the party
While I was enjoying the snake game playing with various Ollama decision models, I found out the Jev is available on OpenRouter. So I quickly went there to verify and yes, it was there along with several other decision models. That was indeed wonderful.
Now the question was how to change my existing snake game. Luckily the API was the same as used with Ollama decision models. Only difference with OpenRouter was that you have to send OpenRouter API key in ‘Authorization’ request header.
It was at this point that I restructured the .config file. Added an AI_PRIVODER option with possible values as ‘ollama’ or openrouter’. Local AI Model Name and Ollama base URL were already there in config, I just added OpenRouter Model Name and OpenRouter base URL. For these changes, few adjustments were made in ai.py file.
6. So it was working fine or was it ….
So by now we had a snake game that you can play as human or use different Decision models to play it. Record the AI progress in log file and then replay the log file on the game board. It was at this point that I realized I do not have a way to compare two different decision models. Since Food appears randomly, we cannot judge properly how Model A performed against Model B.
This is where the idea of benchmarking started. This is discussed in my second blog : [[Link to second blog]]


