
Implementing the Token-Efficient Router required a fundamental shift in how LLM pipelines are traditionally built: prioritizing deterministic machine verification over model self-confidence. The implementation starts with a lightweight, priority-ordered regex classifier. Rather than wasting API tokens on an LLM-based router, we categorized incoming natural-language tasks into eight buckets at zero cost. Once categorized, tasks enter the local lane powered by a heavily quantized Qwen3-4B model running on a llama-server background process. The heavy lifting of the implementation lies within the "gates"—the verification layer that prevents hallucinations from slipping through. We implemented category-specific, host-side checks: Execution Sandboxes: For code generation and debugging, we built a Python subprocess environment that dynamically executes the model's output against the prompt’s worked examples and assertions. Procedural Math/Logic: Instead of trusting the model's in-head arithmetic, the agent forces the model to write scripts to calculate numbers or brute-force logic puzzles using itertools. ONNX-Powered NLP Verification: We baked local, offline models directly into the pipeline. A DeBERTa-v3 NLI model handles entailment checking to verify summarization accuracy sentence-by-sentence, while a MiniLM embedder enables precise few-shot sentiment voting. Finally, the remote fallback implementation optimizes API usage through aggressive batching. When a task legitimately fails the local gates, it is grouped into category-specific batch lanes. This ensures heavy instruction headers are billed once per lane rather than once per task. A strict 540-second deadline guard watches over the entire single-process architecture, ensuring valid JSON results always cleanly write to disk before the system terminates.
13 Jul 2026