# Stop Burning Hidden Reasoning Tokens on Deterministic Math: Putting a Local AST Pre-Solver in Front of LLMs

> Source: <https://dev.to/omnivasai/stop-burning-hidden-reasoning-tokens-on-deterministic-math-putting-a-local-ast-pre-solver-in-front-3lj9>
> Published: 2026-12-04 19:40:23+00:00

**Recommendation:** 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.
More in the playbook "Controlling LLM cost": <https://wpnews.pro/playbooks/llm-cost>

If you've built anything on top of modern reasoning models (Claude, GPT, Gemini, DeepSeek), you've probably noticed an expensive pattern:

Ask 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.

Even 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.

The Architecture Shift: Pre-Solve Before the LLM

While building Omnivas Core, I flipped the pipeline around so deterministic expressions never reach an LLM in the first place:

text

User Prompt

   │

   ▼

[Stage 1: Local / Edge AST Pass-Through]

   ├── Deterministic match (arithmetic, quadratics, derivatives, integrals)

   │      └── Solve + verify locally → 0 LLM tokens billed

   │

   └── Open-ended / word problem / code

          └── Forward to LLM (Gemini, Claude, GPT, DeepSeek, Ollama)

                 └── Optional Stage 2: Closed-World Deterministic Gate

Keeping the Router Conservative

The 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:

js

export function evaluateAstPassThrough(prompt = '') {

  const clean = prompt.trim().toLowerCase().replace(/[?.!]+$/, '').trim();

  const wordCount = clean.split(/\s+/).filter(Boolean).length;

  // Never intercept multi-part, document, or code-generation prompts

  if (

    wordCount > 22 ||

    prompt.includes('\n') ||

    /\b(write|build|create|debug|refactor|explain why|essay|code)\b/i.test(clean)

  ) {

    return { matched: false };

  }

  // Example: Quadratic equation AST match (e.g. "Solve 2x^2 + 5x - 3 = 0")

  const quad = clean.match(

    /^(?:solve\s+)?([+-]?\d*)\s*x^2\s*([+-]\s*\d*)\s*x\s*([+-]\s*\d+)\s*=\s*0$/i

  );

  if (!quad) return { matched: false };

  const a = parseCoeff(quad[1]);

  const b = parseCoeff(quad[2]);

  const c = parseFloat(quad[3].replace(/\s+/g, ''));

  const disc = b * b - 4 * a * c;

  if (a === 0 || disc < 0) return { matched: false };

  const x1 = (-b + Math.sqrt(disc)) / (2 * a);

  const x2 = (-b - Math.sqrt(disc)) / (2 * a);

  return { matched: true, discriminant: disc, roots: [x1, x2] };

}

Anything 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.

Try It Live (No Login Required)

You 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).

I'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?
