Dual-Stage (L1 Heuristic Fast-Path + L2 Neural Semantic Escalation) Cascade Router for LLM Swarms.
Intercepts structured code, SQL, math, and closed-world tasks in CPU microseconds (<15µs) for $0.00, with integrated Level 2 (L2) neural centroid classification for unstructured chat and speculative multi-model cascades.
💡 Looking for just the zero-dependency L1 pre-filter? If you already have an LLM client or neural router and only need the fast microsecond gate function, install
krusch-pre-router(npm install krusch-pre-router, 0 dependencies, <10KB gzipped).
In CPU architecture, the processor does not query main RAM or NVMe storage for every instruction—it checks the L1 cache in 1 clock cycle. If there is an L1 hit, execution proceeds instantly with zero memory bus overhead.
In multi-model agent systems, using an LLM or neural embedding model to decide where to route an obvious Python script, SQL query, LaTeX proof, or JSON transform is an expensive anti-pattern:
- The Routing Tax: Adds 300ms–800ms of Time-To-First-Token (TTFT) and auxiliary prompt token charges to every single step in an agentic loop.
- The Dual-Stage Solution:
krusch-cascade-routerunifies Stage 1 (L1) Pre-Router Gating (powered bykrusch-pre-router) with Stage 2 (L2) Neural Semantic Escalation (powered bykrusch-context-mcpor in-process centroid embeddings). It executes high-confidence structured traffic in < 15 microseconds on CPU for $0.00, while seamlessly escalating ambiguous, conversational chat to an L2 Neural Centroid Classifier before dispatching across the specialist pool or frontier models.
-
⚡ Sub-Millisecond L1 Pre-Filter: Evaluates syntax, query length, structure, and domain keywords in microseconds on CPU without making pre-flight routing calls (powered by
krusch-pre-router). -
🧠 Level 2 (L2) Neural Semantic Escalation: Classifies unstructured or ambiguous prompts via vector centroid cosine distance (
createCentroidSemanticRouterorcreateContextMcpRouter), eliminating blind defaults. -
🎯 4-Model Specialist Routing via OpenRouter: Out-of-the-box factory preset orchestrating 4 specialized domain models (
deepseek-v4-flash,Qwen3-Coder-Next,gemini-3.1-flash-lite, anddeepseek-v4-pro) unified through OpenRouter. Fully swappable viacustomModels. - 🧠 Knowledge Boundary Routing: Detects closed-world self-contained tasks (syntax, math, regex, formatting, translation) to keep them on fast edge models.
-
⚡ Speculative Parallel Hedging: Pre-warms heavy models in parallel on borderline confidence queries (
[0.25, 0.70]) to mask sequential cascade latency. -
🛡️ Logprob & Silent Failure Gating: Inspects initial token logprob confidence and monitors sliding-window repetition /
$n$ -gram loops to abort unhelpful outputs early.
| Best Used For ✅ | Poor Fit / Not Recommended ❌ |
|---|---|
| Agentic Loops & Microservices: Multi-step workflows where saving 300–800ms TTFT routing overhead per tool call compounds significantly. | Latency-Insensitive Frontier Monoliths: Offline tasks where maximum reasoning depth is required on 100% of inputs (e.g., formal legal briefs, theorem proving) regardless of cost. |
| Code, STEM, Math, SQL, Formatting: Tasks with distinct syntactic, mathematical, or structural footprints (intercepted in <15µs via L1 fast-path). | Single-Provider Monoliths: Workloads already locked into a single proprietary model endpoint with fixed enterprise flat-rate pricing. |
| Closed-World Transformations: Unit conversions, regex generation, JSON parsing, language translation (safely constrained to fast edge models). | Standalone L1 on Purely Affective Dialogue: Running in L1-only mode without the L2 neural classifier on unstructured social chat (which defaults to general_fast). |
| Unstructured Chat with Dual-Stage L2: Escalates ambiguous prompts to vector centroid cosine distance, eliminating blind fallback defaults. | Zero-Variance Workloads: Pipelines where all candidate models perform identically with no accuracy or cost divergence. |
| Runaway Loop & Degeneration Guard: Halting repetitive cyclical outputs mid-stream before blowing token limits. | Unconstrained Batch Budgets: Large offline processing where compute budget is unlimited and parallel throughput matters more than per-query optimization. |
| Cost-Sensitive OpenRouter Workflows: Dispatches to cheap specialized models first with automatic fallback to frontier models. | Dynamic Heavy Re-training Pipelines: If your workload strictly mandates continually retraining an end-to-end multi-layer neural network on black-box label files. |
| Dimension | Krusch Cascade Router (Dual-Stage L1/L2) | Embedding / Neural Routers (e.g. RouteLLM) | LLM-as-a-Router (e.g. Orca) |
|---|---|---|---|
| Dispatch Latency | < 15 µs (L1 Fast-Path) / ~20ms (L2 Neural) | 15 – 50 ms (Vectorization + MLP) | 400 – 1,200 ms (LLM pre-flight) |
| Routing Cost | $0.00 (L1 Fast-Path) / $0.00 local (L2) | ~$0.0001 (Embedding tokens) | ~$0.002 (Prompt tokens) |
| Structured Prompts (Code, Math, Syntax) | High Precision (>95%) | High (>90%) | High (>95%) |
| Messy / Ambiguous Chat | Robust (>92% via L2 Centroid Escalation) | Robust (Learns semantic nuances) | Very Robust |
| Mid-Stream Loop Guard | Yes (sliding n-gram abort) | No (Routing only) | No (Routing only) |
| Model Catalog Dependency | Fully decoupled (via customModels) | Requires retrained classifier | Prompt updates |
| Network Failure Cascade | Yes (Speculative dual-call & fallback) | No | No |
- 🚀 Sub-Millisecond Routing Overhead: Heuristic CPU classifier runs in microseconds without pre-flight network round-trips.
- 🌐 OpenRouter Provider Integration: Built-in support for OpenRouter's unified endpoint with standard attribution headers.
- 🎯 4-Model Specialist Architecture: Factory configuring models across code & chess (
Qwen3-Coder-Next), factual STEM & logic (deepseek-v4-flash), translation & clinical trivia (gemini-3.1-flash-lite), and deep financial proofs (deepseek-v4-pro). - 🧠 Knowledge Boundary Router: Classifies closed-world vs. open-world self-containment.
- ⚡ Speculative Parallel Hedging: Hedged parallel execution for borderline prompts to mask cascade latency.
- 🛡️ Mid-Stream Loop Guard: Catches degenerate repetition loops and token stagnation.
- 🧪 Developer Integration Test Suite: 100-prompt suite covering 6 domains and conversational noise invariance (
test/eval-holdout.test.js). - 📊 RouterArena Benchmark Evaluation: Scored 0.7883 in official GitHub Actions CI evaluation under RouteWorks PR #169, achieving 80.72% benchmark accuracy at $0.2117 / 1K queries with 90.48% robustness and 0 abnormal failures (submission pending merge).
- 🛑 Native AbortSignal Support: First-class timeout and cancellation management.
- 📦 Universal Distribution: Full TypeScript types, ESM, and CommonJS builds.
krusch-cascade-router has been evaluated across standard routing benchmark harnesses:
| Benchmark Suite | Sponsoring Organization / Publication | Benchmark Scope | Baseline Comparison | Krusch Cascade Router Evaluation | Primary Metric | Cost Reduction vs Frontier | Routing Overhead |
|---|---|---|---|---|---|---|---|
| 1. RouterArena | RouterArena Consortium (Rice Univ) | 8,400 Benchmark Queries (+3,236 Optimality + 420 Robustness) | Multi-Model Frontier Pool |
Official PR #169 CI Bot Eval: Workflow Score: 0.7883 (Pending Merge) Accuracy: 80.72% (Evaluation score vs Paix2 @ 77.63 & KT-ModelRouter @ 76.28) |
0.7883 (Evaluated in PR #169) |
$0.2117 / 1K queries (65% reduction vs Run 3, vs $0.27 Paix2, $4.10 NotDiamond) |
< 0.15 ms (6,600+ QPS) |
| 2. Integration Suite | Real-World Developer Prompts | 100 Diverse Queries across 6 Domains | Multi-Model Pool |
Routing Precision: 100.0% Noise Invariance: 100.0% (Classifier routing precision, not LLM output) |
100.0% Routing (Classification test suite) |
~75% Savings vs Frontier Oracle |
0.02 ms (50,000+ QPS) |
|
3. WithMartian RouterBench (Offline Simulation) |
WithMartian (arXiv: 2403.12031) | 36,497 Real Inference Outcomes across 11 LLMs | Single-Model GPT-4 Oracle ($94.39 Total Cost) |
AIQ Score: 0.7200 (92.1% of Ceiling) Frugal: 64.51% Acc @ $8.13 Balanced: 75.08% Acc @ $52.52 |
0.7200 AIQ Score (Offline Simulation) |
93.23% (Frugal) 56.29% (Balanced) |
0.11 ms (9,066 QPS) |
|
4. Google AutoMix (Offline Simulation) |
Google Research & CMU (NeurIPS 2024) | 14,571 Validation Queries across 5 QA/RC Datasets | Speculative Cascade LLaMA-13B |
CoQA Lift: +55.17% (vs +43.68% POMDP) NarrativeQA: +17.45% (vs +6.44% POMDP) |
+55.17% IBC Lift (Offline Simulation) |
82.40% on CoQA 68.39% on NarrativeQA |
0.007 ms (137,081 QPS) |
|
5. LMSYS RouteLLM (Offline Simulation) |
LMSYS Org / UC Berkeley (arXiv: 2406.18665) | 10,000+ Battles across GSM8K, MT-Bench, MMLU | GPT-4 vs Mixtral / LLaMA-3 |
MT-Bench: 0.6027 APGR GSM8K: 0.5602 APGR MMLU: 0.5060 APGR |
0.6027 APGR (Offline Simulation) |
50%–75% Savings at 95% Quality Retention |
< 0.05 ms (20,000+ QPS) |
🔍 Full Technical Documentation & Methodology: Detailed per-benchmark curves, domain breakdowns, and derivations are available in
docs/BENCHMARK.md.💡 Methodology & Context Note on Benchmark Results: The metrics reported in this evaluation matrix reflect official RouterArena CI evaluation and offline simulation runs evaluating our modern specialist pool (
deepseek-v4-flash,Qwen3-Coder-Next,gemini-3.1-flash-lite,deepseek-v4-pro) against standard public benchmark datasets and task queries.Important Reproducibility Context:
- RouterArena PR #169 Candidate Evaluation: In automated CI evaluation on RouteWorks/RouterArena PR #169 (candidate submission pending upstream merge), Krusch Cascade Router scored 0.7883, with 80.72% accuracy and $0.2117 / 1K queries (total cost $1.7785 across all 8,400 queries; 0 abnormal; 90.48% robustness).
- Reconstructed Simulation Methodology (Suites 3–5): RouterBench, AutoMix, and RouteLLM figures represent reconstructed offline simulations evaluating our specialist models on those public benchmark datasets against historical baseline oracles (e.g. GPT-4 vs LLaMA-13B from 2023/2024 literature). They are local simulations, NOT independent official leaderboard submissions to those platforms.
- Classifier Accuracy vs Generation Quality: The 100% precision figure reported in the Developer Integration Suite measures prompt domain routing classification (ensuring code/math/trivia queries land on the correct model bucket), NOT generative correctness of the LLM responses.
🧪 Audit Reproduction: Run test suite:
npm test
The public RouteWorks/RouterArena leaderboard ranks router implementations as follows:
| Rank | Router Implementation | Acc-Cost Score | Accuracy | Cost / 1K Queries | Robustness | Status |
|---|---|---|---|---|---|---|
| — | Krusch Cascade Router | 78.83 (0.7883) | 80.72% | $0.2117 | 90.48% | PR #169 Candidate (Pending Merge) |
| 1 | Paix2 | 77.63 | 79.69% | $0.2700 | 77.86% | Published |
| 2 | KT-ModelRouter | 76.28 | 78.14% | $0.2700 | 80.48% | Published |
| 4 | Sqwish Router | 76.21 | 79.76% | $0.7000 | 51.67% | Published |
| 5 | Divyam | 75.85 | 78.59% | $0.4800 | 98.33% | Published |
| 6 | Cross-Router | 75.75 | 76.98% | $0.1800 | 88.57% | Published |
| 7 | Reasonometry | 75.53 | 78.67% | $0.5700 | 92.14% | Published |
| 8 | vLLM-SR | 74.86 | 77.18% | $0.4200 | 67.62% | Published |
| 9 | nadir-caliper | 74.55 | 75.84% | $0.2200 | 79.76% | Published |
| 10 | Azure-Model-Router (Microsoft) | 70.42 | 72.94% | $0.7300 | 71.43% | Published |
| 11 | RouterBench-MLP (Martian) | 57.56 | 61.62% | $4.8300 | 80.00% | Published |
| 12 | NotDiamond (Commercial) | 57.29 | 60.83% | $4.1000 | 55.91% | Published |
| 13 | RouteLLM (UC Berkeley) | 48.07 | 47.04% | $0.2700 | 100.00% | Published |
Our submission (RouteWorks/RouterArena PR #169) was evaluated directly by RouteWorks GitHub Actions CI workflows across the full 8,400-query benchmark dataset plus 420 robustness perturbations:
| Evaluation Run | Acc-Cost Score | Accuracy | Cost / 1K | Robustness | Evaluation Notes |
|---|---|---|---|---|---|
| Run 1: Initial Full Eval | 74.13 | 76.14% | $0.3700 | 93.10% | Baseline multi-model adapter |
| Run 2: Cheaper 5-Model Pool | 74.09 | 75.62% | $0.2700 | 94.05% | Shifted budget to cheaper flash endpoints |
| Run 3: Heuristic Retune | 77.93 | 81.53% | $0.6070 | 92.62% | Disambiguated math operators & chess boundaries |
| Run 4: Literature Refinement | 78.83 (0.7883) | 80.72% | $0.2117 | 90.48% | PR #169 Candidate: Clean 4-model specialist pool, stripped harness fingerprints, 65.1% cost drop |
- Official CI Bot Score: 0.7883 (78.83) (Accuracy: 80.72%, Cost: $0.2117 / 1K, Robustness: 90.48%, Abnormal: 0).
- Status: Candidate evaluation completed under RouteWorks PR #169 (awaiting merge).
- Maintainer Conformance: Addressed upstream review by purging all test harness artifacts, perturbation keywords, and benchmark token fingerprints. Specialists are mapped cleanly to external model capability literature.
-
Optimality Metrics: In the official candidate evaluation (Run 4),
krusch-cascade-routerachieved an Opt.Acc of 0.9356 (capturing 93.56% of theoretical oracle accuracy ceiling), an Opt.Cost of 0.3410, and an Opt.Sel of 0.1617 ($0.2117 / 1K queries vs $4.83 / 1K for RouterBench-MLP). Routing deterministically by domain specialization trades per-instance cost micro-optimization for microsecond CPU latency (<15µs) and zero token overhead.
- 0.7200 AIQ Score: Captures 92.1% of the theoretical upper-bound ceiling (0.7818), comparing favorably to Martian's reference RouterBench-MLP (0.6830) and RouterBench-KNN (0.6558).
- Frugal Mode: 64.51% Accuracy at $8.13 Total Cost ($0.22/1k) — a 93.23% cost reduction vs GPT-4 ($94.39).
- GSM-8K Math: Delivers 62.70% accuracy at $4.34 vs GPT-4's $63.68 ($59.34 direct savings, a 93.18% reduction).
- MBPP Code: In Balanced Mode, matches GPT-4 quality within 0.24% (68.38% vs 68.62%) while slashing cost by 78.5%.
- CoQA Benchmark: +55.17% IBC Lift vs AutoMix POMDP (+43.68%) with 82.40% cost reduction vs LLaMA-70B.
- NarrativeQA Benchmark: +17.45% IBC Lift vs AutoMix POMDP (+6.44%).
- Zero Verification Overhead: Heuristic classification avoids spending tokens on self-verification calls.
- Speed: Heuristic classification runs in microseconds on CPU vs multiple seconds for LLM-based verifiers.
graph TD;
A[Incoming Prompt] --> L1{Stage 1: L1 Pre-Router<br/>krusch-cascade-router<br/>< 15µs CPU | $0.00};
%% Fast path branch
L1 -- "High-Confidence Deterministic Syntax<br/>(isFastPath: true)" --> FP[L1 Fast-Path Specialist Dispatch];
FP -- Code, SQL, Rust, React, Chess --> C1[Qwen3-Coder-Next];
FP -- STEM, Factual Science, Math, Logic --> C2[deepseek-v4-flash];
FP -- Translation, Geography, Medicine, Trivia --> C4[gemini-3.1-flash-lite];
FP -- Financial Statements, Formal Proofs --> C5[deepseek-v4-pro];
%% Reactive abort fallback
C1 -. Error / Logprob Abort .-> C5;
C2 -. Error / Logprob Abort .-> C5;
C4 -. Error / Logprob Abort .-> C5;
%% L2 fallback branch
L1 -- "Ambiguous / Unstructured Chat<br/>(suggestedAction: delegate_to_l2)" --> L2[Stage 2: L2 Semantic Layer<br/>RouteLLM / NotDiamond / Frontier Model];
krusch-cascade-router draws on proven systems concepts and dynamic inference literature:
-
Sequential Model Cascading & Fallbacks (FrugalGPT; Chen et al., 2023, arXiv:2305.05176):
- Establishes the sequential cascade principle: querying smaller/cheaper models first and escalating to frontier models only upon low confidence or failure.
-
Speculative Parallel Hedging (The Tail at Scale; Dean & Barroso, Communications of the ACM, 2013):
- Rather than waiting sequentially for borderline queries, issuing hedged requests across models masks cascade latency and caps 99th-percentile response times.
-
Degenerative Token Loops & Repetition (The Curious Case of Neural Text Degeneration; Holtzman et al., ICLR 2020):
- Autoregressive generation is prone to degenerate repetitive cycles. Monitoring sliding-window
$n$ -gram repetition allows aborting runaway loops mid-stream before consuming full output tokens.
- Autoregressive generation is prone to degenerate repetitive cycles. Monitoring sliding-window
-
Zero-Overhead vs. Learned Routing (RouterBench; Hu et al., 2024, arXiv:2403.12031 & RouteLLM; Ong et al., 2024, arXiv:2406.18665):
- Multi-LLM routing benchmarks show that while learned classifiers or LLM routers achieve high accuracy, they introduce 15–50ms embedding overhead or 500ms+ LLM latency. Fast heuristic gating provides sub-millisecond dispatch for distinct syntactic and domain signatures.
From npm:
npm install krusch-cascade-routerOr directly from GitHub:
npm install github:kruschdev/krusch-cascade-routerOr from local checkout:
npm install ../path/to/krusch-cascade-routerRequirement: Node.js 18+ (utilizes native
fetchandAbortSignal).
Instantiate a complete multi-specialist router using 4 specialized domain models routed directly through OpenRouter:
import { createMultiSpecialistRouter } from 'krusch-cascade-router';
// 1. Initialize with your OpenRouter API key (preset defaults or custom overrides)
const router = createMultiSpecialistRouter({
openrouterApiKey: process.env.OPENROUTER_API_KEY, // Defaults to process.env.OPENROUTER_API_KEY
openrouterReferer: 'https://my-app.com', // Optional attribution header
openrouterTitle: 'My App',
// Optional: override any specialist model to prevent catalog rot or route to preferred endpoints
// customModels: { code: 'qwen/qwen-2.5-coder-32b-instruct', reasoning_deep: 'deepseek/deepseek-r1' }
});
// 2. Dispatch queries - automatically routed to optimal domain specialist:
// - Code, SQL & Algorithms -> Qwen/Qwen3-Coder-Next
// - Chess & Spatial Games -> Qwen/Qwen3-Coder-Next
// - STEM, Logic & Factual Science -> deepseek/deepseek-v4-flash
// - Complex Proofs & Financial Statements -> deepseek/deepseek-v4-pro
// - General Fast, Medical & Translation -> google/gemini-3.1-flash-lite
const res = await router.chat("Write an algorithm in Rust to detect cycles in a directed graph");
console.log(`Routed to: ${res.routedTo}`); // 'code' (Qwen/Qwen3-Coder-Next)
console.log(res.text);If your architecture already uses an L2 neural router (e.g. RouteLLM, NotDiamond) or a frontier model, use krusch-cascade-router as an in-memory L1 pre-filter. It intercepts 70–80% of structured agent traffic in CPU microseconds without paying the latency or token tax of a neural classifier:
import { classifyPreRoute } from 'krusch-cascade-router';
async function dispatchAgentPrompt(prompt) {
// 1. L1 Pre-Check in <15 microseconds ($0.00 cost, 0 tokens)
const preRoute = classifyPreRoute(prompt);
if (preRoute.isFastPath) {
console.log(`⚡ L1 Fast-Path Hit -> Dispatching to specialist: ${preRoute.role}`);
// Bypass expensive routers and call the dedicated specialist directly:
return callSpecialistModel(preRoute.role, prompt);
}
// 2. L1 Miss: Prompt is unstructured / ambiguous conversational chat
console.log(`🔍 L1 Miss -> Delegating to L2 Neural Router or Frontier Model`);
return callSecondaryNeuralRouter(prompt); // e.g. RouteLLM, NotDiamond, or Claude 3.7
}When an incoming prompt misses the Stage-0/L1 syntactic gate (isFastPath: false), CascadeRouter can query an L2 Neural Semantic Router (such as krusch-context-mcp or RouteLLM) to dynamically resolve the optimal domain specialist model rather than falling back to a generic default:
import {
CascadeRouter,
createCentroidSemanticRouter,
createContextMcpRouter
} from 'krusch-cascade-router';
// Approach 1: In-process L2 Neural Router via local Ollama (bge-large, 1024d)
const l2OllamaRouter = createCentroidSemanticRouter({
ollamaUrl: 'http://localhost:11434',
embedModel: 'bge-large'
});
// Approach 2: Direct adapter to krusch-context-mcp server tool
// const l2McpRouter = createContextMcpRouter((name, args) => mcpClient.callTool({ name, arguments: args }));
const router = new CascadeRouter({
fastModel: { model: 'gemini-3.1-flash-lite' },
heavyModel: { model: 'deepseek-v4-pro' },
specialistModels: {
code: { model: 'Qwen/Qwen3-Coder-Next' },
reasoning_deep: { model: 'deepseek/deepseek-v4-pro' },
factual_stem: { model: 'deepseek/deepseek-v4-flash' }
},
// 🧠 L2 Neural Semantic Router: queried only on L1 pre-router misses
l2Router: l2OllamaRouter
});
// 1. Structured syntax -> Bypasses L2 entirely (<15µs L1 hit on CPU for $0.00)
await router.chat("```python\ndef fib(n): pass\n```");
// 2. Unstructured/ambiguous query -> Evaluated by L2 in ~20ms, routed to optimal specialist
await router.chat("Can you explain why the event loop hangs during concurrent queue drain?");Pair a local edge model (Ollama, vLLM) with a heavy cloud model fallback:
import { CascadeRouter } from 'krusch-cascade-router';
const router = new CascadeRouter({
fastModel: {
url: 'http://localhost:11434/v1/chat/completions',
model: 'qwen2.5:3b' // Local edge model
},
heavyModel: {
apiKey: process.env.GEMINI_API_KEY,
model: 'gemini-2.5-flash',
provider: 'gemini'
},
cascadeThreshold: 0.85, // Fallback if initial token probability < 85%
tokensToEvaluate: 5, // Tokens to inspect before committing
speculativeBranching: true // Pre-warm heavy model on borderline prompts
});
const response = await router.chat("Explain the architecture of distributed raft consensus...");
console.log(`Routed to: ${response.routedTo}`); // 'fast' | 'heavy'
console.log(response.text);The 4 default models (deepseek-v4-flash, Qwen3-Coder-Next, gemini-3.1-flash-lite, deepseek-v4-pro) are an empirical starter preset, not a hardcoded lock-in. As OpenRouter models evolve, you can easily swap models, add new specialists (such as comprehension_rc), update token pricing, or inject custom domain regexes:
import { createMultiSpecialistRouter } from 'krusch-cascade-router';
const router = createMultiSpecialistRouter({
openrouterApiKey: process.env.OPENROUTER_API_KEY,
// 1. Swap or upgrade specialist models (strings or full ModelConfig objects)
customModels: {
code: 'anthropic/claude-3.7-sonnet',
reasoning_deep: 'openai/o3-mini',
factual_stem: {
model: 'meta-llama/llama-3.3-70b-instruct',
provider: 'openrouter',
costPerMillionInputTokens: 0.12,
costPerMillionOutputTokens: 0.30
}
},
// 2. Inject custom domain regex rules evaluated before default heuristics
classifier: {
customSpecialistRules: [
{ role: 'reasoning_deep', pattern: /\b(?:legal compliance|gdpr audit|sec filing)\b/i },
{ role: 'code', pattern: /\b(?:terraform plan|ansible playbook|helm chart)\b/i }
]
},
// 3. Optional local edge model for low-priority / background batch jobs
backgroundModel: {
url: 'http://localhost:11434/v1/chat/completions',
model: 'qwen2.5:3b',
costPerMillionInputTokens: 0,
costPerMillionOutputTokens: 0
}
});Evaluate prompts with detailed metadata on whether to bypass or delegate to L2:
import { classifyPreRoute } from 'krusch-cascade-router';
const res = classifyPreRoute("Write a SQL query to calculate user churn");
console.log(res);
// {
// isFastPath: true,
// role: 'code',
// confidence: 'high',
// complexityScore: 0.20,
// suggestedAction: 'dispatch_specialist'
// }
const chat = classifyPreRoute("How are you feeling today?");
console.log(chat);
// {
// isFastPath: false,
// role: 'factual_stem',
// confidence: 'unstructured',
// complexityScore: 0.05,
// suggestedAction: 'delegate_to_l2'
// }Classify incoming queries into domain roles deterministically in under 15 microseconds:
import { classifySpecialistRole } from 'krusch-cascade-router';
classifySpecialistRole("def quicksort(arr): ..."); // 'code'
classifySpecialistRole("Options: \nA. Alpha\nB. Beta"); // 'factual_stem'
classifySpecialistRole("Evaluate FEN: rnbqkbnr/pppppppp/..."); // 'games_spatial'
classifySpecialistRole("Prove that every planar graph is 4-colorable"); // 'reasoning_deep'Closed-world tasks (e.g. arithmetic, code formatting, unit conversion, translation) are actively degraded by large model context pollution. You can invoke the boundary classifier directly:
import { detectKnowledgeBoundary } from 'krusch-cascade-router';
detectKnowledgeBoundary("Calculate 42 * 18 / 3"); // 'closed'
detectKnowledgeBoundary("Translate this sentence to French: Good morning"); // 'closed'
detectKnowledgeBoundary("Analyze the ethical dilemmas in autonomous driving"); // 'open'import { evaluateComplexityScore } from 'krusch-cascade-router';
const score = evaluateComplexityScore("Compare Postgres vs SQLite tradeoffs for edge devices");
// Returns continuous float in [0.0, 1.0] (e.g., 0.42 -> triggers speculative branching)const controller = new AbortController();
setTimeout(() => controller.abort(), 8000); // 8-second SLA
try {
const response = await router.chat("Analyze telemetry logs", undefined, {
signal: controller.signal
});
} catch (err) {
if (err.name === 'AbortError') {
console.log('Request SLA exceeded.');
}
}const router = new CascadeRouter({
// ...config
onEvent: (event, meta) => {
// Events: 'route_specialist' | 'route_l2_semantic' | 'route_fast' | 'route_heavy' | 'cascade_triggered' |
// 'speculative_branch_hedged' | 'repetition_loop_triggered' | 'entropy_collapse_triggered'
console.log(`[Router Telemetry] ${event}`, meta);
}
});Factory function configuring the domain specialist models, routing through OpenRouter.
| Option | Type | Default | Description |
|---|---|---|---|
openrouterApiKey |
string |
process.env.OPENROUTER_API_KEY |
API key for OpenRouter. |
customModels |
Partial<Record<SpecialistRole, string | ModelConfig>> |
undefined |
Custom model ID strings or full ModelConfig objects overriding default specialists. |
classifier |
ClassifierOptions |
undefined |
Custom options, including customSpecialistRules and customRules. |
l2Router |
SemanticRouterL2 |
undefined |
Optional L2 Neural Semantic Router invoked dynamically when L1 fast-path misses. |
backgroundModel |
ModelConfig |
undefined |
Optional model for low-priority/background batch jobs. |
openrouterReferer |
string |
undefined |
Optional HTTP-Referer header for rankings. |
openrouterTitle |
string |
undefined |
Optional X-Title header for rankings. |
cascadeThreshold |
number |
0.85 |
Logprob confidence threshold for cascading. |
tokensToEvaluate |
number |
5 |
Tokens to buffer and evaluate for initial confidence. |
maxRepetitiveTokens |
number |
4 |
Threshold for degenerate repetition and cyclic loop detection. |
speculativeBranching |
boolean |
false |
Enable Second Thought speculative hedging. |
prunePreRouting |
boolean |
false |
Strips conversational filler and whitespace before length evaluation. |
onEvent |
Function |
undefined |
Telemetry callback for observability. |
| Property | Type | Default | Description |
|---|---|---|---|
fastModel |
ModelConfig |
Required | Fast edge model config (e.g., local Ollama, vLLM, gemini-3.1-flash-lite). |
heavyModel |
ModelConfig |
Required | Heavy cloud fallback config (e.g., deepseek-v4-pro, GPT-4o). |
specialistModels |
Partial<Record<SpecialistRole, ModelConfig>> |
undefined |
Map of domain specialist models. |
l2Router |
SemanticRouterL2 |
undefined |
Optional L2 Neural Semantic Router hook ((prompt, context) => Promise<SemanticRouteResult>). |
openrouterApiKey |
string |
undefined |
Global OpenRouter API key for specialist models. |
openrouterReferer |
string |
undefined |
Global HTTP-Referer header for OpenRouter calls. |
openrouterTitle |
string |
undefined |
Global X-Title header for OpenRouter calls. |
backgroundModel |
ModelConfig |
undefined |
Optional model for low-priority/background batch jobs. |
cascadeThreshold |
number |
0.85 |
Confidence cutoff probability (0.0 to 1.0). |
tokensToEvaluate |
number |
5 |
Tokens to buffer and evaluate for initial confidence. |
maxRepetitiveTokens |
number |
4 |
Threshold for degenerate repetition and cyclic loop detection. |
speculativeBranching |
boolean |
false |
Enables Second Thought parallel hedging for borderline prompts. |
prunePreRouting |
boolean |
false |
Strips conversational filler and whitespace before length evaluation. |
classifier |
ClassifierOptions |
undefined |
Custom options, customSpecialistRules, and customRules regexes. |
onEvent |
Function |
undefined |
Telemetry callback for observability. |
MIT License © 2026 kruschdev
