← CookbookView source on GitHub β†—

🐍 LLM Snake Battle

A visual snake battle game where two LLMs compete against each other! Watch as AI models control their snakes in a battle of survival.

Key feature: each snake moves independently and asynchronously β€” the moment its model responds β€” so faster models move more often. Game speed is driven by model latency, not a fixed turn delay.

Features

How to Play

  1. Open snake.html in your web browser
  2. Enter your API URL (already filled with Nebius Token Factory endpoint)
  3. Paste your API Key
  4. Click Load Models to fetch available models
  5. Select different models for Player 1 (Red) and Player 2 (Blue) via the searchable dropdowns
  6. (Optional) Adjust Options β€” Visibility Radius, Collision Avoidance, Thinking Mode, Debug Mode
  7. Click Start to begin the battle

Controls in the left pane: Start, Pause ⏸️, Restart πŸ”„, and a Loop toggle. The πŸ”§ Extra section has the Speed Test benchmark and sort buttons.

What's New (v1 β†’ v2)

Game Rules

Tip: Wall wrapping creates interesting strategies - you might wrap around the board quickly to reach a far-away fruit or trap your opponent!

Tech Stack

File Structure

β”œβ”€β”€ snake.html      # Main HTML structure
β”œβ”€β”€ style.css       # Styling and animations
β”œβ”€β”€ game.js         # Game logic, rendering, and API integration
β”œβ”€β”€ benchmark.js    # Model performance testing system
└── README.md       # This file

Customization

You can modify the game by editing game.js:

const GRID_SIZE = 30;                 // Board dimensions (30Γ—30). Try 20 for faster games, 40 for more space
const NUM_FRUITS = 3;                 // Fruits on the board at once
const LLM_TIMEOUT_MS = 30000;         // Per-request LLM timeout (30s)
const API_RETRY_DELAY_MS = 2000;      // Base retry delay (also seeds timeout backoff)
const MAX_TOKENS_CASCADE = [10, 100, 1000, null]; // Adaptive token limits per player
let   VIEW_RADIUS = 5;                // Default snake vision radius (1–30, adjustable from UI)
let   collisionAvoidanceEnabled = true; // LLM hints + auto safe-move override
let   thinkingModeEnabled = false;    // Pass enable_thinking to chat_template_kwargs
const MAX_LOG_ENTRIES = 100;          // Cap on game-log <p> entries (oldest trimmed)

// Snake starting positions (controlled in initializeGame)
// LLM prompting logic (in getBoardState)

Note: There is no fixed "turn delay" β€” snakes move at the natural speed of their model's responses. If you want to add artificial pacing, you would add a delay inside moveSnakeWithLLM before it recurses.

API Compatibility

Works with any OpenAI-compatible API that supports: - GET /models endpoint (verbose metadata is tried first for modality filtering, with plain fallback) - POST /chat/completions endpoint - Standard message format

Tested with Nebius Token Factory but should work with others (OpenAI, compatible proxies, Ollama, LM Studio). Models are filtered by output modality: any model that produces text output is included (so vision-capable text LLMs like moonshotai/Kimi-K2.6 are selectable), while pure image/audio generation models are excluded.

Troubleshooting

"Failed to load models" error: - Check your API URL ends with / - Verify your API key is correct - Check browser console (F12) for detailed errors - Some APIs require CORS to be configured to work from the browser

Snakes don't seem smart: - Try different models - some are better at this task than others - Models get the full 30s timeout to respond; slower models simply move less often - Keep Collision Avoidance on for safer gameplay, or turn it off to see raw LLM decisions - The prompt is intentionally simple - complex strategic behavior may need prompting adjustments

Game too slow/fast: - Game speed is determined by model response times β€” faster models move more often - Try a faster model for comparison (the move counters and latency graph make speed differences visible) - Smaller grid sizes (edit GRID_SIZE in game.js) result in quicker games

One snake barely moves: - That model is slow to respond β€” it will move less frequently (intended behavior) - Check the latency graph/stats for that player - After 3 consecutive API failures (or 5 consecutive timeouts) a model forfeits the round

Enjoy watching AI snake battles! πŸβš”οΈπŸ