{"slug": "tackling-llm-prompt-bloat-building-a-just-in-time-context-router-in-rust-part-8", "title": "Tackling LLM Prompt Bloat: Building a Just-In-Time Context Router in Rust (Part 8)", "summary": "A developer built a just-in-time context router in Rust for the Flowork OS agent framework to combat \"Fatal Prompt Bloat,\" where global system prompts balloon from 500 to over 8,000 tokens as rules accumulate. The router keeps a minimal ~400-token anchor prompt and injects domain directives only when environmental conditions match, evaluating triggers natively in Rust with a sub-50ms fast-path timeout. Individual trigger directives are kept under 250 words, and the router, cognitive loop, and Landlock security sandbox are open-sourced.", "body_md": "As autonomous agent frameworks grow in complexity, developers continually add rules, domain doctrines, coding style guides, and edge-case remediations.\n\nThe easiest path—and the most common anti-pattern—is simply appending every new rule to the global system prompt. Within a few months, the base prompt balloons from 500 tokens to 8,000+ tokens.\n\nThis phenomenon, known as **Fatal Prompt Bloat**, has severe consequences:\n\nWhen architecting the cognitive subsystem for [Flowork OS](https://floworkos.com/), we outlawed always-on static prompts in favor of a **Formula 5-3-3 Just-In-Time Sliding Window Router** written in Rust.\n\nUnder the Flowork OS system prompt architecture, the base prompt remains locked to a minimal anchor (~400 tokens covering identity and approval boundaries).\n\nEvery other domain instruction exists as a **Passive Trigger** (`/opt/floworkos/system/system_promp/promps/triggers/`) or a native compiled Rust rule checker (` rules/.bin/*`).\n\nDirectives are injected dynamically only when their environmental conditions match:\n\nTo ensure dynamic context injection introduces zero noticeable user latency, rule evaluation is executed natively in Rust (`mrflow-rs/src/cognitive/mod.rs`):\n\n``` js\n// Fast-path landmark and structural inspection (<50ms timeout)\nlet structural_checks = vec![\n    (\n        is_build_intent && (has_tool(\"compile_code\") || has_tool(\"run_command\")),\n        \"triggers/code_engineering.md\",\n    ),\n    (\n        is_search_intent && (has_tool(\"grep_search\") || has_tool(\"glob\")),\n        \"triggers/search_efficiency.md\",\n    ),\n    (\n        cwd.join(\"FLOWORK.md\").exists() || cwd.join(\".flowork/MEMORY.md\").exists(),\n        \"triggers/memory_routing.md\",\n    ),\n];\n```\n\nBy keeping individual trigger directives dense and strictly under 250 words, the agent retains razor-sharp focus on the task at hand without suffering cognitive drift.\n\nThe entire prompt router, native cognitive loop, and Landlock security sandbox are open-source.", "url": "https://wpnews.pro/news/tackling-llm-prompt-bloat-building-a-just-in-time-context-router-in-rust-part-8", "canonical_source": "https://dev.to/mr_flow/tackling-llm-prompt-bloat-building-a-just-in-time-context-router-in-rust-part-8-43kh", "published_at": "2026-11-27 16:37:11+00:00", "updated_at": "2026-11-27 16:42:28.123323+00:00", "lang": "en", "topics": ["ai-agents", "large-language-models", "ai-tools", "developer-tools"], "entities": ["Flowork OS", "Rust", "Landlock"], "also_reported_by": [], "takeaway": "Replacing an always-on system prompt with a Rust-evaluated just-in-time router that injects sub-250-word triggers only when conditions match keeps the base prompt near 400 tokens instead of 8,000+.", "recommendation": {"advice": "Keep a minimal anchor prompt and inject dense, condition-matched trigger directives under 250 words via a fast native router instead of appending every rule to the global system prompt.", "playbook": "agent-architecture", "playbook_url": "https://wpnews.pro/playbooks/agent-architecture"}, "alternates": {"html": "https://wpnews.pro/news/tackling-llm-prompt-bloat-building-a-just-in-time-context-router-in-rust-part-8", "markdown": "https://wpnews.pro/news/tackling-llm-prompt-bloat-building-a-just-in-time-context-router-in-rust-part-8.md", "text": "https://wpnews.pro/news/tackling-llm-prompt-bloat-building-a-just-in-time-context-router-in-rust-part-8.txt", "jsonld": "https://wpnews.pro/news/tackling-llm-prompt-bloat-building-a-just-in-time-context-router-in-rust-part-8.jsonld"}}