How to Build an AI Agent That Plays Pokémon (and Why It Will Make You a Better Builder)
You don't need to train a foundation model to build an agent that can play Pokémon. In fact, you can do it with an API call to any major LLM and about 200 lines of Python. The trick is designing the right interface between the model and the game. Here's a step-by-step approach that I've used to get Claude, GPT-4, and Gemini all playing Pokémon Red on a local emulator.
First, you need a Game Boy emulator with a scripting interface. PyBoy is the gold standard here — it's a Python-based Game Boy emulator that gives you pixel-level access to the screen and memory. You can read the current frame, detect sprites, and even read the game's internal variables like the player's position, Pokémon party, and item inventory. This is your AI's eyes and ears.
Next, you need to translate that raw game state into a text prompt. Don't feed the model raw pixels — it's too slow and expensive. Instead, extract key information: your current location, your Pokémon and their HP, the wild Pokémon you're facing, and your available items. Format this as a structured text block. For example: 'Location: Route 1. Party: Charmander (HP 20/20), no other Pokémon. Wild encounter: Pidgey (HP 12/12). Available actions: FIGHT (Scratch, Growl), BAG (Potion x3), RUN.' The model can then reason about what to do.
Now, the hard part: prompting. You need to give the model a persona that encourages strategic thinking. I use a system prompt like: 'You are an expert Pokémon trainer. Your goal is to beat the Elite Four as fast as possible. You must optimize for speed, not completion. Avoid unnecessary battles. Prioritize leveling your starter. Use items sparingly. Output only a single action: FIGHT [move], BAG [item], SWITCH [Pokémon], or RUN.' This constrains the output to a parseable format while giving the model enough context to make smart decisions.
But here's where most builders fail: they don't handle errors. The model will occasionally output an invalid action — like trying to use a move that doesn't exist or switching to a fainted Pokémon. You need a robust error handler that either retries with a corrected prompt or falls back to a safe action like 'RUN'. I log every invalid action and feed it back into the next prompt as a lesson: 'Your previous action was invalid because Charmander does not know Water Gun. Choose a valid action.' This gives the model a chance to learn from mistakes.
Finally, you need to think about speed. LLM API calls take 1-3 seconds, but the game keeps running. You can either pause the emulator while waiting for a response (which makes it less realistic) or let the game run in real-time and have the AI react every few frames. I prefer the latter — it forces the model to make decisions under time pressure, which is closer to real-world agent deployment. You can also cache common responses for repetitive situations like wild battles.
The result? A surprisingly competent AI player that can beat the first few gyms without much trouble. The real value, though, is what you learn about prompt engineering, error handling, and state representation. These skills transfer directly to building any AI agent — whether it's a chatbot, a code assistant, or a trading bot. So fire up PyBoy, grab an API key, and start training your own Pokémon master. Your users will thank you.
Build an AI Ethics Policy
Act as a legal and ethics advisor for an AI startup. Draft a 3-paragraph internal policy outlining red lines for military or defense applications, including employee opt-out rights and public transparency commitments. Use concise, enforceable language.