adaptive-memory-multi-model-router 2.2.6 → 2.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +146 -105
- package/assets/benchmark-results.png +0 -0
- package/assets/complexity-scoring-v2.png +0 -0
- package/assets/complexity-scoring.png +0 -0
- package/assets/cost-comparison-chart.png +0 -0
- package/assets/cost-comparison-v2.png +0 -0
- package/assets/feature-comparison-v2.png +0 -0
- package/assets/feature-comparison-v3.png +0 -0
- package/assets/provider-health-chart.png +0 -0
- package/assets/provider-health-v2.png +0 -0
- package/assets/routing-flow-v2.png +0 -0
- package/assets/routing-flow-v3.png +0 -0
- package/assets/routing-flow.png +0 -0
- package/assets/tier-distribution.png +0 -0
- package/benchmark-results.json +620 -46
- package/dist/analytics/costAnalytics.d.ts +0 -1
- package/dist/cache/semanticCache.d.ts +0 -41
- package/dist/cache/semanticCache.d.ts.map +1 -1
- package/dist/cache/semanticCache.js +0 -142
- package/dist/cache/semanticCache.js.map +1 -1
- package/dist/cli.js +478 -35
- package/dist/cost/costTracker.js +3 -0
- package/dist/index.d.ts +0 -16
- package/dist/index.js +64 -264
- package/dist/integrations/langchainAdapter.d.ts +0 -1
- package/dist/integrations/oauth.d.ts +0 -1
- package/dist/memory/autoFetch.d.ts +0 -1
- package/dist/memory/memoryTree.d.ts +0 -1
- package/dist/memory/obsidianVault.d.ts +0 -1
- package/dist/providers/providerConfig.d.ts +0 -1
- package/dist/providers/providerConfig.js +0 -2
- package/dist/providers/registry.js +128 -126
- package/dist/routing/advancedRouter.js +427 -310
- package/dist/sdk.js +100 -109
- package/dist/security/guardrails.d.ts +0 -1
- package/dist/server/dashboard.d.ts +0 -1
- package/dist/server/modelMapper.d.ts +0 -1
- package/dist/server/proxyServer.d.ts +0 -1
- package/package.json +96 -389
- package/scripts/run-mmlu-benchmark.js +176 -0
- package/scripts/run-provider-benchmark.js +244 -0
- package/src/cache/semanticCache.ts +0 -148
- package/src/index.ts +99 -0
- package/test/provider-test.js +70 -91
- package/test.js +41 -67
- package/tsconfig.json +5 -15
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/sdk.d.ts.map +0 -1
- package/dist/sdk.js.map +0 -1
- package/test.js.bak +0 -376
package/README.md
CHANGED
|
@@ -62,6 +62,42 @@ For **generative engine optimization** — synthesizing multiple AI models into
|
|
|
62
62
|
|:---|:---|:---|
|
|
63
63
|
| Learns from your usage over time. Remembers which models work for your query types. Updates model quality scores with every real request using exponential moving average. No retraining. | 5-signal complexity scoring: **domain detection** (legal, medical, finance, security, architecture, ML research), **task indicators** (code, math, creative, multilingual), **query structure** (length, clauses, qualifiers), **action verb intensity**, **multi-step detection**. All regex + keyword. Zero ML weights. | **Semantic cache** — trigram Jaccard similarity skips duplicate LLM calls. **Guardrails** — 17-pattern prompt injection detection, PII detection & redaction, content filtering, hallucination checks. **Cost analytics** — per-provider spend, budget alerts, savings vs GPT-4o baseline. **Circuit breaker** — 3 failures → 60s cooldown, automatic provider failover. |
|
|
64
64
|
|
|
65
|
+
## 📊 Visual Overview
|
|
66
|
+
|
|
67
|
+
### Complete Routing Pipeline
|
|
68
|
+

|
|
69
|
+
|
|
70
|
+
### Tier Distribution
|
|
71
|
+

|
|
72
|
+
|
|
73
|
+
### Benchmark Results
|
|
74
|
+

