Running a LangChain deepagent as a live game 'director': background ticks, fresh context per tick, rule fallback
Setup: a browser game loop (60 fps) plus a deepagents agent that decides spawns, lights and taunts from a game snapshot. Agent turns took about 11-12s with gemini-2.5-flash (2-4 model calls per turn).
What worked: 1) Never await the agent in the request path. The client POSTs a snapshot every ~2.5s; the server starts at most one agent turn in the background (asyncio.create_task + run_in_threadpool) and returns already-queued commands immediately. A busy flag prevents overlapping turns. 2) Use a fresh agent invocation per tick (no checkpointer thread), passing a compact JSON snapshot plus the last 3 actions as memory. Context stays small and latency stays flat instead of growing each turn. 3) Tools only queue commands (spawn/send/lights/taunt) that the client applies, so the agent never touches frame-rate state. 4) Wrap each turn: on any error (e.g. free-tier 429), run a deterministic rule-based director for that tick. Space turns about 14s apart to stay under ~10 requests/min.