LLM-Orchestrated Multiplayer Game Backend
A multiplayer game is a surprisingly unforgiving stress test for LLM orchestration: players double-click, models time out, and nobody wants to wait on an HTTP request while four prompts and an image pipeline run. I built the TypeScript/Node backend on queued workers, a four-stage LangGraph pipeline, MariaDB transactions, and an eval layer that scores both offline scenarios and live turns.
Most LLM demos assume a single user waiting patiently for one streaming response. A multiplayer game breaks that assumption in the first thirty seconds: players hammer the button, turns mutate shared state, scene art takes longer than prose, and a slow model call will happily lock up an entire Express worker if you let it sit inside an HTTP request.
I built this backend in TypeScript and Node.js so the synchronous web tier never waits on a model. HTTP requests validate input with Zod, write a queued job to MariaDB, and return a lightweight receipt right away. Background workers do the heavy lifting and push completed turns and imagery back to connected clients over Socket.IO.
Decoupled workers and transactional state
Splitting turn execution into explicit stages kept the LLM work from corrupting game state when a provider stalls or a worker restarts:
- Precompute worker: leases a queued turn, runs deterministic game checks, executes the LangGraph story pipeline, writes a provisional manifest, and enqueues the apply stage.
- Apply worker: rehydrates the precomputed artifacts and commits the turn inside a database transaction that locks the game row, preventing double-processing when multiple workers compete.
- Image worker: resolves scene artwork cache-first via fingerprinted entries, falling back to on-demand generation and persisting image metadata inside a transaction before emitting
imageReadyover Socket.IO. - Idempotency and recovery: manifest hashes, lease management, dead-letter diagnostics, and a stuck-job sweeper on MariaDB keep crashed or timed-out jobs from hanging a game, and a Socket.IO relay ensures live events reach browsers even when multiple app processes are running.
The four-stage LangGraph story pipeline
Every turn — whether it’s live gameplay, an inspector run, model qualification, or an offline eval — enters through the same LangGraph graph. Instead of stuffing the entire turn into one giant prompt, the graph runs four distinct stages: master state resolution, narrative generation, and then player actions and image prompting in parallel, each with fallback-model recovery. Model selection resolves in a clear order: theme-forced model, then player preference, then the active release default.
Game content (prompts, events, items, image templates, and model catalogs) lives in versioned reference releases loaded independently of code deploys, so prompt or catalog updates don’t require rebuilding the server.
Tracing and evaluating turns
When LangSmith is enabled, trace headers travel inside the MariaDB queue-entry metadata so precompute, apply, and image workers stitch back into one end-to-end trace per player turn. That gives operators one place to inspect queue latency, model behavior, and failures alongside Prometheus /metrics and /api/health.
To judge a prompt or model change before players ever see it, I paired the pipeline with an evaluation layer:
- Deterministic rule checks validate structural verdicts, action validity, and narration length without spending judge tokens.
- LLM judges score narration quality, action relevance, and image-prompt fidelity on a 0-to-1 scale with written reasoning.
- Offline experiments and live sampling run the same scorers over saved scenarios on draft or published prompts, and score a configurable sample of committed live turns in the background without ever delaying gameplay.
Where it fits
It applies the same rule-first-then-judge pattern from my enterprise evals and observability work to an asynchronous TypeScript production stack: deterministic state transitions where correctness can’t drift, LLM calls isolated behind queues and schemas, and every turn traceable after the fact.