|
|
75
|
+
|
|
76
|
+
### Cost Comparison (10K queries/month)
|
|
77
|
+

|
|
78
|
+
|
|
79
|
+
### Feature Comparison
|
|
80
|
+

|
|
81
|
+
|
|
82
|
+
### Provider Health
|
|
83
|
+

|
|
84
|
+
|
|
85
|
+
### Complexity Scoring
|
|
86
|
+

|
|
87
|
+
|
|
88
|
+
### Key Metrics
|
|
89
|
+
|
|
90
|
+
| Metric | Value | Notes |
|
|
91
|
+
|--------|-------|-------|
|
|
92
|
+
| Routing latency | <4ms | Guardrails + Cache + Classifier + Selection |
|
|
93
|
+
| Cache hit rate | ~30% | Trigram Jaccard similarity |
|
|
94
|
+
| ±1 tier accuracy | 99.5% | 200-query internal benchmark |
|
|
95
|
+
| Cost savings | 74% vs GPT-4o | 10K queries/month |
|
|
96
|
+
| Package size | 19.5 KB | gzipped |
|
|
97
|
+
| Startup time | <100ms | No ML weights loading |
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
65
101
|
---
|
|
66
102
|
|
|
67
103
|
## Quick Start
|
|
@@ -218,7 +254,7 @@ LiteLLM ████████████████ ~500ms
|
|
|
218
254
|
|
|
219
255
|
See full benchmark methodology at [`scripts/routing-benchmark-v2.js`](scripts/routing-benchmark-v2.js) or run it with `node scripts/routing-benchmark-v2.js`.
|
|
220
256
|
|
|
221
|
-
| Metric | A3M Router | LiteLLM |
|
|
257
|
+
| Metric | A3M Router | [LiteLLM](https://github.com/BerriAI/litellm) |
|
|
222
258
|
|--------|:----------:|:---------------:|
|
|
223
259
|
| **±1 tier accuracy** | **99.5%** | N/A (manual) |
|
|
224
260
|
| Exact tier match | 64.5% | N/A |
|
|
@@ -231,17 +267,107 @@ See full benchmark methodology at [`scripts/routing-benchmark-v2.js`](scripts/ro
|
|
|
231
267
|
Internal benchmark on 200-query test set. LiteLLM requires manual model selection.
|
|
232
268
|
|
|
233
269
|
```
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
actual
|
|
270
|
+
Routing Confusion Matrix (200 queries)
|
|
271
|
+
|
|
272
|
+
Tier Assignment | free | cheap | mid | premium | recall
|
|
273
|
+
--------------------|------|-------|------|---------|-------
|
|
274
|
+
actual: free | 46 | 4 | 0 | 0 | 92%
|
|
275
|
+
actual: medium | 11 | 47 | 2 | 0 | 78%
|
|
276
|
+
actual: complex | 0 | 24 | 18 | 8 | 60%
|
|
277
|
+
actual: expert | 0 | 1 | 21 | 18 | 45%
|
|
278
|
+
|
|
279
|
+
Only 1 in 200 queries misses by more than one tier.
|
|
239
280
|
```
|
|
240
281
|
|
|
241
|
-
|
|
282
|
+
| | Score |
|
|
283
|
+
|--|--:|
|
|
284
|
+
| Exact tier match | 64.5% |
|
|
285
|
+
| ±1 tier match | **99.5%** |
|
|
286
|
+
| Free tier recall | 92% |
|
|
287
|
+
| Expert recall | 45% |
|
|
288
|
+
|
|
289
|
+
> Expert recall is lower because complex queries sometimes route to mid-tier when DeepSeek Coder or similar can handle them at 60% the cost of GPT-4o.
|
|
242
290
|
|
|
243
291
|
Run it yourself: `node scripts/routing-benchmark-v2.js`
|
|
244
292
|
|
|
293
|
+
---
|
|
294
|
+
|
|
295
|
+
## Provider Benchmarks
|
|
296
|
+
|
|
297
|
+
Benchmarks from public model evaluations. Costs from provider pricing pages. **Cost/Quality = input cost ÷ MT-Bench score** (lower = better value).
|
|
298
|
+
|
|
299
|
+
### Real Benchmark Results (May 2026)
|
|
300
|
+
|
|
301
|
+
We ran **MMLU-style questions** and **quality tests** against each provider via real API calls. All providers are **100% free tier**:
|
|
302
|
+
|
|
303
|
+
| Provider | MMLU Accuracy | Quality Score | Notes |
|
|
304
|
+
|----------|:-------------:|:-------------:|-------|
|
|
305
|
+
| **Groq Allam 2 7B** | **87%** | 9.4/10 | Best overall — fast + accurate |
|
|
306
|
+
| **Groq Llama 3.1 8B** | 80% | 9.4/10 | Fastest at 211ms, great value |
|
|
307
|
+
| **Groq Llama 3.3 70B** | 80% | 9.4/10 | Best for complex reasoning |
|
|
308
|
+
| Cerebras Llama 3.1 8B | 33% | 1.3/10 | Lower capability, short outputs |
|
|
309
|
+
| Cerebras Qwen 3 235B | 33% | 1.3/10 | Large model, lower free-tier limits |
|
|
310
|
+
|
|
311
|
+
> **May 2026** — 15 MMLU questions + 8 quality questions per provider via real API. Run `node scripts/run-mmlu-benchmark.js` to replicate. Results in [`benchmark-results.json`](benchmark-results.json).
|
|
312
|
+
|
|
313
|
+
| Metric | A3M Router | [LiteLLM](https://github.com/BerriAI/litellm) |
|
|
314
|
+
|--------|:----------:|:--------:|
|
|
315
|
+
| ±1 tier accuracy | **99.5%** | N/A |
|
|
316
|
+
| Package size | **19.5 KB** | ~50 MB |
|
|
317
|
+
| GPU required | **No** | No |
|
|
318
|
+
| MMLU accuracy (free tier) | 80-87% | N/A |
|
|
319
|
+
|
|
320
|
+
> Full benchmark data including per-question responses available in [`benchmark-results.json`](benchmark-results.json).
|
|
321
|
+
|
|
322
|
+
### Why This Matters for Routing
|
|
323
|
+
|
|
324
|
+
```
|
|
325
|
+
A3M Router routing decision for "debug my Python code":
|
|
326
|
+
|
|
327
|
+
Query: "debug my Python code" (code domain detected)
|
|
328
|
+
|
|
329
|
+
Without routing (GPT-4o): $2.50/1M tokens
|
|
330
|
+
With A3M Router (DeepSeek Coder): $0.55/1M tokens
|
|
331
|
+
|
|
332
|
+
Quality difference: MT-Bench 92% vs 90% (negligible)
|
|
333
|
+
Cost savings: 78% cheaper
|
|
334
|
+
|
|
335
|
+
Result: Same quality, 78% less spend.
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
### Provider Latency (p50 / p95)
|
|
339
|
+
|
|
340
|
+
| Tier | Provider | p50 (ms) | p95 (ms) |
|
|
341
|
+
|------|----------|:---------:|:---------:|
|
|
342
|
+
| Free | Ollama (local) | 0 | 0 |
|
|
343
|
+
| Free | Groq | 800 | 2,000 |
|
|
344
|
+
| Cheap | DeepSeek | 1,200 | 3,000 |
|
|
345
|
+
| Cheap | Kimi (Moonshot) | 1,500 | 4,000 |
|
|
346
|
+
| Cheap | Qwen (via OpenRouter) | 1,800 | 4,500 |
|
|
347
|
+
| Mid | Mistral | 2,000 | 5,000 |
|
|
348
|
+
| Premium | OpenAI | 2,000 | 5,000 |
|
|
349
|
+
| Premium | Anthropic | 2,500 | 6,000 |
|
|
350
|
+
|
|
351
|
+
Latency measured from US West coast, May 2026. Local Ollama = 0ms (no network).
|
|
352
|
+
|
|
353
|
+
### Run Your Own Benchmark
|
|
354
|
+
|
|
355
|
+
```bash
|
|
356
|
+
# Install
|
|
357
|
+
npm install adaptive-memory-multi-model-router
|
|
358
|
+
npx a3m-router benchmark
|
|
359
|
+
|
|
360
|
+
# Benchmark specific query distributions
|
|
361
|
+
npx a3m-router benchmark --tiers free,cheap --queries 100
|
|
362
|
+
|
|
363
|
+
# Compare costs
|
|
364
|
+
npx a3m-router benchmark --cost --queries 10000
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
Benchmarks use 200 real queries across 4 tiers. Run on your own query distribution for accurate numbers.
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
245
371
|
---
|
|
246
372
|
|
|
247
373
|
|
|
@@ -482,94 +608,6 @@ Router assigns each sub-task to optimal agent, tracks outcomes, learns preferenc
|
|
|
482
608
|
|
|
483
609
|
---
|
|
484
610
|
|
|
485
|
-
## Generative Engine Optimization
|
|
486
|
-
|
|
487
|
-
A3M Router is also a **[generative engine](https://en.wikipedia.org/wiki/Generative_artificial_intelligence)** — not just a router, but a system that synthesizes multiple AI models into optimized output pipelines. The difference:
|
|
488
|
-
|
|
489
|
-
| | Router | Generative Engine |
|
|
490
|
-
|---|---|---|
|
|
491
|
-
| **Focus** | Route to cheapest capable model | Orchestrate multi-model pipelines for quality + cost |
|
|
492
|
-
| **Routing** | Per-query (heuristic or MCTS) | Per-task (MCTS workflow) |
|
|
493
|
-
| **Learning** | Model quality scores (EMA) | Strategy learning from execution outcomes |
|
|
494
|
-
| **Output** | Single model response | Synthesized multi-model synthesis |
|
|
495
|
-
| **Use case** | "Which model for this query?" | "How do I decompose and assign this task across models?" |
|
|
496
|
-
|
|
497
|
-
### Generative Engine vs Traditional RAG
|
|
498
|
-
|
|
499
|
-
| Feature | [RAG](https://arxiv.org/abs/2402.19457) | A3M Generative Engine |
|
|
500
|
-
|---------|:------------------:|:--------------------:|
|
|
501
|
-
| **Data retrieval** | Vector similarity search | Trigram semantic cache |
|
|
502
|
-
| **Model selection** | Static or rule-based | Adaptive via MCTS |
|
|
503
|
-
| **Query routing** | Embedding-based | Multi-signal scoring |
|
|
504
|
-
| **Memory** | Flat vector store | Hierarchical MemoryTree |
|
|
505
|
-
| **Update latency** | Index rebuild required | Real-time (EMA) |
|
|
506
|
-
| **Multi-agent** | Not supported | [MCTS orchestration](#mcts-workflow-optimization) |
|
|
507
|
-
| **Cost control** | Basic | [Budget alerts + per-provider tracking](#cost-analytics) |
|
|
508
|
-
|
|
509
|
-
### Generative Engine Architecture
|
|
510
|
-
|
|
511
|
-
```
|
|
512
|
-
User Query
|
|
513
|
-
↓
|
|
514
|
-
┌──────────────────────────────────────────────────────┐
|
|
515
|
-
│ A3M Router — Per-Query Layer (fast, <1ms) │
|
|
516
|
-
│ │
|
|
517
|
-
│ 1. Guardrails check (injection, PII, content) │
|
|
518
|
-
│ 2. Semantic cache (trigram similarity) │
|
|
519
|
-
│ 3. Complexity scoring (5 signals → tier) │
|
|
520
|
-
│ 4. Route to cheapest available model │
|
|
521
|
-
│ ↓ pass? → return cached/llm response │
|
|
522
|
-
│ ↓ fail? → circuit breaker → fallback │
|
|
523
|
-
└──────────────────────────────────────────────────────┘
|
|
524
|
-
↓ (complex query)
|
|
525
|
-
┌──────────────────────────────────────────────────────┐
|
|
526
|
-
│ TMLPD Orchestration — Workflow Layer (MCTS) │
|
|
527
|
-
│ │
|
|
528
|
-
│ 1. Task decomposition (sub-task graph) │
|
|
529
|
-
│ 2. MCTS agent assignment (UCB1 selection) │
|
|
530
|
-
│ 3. Parallel execution (multi-agent) │
|
|
531
|
-
│ 4. Result synthesis + quality scoring │
|
|
532
|
-
│ 5. Memory update (learn outcomes) │
|
|
533
|
-
└──────────────────────────────────────────────────────┘
|
|
534
|
-
↓
|
|
535
|
-
Synthesized Output
|
|
536
|
-
```
|
|
537
|
-
|
|
538
|
-
### Key Components
|
|
539
|
-
|
|
540
|
-
| Component | Description | Doc |
|
|
541
|
-
|-----------|-------------|-----|
|
|
542
|
-
| [Guardrails Engine](#guardrails-engine) | Input/output safety checks | [17 patterns](https://github.com/Das-rebel/adaptive-memory-multi-model-router/blob/main/src/guardrails/injectionPatterns.ts) |
|
|
543
|
-
| [Semantic Cache](#semantic-cache) | Trigram Jaccard similarity | [algorithm](https://github.com/Das-rebel/adaptive-memory-multi-model-router/blob/main/src/cache/semanticCache.ts) |
|
|
544
|
-
| [MemoryTree](#adaptive-memory--learning) | Hierarchical context storage | [implementation](https://github.com/Das-rebel/adaptive-memory-multi-model-router/blob/main/src/memory/memoryTree.ts) |
|
|
545
|
-
| [MCTS Orchestration](#mcts-workflow-optimization) | Monte Carlo agent assignment | [UCB1 formula](#mcts-workflow-optimization) |
|
|
546
|
-
| [Cost Analytics](#cost-analytics) | Per-provider budget tracking | [tracker](https://github.com/Das-rebel/adaptive-memory-multi-model-router/blob/main/src/analytics/costTracker.ts) |
|
|
547
|
-
| [Circuit Breaker](#comparison) | Provider failover | [3-failure rule](#comparison) |
|
|
548
|
-
|
|
549
|
-
### Routing Flow Diagram
|
|
550
|
-
|
|
551
|
-
```
|
|
552
|
-
Query → Guardrails → Cache? → Complexity → Tier → Cheapest Available
|
|
553
|
-
↓ ↓
|
|
554
|
-
HIT Score → Route
|
|
555
|
-
↓ ↓
|
|
556
|
-
Return Fallback models
|
|
557
|
-
cached (2 configured)
|
|
558
|
-
↓
|
|
559
|
-
Cache miss → LLM call → Memory update → Response
|
|
560
|
-
```
|
|
561
|
-
|
|
562
|
-
### Optimization Levers
|
|
563
|
-
|
|
564
|
-
| Lever | How It Works | Impact |
|
|
565
|
-
|-------|-------------|--------|
|
|
566
|
-
| **Cache hit rate** | Higher similarity threshold → fewer misses, more savings | ~30% of queries cached |
|
|
567
|
-
| **Tier boundaries** | Adjust complexity thresholds | Moves queries up/down tiers |
|
|
568
|
-
| **Model profiles** | EMA updates quality scores per model | Better model selection over time |
|
|
569
|
-
| **Provider health** | Circuit breaker excludes failed providers | 99.9% uptime SLA |
|
|
570
|
-
| **MCTS iterations** | More iterations → better strategy, slower | 50 default, increase for critical tasks |
|
|
571
|
-
|
|
572
|
-
For production tuning, see [`docs/GENERATIVE_ENGINE_TUNING.md`](docs/GENERATIVE_ENGINE_TUNING.md).
|
|
573
611
|
|
|
574
612
|
## Features in Detail
|
|
575
613
|
|
|
@@ -746,20 +784,22 @@ const modelWithTools = model.bindTools([searchTool, calculatorTool]);
|
|
|
746
784
|
|
|
747
785
|
## Comparison
|
|
748
786
|
|
|
749
|
-
| Feature | A3M Router | [LiteLLM](https://github.com/BerriAI/litellm) | [Portkey](https://github.com/Portkey-AI/gateway) | [
|
|
787
|
+
| Feature | A3M Router | [LiteLLM](https://github.com/BerriAI/litellm) | [Portkey](https://github.com/Portkey-AI/gateway) | [RouteLLM](https://github.com/Surfsol/RouteLLM) |
|
|
750
788
|
|---------|:----------:|:-------:|:-------:|:-------:|
|
|
751
789
|
| **Routing accuracy published** | **Yes** (99.5% ±1) | No (manual) | No | No |
|
|
752
790
|
| **Intelligent routing** | Multi-signal per-query | Manual selection | Manual | Manual |
|
|
753
791
|
| **Zero ML / Zero GPU** | **Yes** | Yes | Yes | Yes |
|
|
754
|
-
| **Package size** | 19.5 KB | ~50 MB | ~30 MB |
|
|
755
|
-
| **OpenAI-compatible proxy** | **Yes** | No | Yes | Yes |
|
|
756
|
-
| **Adaptive memory** | **Yes** | No | No | No |
|
|
757
|
-
| **Semantic cache** | **Yes** (trigram) | No | No |
|
|
758
|
-
| **Prompt injection detection** | **Yes** (17 patterns) | No | No |
|
|
759
|
-
| **PII redaction** | **Yes** | No | No |
|
|
760
|
-
| **Hallucination checks** | **Yes** | No | No | No |
|
|
761
|
-
| **Cost analytics** | **Yes** | No | Yes |
|
|
762
|
-
| **Budget alerts** | **Yes** | No | No |
|
|
792
|
+
| **Package size** | 19.5 KB | ~50 MB | ~30 MB | ~15 MB |
|
|
793
|
+
| **OpenAI-compatible proxy** | **Yes** | No | Yes | Yes |
|
|
794
|
+
| **Adaptive memory** | **Yes** | No | No | No |
|
|
795
|
+
| **Semantic cache** | **Yes** (trigram) | No | No | No |
|
|
796
|
+
| **Prompt injection detection** | **Yes** (17 patterns) | No | No | No |
|
|
797
|
+
| **PII redaction** | **Yes** | No | No | No |
|
|
798
|
+
| **Hallucination checks** | **Yes** | No | No | No |
|
|
799
|
+
| **Cost analytics** | **Yes** | No | Yes | No |
|
|
800
|
+
| **Budget alerts** | **Yes** | No | No | No |
|
|
801
|
+
| **Circuit breaker** | **Yes** | No | No | No |
|
|
802
|
+
| **Multi-provider (36+)** | **Yes** | Yes | Yes | Yes |
|
|
763
803
|
| **Circuit breaker** | **Yes** | No | No | Yes | No |
|
|
764
804
|
| **LangChain adapter** | **Yes** | No | Yes | Yes | No |
|
|
765
805
|
| **Python SDK** | **Yes** | Yes | Yes | Yes | Yes |
|
|
@@ -814,6 +854,7 @@ import { createProxyServer } from 'adaptive-memory-multi-model-router/server';
|
|
|
814
854
|
- You only use one LLM provider
|
|
815
855
|
- Your workload is >80% expert-level queries (just use GPT-4o directly)
|
|
816
856
|
- You need 250+ provider integrations (use [Portkey](https://github.com/Portkey-AI/gateway))
|
|
857
|
+
- You need ML-based routing with BERT classifiers (use [RouteLLM](https://github.com/Surfsol/RouteLLM))
|
|
817
858
|
- You need enterprise SLAs or managed hosting
|
|
818
859
|
|
|
819
860
|
---
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|