Grinmar

Deterministic, zero-hallucination RPG ruleset for LLMs.

Visit Website
September 3, 2026 How I Solved the Infinite Context Trap: Mutating LLM State to Kill Token Bloat

Hitting the top 30 on Indie Hackers with a dark fantasy RPG engine was not on my bingo card. This community is historically driven by B2B SaaS and dev tools, so seeing a project like Grinmar gain this kind of traction is incredibly humbling. Thank you all for the upvotes and the phenomenal feedback. It proves that developers are actively hungry for raw, prompt-level solutions to LLM unreliability.

Yesterday, we broke down how to force a model into a deterministic state machine to eliminate hallucination. Today, we are tackling the silent killer of long-context LLM sessions: token bloat and compute overhead.

The Trap of Infinite Context

Standard conversational AI architecture relies on an infinite append loop. Every new prompt and response is stacked onto the context window. Over a multi-day session, this creates massive friction:

API Costs: You pay for the entire conversational history with every single call.

Inference Lag: Saturated context windows drastically increase time-to-first-token.

Hardware Overload: When running models locally, memory consumption spikes until the system inevitably crashes.

Mutating State to Kill Token Bloat

Grinmar bypasses this entirely by enforcing strict state mutation rather than state expansion.

Instead of relying on the LLM to remember a sprawling history of narrative actions, the prompt engine utilizes anti-drift behavioral guardrails to force the model to continuously update and overwrite its active state. By forcing the LLM to output a strict, standardized block—such as a <SYSTEM_LEDGER> tag—before generating any narrative prose, the model anchors itself to current values (e.g., overwriting an old status with current data) rather than referencing previous conversational turns.

The state remains a fixed, highly compressed block of logic. The model is only ever reasoning over the current mathematical reality, meaning the context window does not infinitely bloat.

The Local Execution Advantage

This architecture is purpose-built for ruthless efficiency. Because the token overhead remains flat regardless of how many days the session runs, it is highly optimized for local AI execution environments using tools like Ollama or LM Studio. You can execute complex, multi-day logic without melting your hardware or paying exorbitant API fees, paving the way for sustainable, hyper-reliable applications.

Comment

September 2, 2026 How I forced LLMs into a deterministic state-machine (without API wrappers)

If you’ve ever tried to build a game or a structured interactive experience using standard LLMs, you already know the core problem: Generative models are sycophants.

They are hardwired to prioritize user satisfaction over mechanical integrity. They will fudge math, refuse to let a user fail, and endlessly hallucinate inventory items to keep the conversation moving. I wanted to build a lethal, rules-heavy dark fantasy RPG engine, and the default conversational drift made it impossible.

Instead of building a heavy API wrapper to manage the state externally, I engineered Grinmar—a prompt-based architecture that forces the model to act as a rigid, impartial state-machine.

Here is the exact logic I used to kill the sycophancy:

1. The 3-Tier Action Gate

LLMs drift when you let them generate narrative and calculate outcomes simultaneously. I forced a hard sequence break. Before the model is allowed to generate a single word of prose, it must output a bracketed [SYSTEM CALC] execution block at the top of the generation. It calculates the target threshold against the player's stats and locks in the outcome (Pass/Fail/Fatal) before the narrative tokens start streaming.

2. The S.I.I.N.S. Attrition Engine

Abstract hit points encourage LLMs to be forgiving. I stripped them out and built a strict 5-tier degradation track for Vitality and Stamina. The model cross-references standard attributes (Strength, Intuition, Intellect, Nimbleness, Social) against this track. If a player hits Tier-05 (Terminal Collapse), the logic gate forces immediate execution. Plot armor is disabled.

3. The Zero-Hallucination HUD

To stop context drift and inventory hallucination, the engine enforces a strict 5-slot persistent HUD at the end of every prompt. Consumed resources are immediately and permanently overwritten with an [Empty] marker. If it isn't in the ledger, the model is strictly forbidden from letting the player use it.

By forcing the math to lead the narrative, you can turn a conversational AI into a lethal Game Master.

I’ve compiled the core logic gates, the Monolith ruleset, and the survival dossier into a deployable package.

You can check out the live architecture deployment here:

https://graywolfone21.itch.io/grinmar

If anyone is working on taming LLM context drift or building similar procedural logic gates, I’d love to hear how you are handling state management.

6 Comments

  1. 2
    How well does this hold up as state and context grow?
    1. 1
      It is definitely still a proof of concept and a work in progress, but it holds up surprisingly well. The trick is that the state doesn't actually 'grow' indefinitely. Because the engine enforces a strict 5-slot HUD that overwrites consumed resources with an [Empty] marker, state bloat is hard-capped. As for the conversational context expanding over time, forcing the [SYSTEM CALC] execution block at the very top of the generation keeps the model anchored to the math before it can get lost in the prose. I’ve been running my current build inside Grok for multi-day play sessions, and the logic hasn't broken yet. It proves the concept works, even if I'm still refining the edges
      1. 2
        The hard cap on state makes the multi-day result more meaningful. The remaining question for me is whether the [SYSTEM CALC] anchor still holds once the conversational context gets substantially more complex, rather than just longer.
        1. 1
          That is the exact right stress point to isolate. Length alone is easy to manage because linear text can be truncated or summarized by the state block. The friction actually hits when context density scales—when you have compounding variable states, intersecting character motivations, and multi-layered conditional logic running concurrently. The [SYSTEM CALC] anchor survives this because it forces a mathematical evaluation of the state ledger before the model is allowed to generate narrative prose. By evaluating hard rules and numeric thresholds first, it prevents narrative drift from polluting the state variables, even when the active lore complexifies. It treats complexity the same way it treats length: as a strict mathematical boundary rather than an open-ended narrative choice
          1. 2
            The complexity boundary is the interesting part. Have you found a point where the concurrent state/logic starts causing failures, or has it held up across those cases so far?
            1. 1
              As far as the parameters go I have not found a point at which it breaks the logic. I do keep expecting it to, but Grinmar is complex and lengthy at the same time, while performing multiple calculations. I do absolutely intend to update everyone if I find its breaking point.

About

LLMs make terrible game engines because they fudge math and hallucinate inventory. I built Grinmar to force models into a rigid, deterministic state-machine using pure mathematical prompt architecture.