{"slug": "stop-burning-hidden-reasoning-tokens-on-deterministic-math-putting-a-local-ast", "title": "Stop Burning Hidden Reasoning Tokens on Deterministic Math: Putting a Local AST Pre-Solver in Front of LLMs", "summary": "A developer building Omnivas Core described an architecture that routes deterministic math problems through a local AST pre-solver before any LLM call, so expressions like quadratics and arithmetic are solved and verified on-device at zero token cost. A conservative intent guard skips interception for multi-part, code-generation, or long prompts, forwarding those to models such as Claude, GPT, Gemini, DeepSeek, or Ollama, with an optional deterministic gate for verifying reasoning outputs. The developer says the approach avoids the 1,500–3,000 hidden reasoning tokens models typically spend on simple problems and is testable as a guest on omnivas.ai.", "body_md": "If you've built anything on top of modern reasoning models (Claude, GPT, Gemini, DeepSeek), you've probably noticed an expensive pattern:\n\nAsk a reasoning model to solve a straightforward quadratic like 2x^2 + 5x - 3 = 0 or evaluate (17 * 24) + (89 * 89), and it will happily burn 1,500–3,000 hidden thinking tokens before returning an answer that a 50-line parser could compute deterministically.\n\nEven worse, the usual fix—LLM tool-calling (having the model call Python or SymPy)—still pays for a full LLM round-trip and hidden reasoning tokens before and after the tool call.\n\nThe Architecture Shift: Pre-Solve Before the LLM\n\nWhile building Omnivas Core, I flipped the pipeline around so deterministic expressions never reach an LLM in the first place:\n\ntext\n\nUser Prompt\n\n   │\n\n   ▼\n\n[Stage 1: Local / Edge AST Pass-Through]\n\n   ├── Deterministic match (arithmetic, quadratics, derivatives, integrals)\n\n   │      └── Solve + verify locally → 0 LLM tokens billed\n\n   │\n\n   └── Open-ended / word problem / code\n\n          └── Forward to LLM (Gemini, Claude, GPT, DeepSeek, Ollama)\n\n                 └── Optional Stage 2: Closed-World Deterministic Gate\n\nKeeping the Router Conservative\n\nThe biggest trap with pre-LLM interception is being too aggressive and accidentally hijacking a complex multi-part prompt. In practice, adding a strict intent guard before matching keeps false positives at zero:\n\njs\n\nexport function evaluateAstPassThrough(prompt = '') {\n\n  const clean = prompt.trim().toLowerCase().replace(/[?.!]+$/, '').trim();\n\n  const wordCount = clean.split(/\\s+/).filter(Boolean).length;\n\n  // Never intercept multi-part, document, or code-generation prompts\n\n  if (\n\n    wordCount > 22 ||\n\n    prompt.includes('\\n') ||\n\n    /\\b(write|build|create|debug|refactor|explain why|essay|code)\\b/i.test(clean)\n\n  ) {\n\n    return { matched: false };\n\n  }\n\n  // Example: Quadratic equation AST match (e.g. \"Solve 2x^2 + 5x - 3 = 0\")\n\n  const quad = clean.match(\n\n    /^(?:solve\\s+)?([+-]?\\d*)\\s*x^2\\s*([+-]\\s*\\d*)\\s*x\\s*([+-]\\s*\\d+)\\s*=\\s*0$/i\n\n  );\n\n  if (!quad) return { matched: false };\n\n  const a = parseCoeff(quad[1]);\n\n  const b = parseCoeff(quad[2]);\n\n  const c = parseFloat(quad[3].replace(/\\s+/g, ''));\n\n  const disc = b * b - 4 * a * c;\n\n  if (a === 0 || disc < 0) return { matched: false };\n\n  const x1 = (-b + Math.sqrt(disc)) / (2 * a);\n\n  const x2 = (-b - Math.sqrt(disc)) / (2 * a);\n\n  return { matched: true, discriminant: disc, roots: [x1, x2] };\n\n}\n\nAnything that doesn't cleanly match falls straight through to the LLM—and if strict verification is needed on word problems or reasoning outputs, a toggleable Deterministic Gate enforces closed-world checks around the model response.\n\nTry It Live (No Login Required)\n\nYou can test the local AST pass-through and Deterministic Gate directly at omnivas.ai (no signup needed for guest prompts—clicking the Solve 2x² + 5x - 3 = 0 chip executes locally with 0 LLM API calls).\n\nI'd love to hear from other devs building AI apps: what other deterministic classes (regex validation, cron parsing, unit conversions, SQL linting?) would you intercept before touching an LLM?", "url": "https://wpnews.pro/news/stop-burning-hidden-reasoning-tokens-on-deterministic-math-putting-a-local-ast", "canonical_source": "https://dev.to/omnivasai/stop-burning-hidden-reasoning-tokens-on-deterministic-math-putting-a-local-ast-pre-solver-in-front-3lj9", "published_at": "2026-12-04 19:40:23+00:00", "updated_at": "2026-12-04 19:44:22.080525+00:00", "lang": "en", "topics": ["large-language-models", "ai-agents", "ai-tools"], "entities": ["Omnivas Core", "omnivas.ai", "Claude", "GPT", "Gemini", "DeepSeek", "Ollama", "SymPy"], "also_reported_by": [], "takeaway": "Intercepting deterministic math with a local AST parser behind a strict intent guard (skipping prompts over 22 words, multi-line, or containing code/write verbs) solves quadratics and arithmetic at zero LLM tokens while letting everything else fall through to the model.", "recommendation": {"advice": "Route short, single-expression math through a local AST solver with a strict intent guard before calling an LLM, and forward anything multi-part, multi-line, or code-related untouched to avoid false positives.", "playbook": "llm-cost", "playbook_url": "https://wpnews.pro/playbooks/llm-cost"}, "alternates": {"html": "https://wpnews.pro/news/stop-burning-hidden-reasoning-tokens-on-deterministic-math-putting-a-local-ast", "markdown": "https://wpnews.pro/news/stop-burning-hidden-reasoning-tokens-on-deterministic-math-putting-a-local-ast.md", "text": "https://wpnews.pro/news/stop-burning-hidden-reasoning-tokens-on-deterministic-math-putting-a-local-ast.txt", "jsonld": "https://wpnews.pro/news/stop-burning-hidden-reasoning-tokens-on-deterministic-math-putting-a-local-ast.jsonld"}}