adaptive-memory-multi-model-router 2.15.2 → 2.15.4
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/ARCHITECTURE.md +1 -1
- package/README.md +248 -150
- package/adapters/README.md +36 -0
- package/adapters/__init__.py +25 -0
- package/adapters/a3m_adapter/__init__.py +15 -0
- package/adapters/a3m_adapter/adapter/__init__.py +7 -0
- package/adapters/a3m_adapter/adapter/config.py +100 -0
- package/adapters/a3m_adapter/adapter/langchain.py +155 -0
- package/adapters/a3m_adapter/adapter/llamaindex.py +162 -0
- package/adapters/a3m_adapter/tests/__init__.py +1 -0
- package/adapters/a3m_adapter/tests/test_adapters.py +120 -0
- package/adapters/setup.py +23 -0
- package/articles/HN_SHOW_SEO.md +49 -0
- package/articles/PRODUCTHUNT_READY.md +47 -67
- package/articles/REDDIT_SEO_2026.md +50 -0
- package/demo/IMPROVED_PROMPTS.md +3 -3
- package/demo/VEO3_PROMPTS.md +2 -2
- package/demo/VIDEO_PRODUCTION_GUIDE.md +3 -3
- package/docs/comparison-litellm.md +10 -18
- package/docs/index.html +29 -34
- package/docs/llms-full.txt +286 -153
- package/docs/llms.txt +109 -70
- package/docs-site/index.html +19 -19
- package/llms.txt +109 -70
- package/mcp-server/package.json +0 -6
- package/mcp-server/tsconfig.json +2 -3
- package/package.json +13 -92
- package/sitemap.xml +15 -9
- package/src/routing/advancedRouter.ts +101 -0
- package/src/routing/providerHealth.ts +234 -4
- package/src/routing/shadowSampler.ts +427 -0
- package/.github/ISSUE_TEMPLATE/checklist.md +0 -35
package/docs/llms-full.txt
CHANGED
|
@@ -1,199 +1,332 @@
|
|
|
1
|
-
# A3M Router —
|
|
1
|
+
# A3M Router — Full Technical Documentation
|
|
2
2
|
|
|
3
3
|
## Overview
|
|
4
|
-
A3M Router is an OpenAI-compatible LLM routing gateway that selects the cheapest capable provider per query using multi-signal heuristic scoring. Evaluated on RouterArena across 8,400 queries: 96.77% accuracy, $0.0768/1K average cost, 1.0000 robustness, zero abnormal entries. Open-source, MIT licensed, 19.5 KB gzipped, zero ML dependencies.
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
**Repository:** `Das-rebel/a3m-router` (GitHub)
|
|
8
|
-
**Language:** TypeScript (Node.js)
|
|
9
|
-
**License:** MIT
|
|
5
|
+
A3M Router is a stateless proxy that routes LLM requests to the optimal provider based on query complexity analysis, cost, and availability.
|
|
10
6
|
|
|
11
|
-
|
|
7
|
+
## Routing Algorithm
|
|
12
8
|
|
|
13
|
-
|
|
9
|
+
### Complexity Scoring
|
|
14
10
|
|
|
15
|
-
|
|
11
|
+
Five signals are combined into a composite score:
|
|
16
12
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
| Abnormal entries | 0 |
|
|
24
|
-
| Queries evaluated | 8,400 |
|
|
13
|
+
1. **Domain Detection**
|
|
14
|
+
- Legal: contract, lawsuit, compliance, patent
|
|
15
|
+
- Medical: diagnosis, treatment, prescription, symptoms
|
|
16
|
+
- Code: function, class, API, debugging, refactor
|
|
17
|
+
- Finance: investment, portfolio, risk, return, audit
|
|
18
|
+
- ML: training, inference, gradient, loss, model
|
|
25
19
|
|
|
26
|
-
|
|
20
|
+
2. **Task Classification**
|
|
21
|
+
- Code generation: write, implement, create function
|
|
22
|
+
- Translation: translate, convert, rewrite in
|
|
23
|
+
- Analysis: compare, evaluate, assess, analyze
|
|
24
|
+
- Creative: write story, poem, generate idea
|
|
25
|
+
- Factual: what is, who was, when did, where is
|
|
27
26
|
|
|
28
|
-
|
|
27
|
+
3. **Structural Analysis**
|
|
28
|
+
- Clause count: complex sentences
|
|
29
|
+
- Explicit steps: first...then, step 1/2/3
|
|
30
|
+
- Qualifications: might, could, possibly
|
|
31
|
+
- Conditional: if...then, unless, provided that
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
| RouterEval | EMNLP 2025 | Baseline merged | MilkThink-Lab/RouterEval#4 |
|
|
34
|
-
| MMR-Bench | ArXiv 2026 | Baseline merged | Hunter-Wrynn/MMR-Bench#4 |
|
|
35
|
-
| LLMRouterBench | ACL 2026 | Submitted | ynulihao/LLMRouterBench#3 |
|
|
33
|
+
4. **Verb Intensity**
|
|
34
|
+
- Complex verbs: design, architect, optimize, synthesize
|
|
35
|
+
- Simple verbs: what, who, find, get
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
5. **Multi-Modal Hints**
|
|
38
|
+
- Image references: explain this diagram
|
|
39
|
+
- Code blocks: debug this function
|
|
40
|
+
- Data: analyze this dataset
|
|
38
41
|
|
|
39
|
-
|
|
40
|
-
|--------|-------|
|
|
41
|
-
| Exact tier match | 67% |
|
|
42
|
-
| Within 1 tier | 96% |
|
|
43
|
-
| Cost savings vs all-premium | 62.9% |
|
|
42
|
+
### Tier Assignment
|
|
44
43
|
|
|
45
|
-
|
|
44
|
+
Score maps to tier:
|
|
46
45
|
|
|
47
|
-
|
|
46
|
+
| Score Range | Tier | Providers | Example |
|
|
47
|
+
|------------|------|-----------|---------|
|
|
48
|
+
| 0-20 | Free | Ollama, Llama.cpp | Simple what/who |
|
|
49
|
+
| 21-40 | Cheap | Groq, DeepSeek, Mistral | Short code, basic QA |
|
|
50
|
+
| 41-70 | Mid | GPT-4o-mini, Claude-haiku | Standard tasks |
|
|
51
|
+
| 71-100 | Premium | GPT-4o, Claude-sonnet, Gemini | Complex reasoning |
|
|
48
52
|
|
|
53
|
+
## Ensemble Execution
|
|
54
|
+
|
|
55
|
+
### Configuration
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
router = A3MRouter(
|
|
59
|
+
model="auto",
|
|
60
|
+
parallel_ensemble=3,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
result = router.route(
|
|
64
|
+
messages=[{"role": "user", "content": prompt}],
|
|
65
|
+
ensemble_config={
|
|
66
|
+
"providers": ["groq", "openai", "deepseek"],
|
|
67
|
+
"timeout_ms": 15000,
|
|
68
|
+
"score_weights": {
|
|
69
|
+
"relevance": 0.4,
|
|
70
|
+
"conciseness": 0.3,
|
|
71
|
+
"accuracy": 0.3,
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
)
|
|
49
75
|
```
|
|
50
|
-
Request → Guardrails → Semantic Cache → Router (5-signal heuristic) → Provider → Response
|
|
51
|
-
```
|
|
52
76
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
###
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
| 0.70–1.00 | premium | gpt-4o ($2.50/M), claude-3.5-sonnet ($1.50/M) |
|
|
83
|
-
|
|
84
|
-
---
|
|
85
|
-
|
|
86
|
-
## Provider Coverage (47+)
|
|
87
|
-
|
|
88
|
-
| Provider | Tiers | Models |
|
|
89
|
-
|---------|-------|--------|
|
|
90
|
-
| OpenAI | premium, mid | gpt-4o, gpt-4o-mini |
|
|
91
|
-
| Anthropic | premium, mid | claude-3.5-sonnet, claude-3-haiku |
|
|
92
|
-
| Google | premium, mid | gemini-1.5-pro, gemini-1.5-flash |
|
|
93
|
-
| Groq | cheap | llama-3.3-70b, llama-3.1-8b |
|
|
94
|
-
| DeepSeek | cheap, mid | deepseek-chat, deepseek-coder |
|
|
95
|
-
| Mistral | cheap, mid | mistral-large, mistral-small |
|
|
96
|
-
| NVIDIA | premium | nvidia/llama-3.1-nemotron |
|
|
97
|
-
| OpenRouter | all | aggregated access |
|
|
98
|
-
| Kimi | cheap | moonshot-v1 |
|
|
99
|
-
| Qwen | cheap, mid | qwen-turbo, qwen-plus |
|
|
100
|
-
| Zhipu | cheap | glm-4 |
|
|
101
|
-
| Yi | cheap | yi-large |
|
|
102
|
-
| Azure OpenAI | premium, mid | via OpenAI-compatible endpoint |
|
|
103
|
-
| AWS Bedrock | premium, mid | via OpenAI-compatible endpoint |
|
|
104
|
-
| Local Ollama | all | configurable model discovery |
|
|
105
|
-
| Local vLLM | all | OpenAI-compatible server |
|
|
106
|
-
|
|
107
|
-
---
|
|
108
|
-
|
|
109
|
-
## Feature Specifications
|
|
110
|
-
|
|
111
|
-
### Parallel Ensemble
|
|
112
|
-
Executes a single query against multiple providers simultaneously. Each response is scored on specificity, structure, and relevance. The highest-scoring result is returned with full provenance.
|
|
113
|
-
|
|
114
|
-
```typescript
|
|
115
|
-
import { executeEnsemble } from 'adaptive-memory-multi-model-router/ensemble';
|
|
116
|
-
const result = await executeEnsemble(query, systemPrompt, context, providers, options);
|
|
117
|
-
// result.winner — provider key
|
|
118
|
-
// result.scores — per-provider score map
|
|
119
|
-
// result.reasoning — human-readable scoring rationale
|
|
120
|
-
// result.allResults — preserved responses from all providers
|
|
77
|
+
### Scoring Algorithm
|
|
78
|
+
|
|
79
|
+
1. Collect all responses within timeout
|
|
80
|
+
2. Compute per-provider scores:
|
|
81
|
+
- Relevance: cosine similarity to query embedding
|
|
82
|
+
- Conciseness: ratio of signal tokens / total tokens
|
|
83
|
+
- Accuracy: factual consistency score
|
|
84
|
+
3. Weighted sum → normalized scores
|
|
85
|
+
4. Winner = provider with highest weighted score
|
|
86
|
+
|
|
87
|
+
### Provider Response
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
{
|
|
91
|
+
"content": "winning response text",
|
|
92
|
+
"provider": "openai",
|
|
93
|
+
"scores": {
|
|
94
|
+
"groq": {"relevance": 0.85, "conciseness": 0.9, "accuracy": 0.88},
|
|
95
|
+
"openai": {"relevance": 0.92, "conciseness": 0.85, "accuracy": 0.95},
|
|
96
|
+
"deepseek": {"relevance": 0.88, "conciseness": 0.82, "accuracy": 0.90},
|
|
97
|
+
},
|
|
98
|
+
"all_results": {
|
|
99
|
+
"groq": {"content": "...", "latency_ms": 450},
|
|
100
|
+
"openai": {"content": "...", "latency_ms": 1200},
|
|
101
|
+
"deepseek": {"content": "...", "latency_ms": 800},
|
|
102
|
+
},
|
|
103
|
+
"latency_ms": 1200,
|
|
104
|
+
"cost_usd": 0.0012,
|
|
105
|
+
}
|
|
121
106
|
```
|
|
122
107
|
|
|
108
|
+
## Memory System
|
|
109
|
+
|
|
123
110
|
### Semantic Cache
|
|
124
|
-
Embedding-based lookup with configurable similarity threshold (default 0.92). Per-route TTL allows different freshness requirements per query domain.
|
|
125
111
|
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
112
|
+
```python
|
|
113
|
+
router = A3MRouter(
|
|
114
|
+
model="auto",
|
|
115
|
+
cache={
|
|
116
|
+
"type": "semantic",
|
|
117
|
+
"threshold": 0.85, # cosine similarity
|
|
118
|
+
"ttl_seconds": 3600,
|
|
119
|
+
},
|
|
120
|
+
)
|
|
130
121
|
```
|
|
131
122
|
|
|
132
|
-
###
|
|
133
|
-
Prompt injection detection covers 17 patterns including jailbreak templates, system prompt overrides, and delimiter-based injection. PII detection supports common entity types.
|
|
123
|
+
### Conversation Context
|
|
134
124
|
|
|
135
|
-
|
|
136
|
-
|
|
125
|
+
```python
|
|
126
|
+
router = A3MRouter(
|
|
127
|
+
model="auto",
|
|
128
|
+
memory={
|
|
129
|
+
"type": "conversation",
|
|
130
|
+
"window": 10, # last 10 exchanges
|
|
131
|
+
"embedding_model": "text-embedding-3-small",
|
|
132
|
+
},
|
|
133
|
+
)
|
|
134
|
+
```
|
|
137
135
|
|
|
138
|
-
###
|
|
139
|
-
Per-user and per-team monthly spend caps with hard limits. Real-time alerts at 50%, 80%, and 100% thresholds. Per-provider cost breakdown.
|
|
136
|
+
### Cross-Session Memory
|
|
140
137
|
|
|
141
|
-
|
|
142
|
-
|
|
138
|
+
```python
|
|
139
|
+
router = A3MRouter(
|
|
140
|
+
model="auto",
|
|
141
|
+
memory={
|
|
142
|
+
"type": "semantic",
|
|
143
|
+
"persistent": True,
|
|
144
|
+
"namespace": "user_123",
|
|
145
|
+
"similarity_threshold": 0.85,
|
|
146
|
+
},
|
|
147
|
+
)
|
|
148
|
+
```
|
|
143
149
|
|
|
144
|
-
|
|
145
|
-
Custom timeout per provider. Exponential backoff with jitter. Rate limit detection (429) triggers Retry-After-aware backoff.
|
|
150
|
+
## Guardrails
|
|
146
151
|
|
|
147
|
-
|
|
152
|
+
### Prompt Injection Detection
|
|
148
153
|
|
|
149
|
-
|
|
154
|
+
```python
|
|
155
|
+
# Patterns detected:
|
|
156
|
+
# - System prompt override attempts
|
|
157
|
+
# - Delimiter injection (USER:, SANDBOX:)
|
|
158
|
+
# - Role confusion attacks
|
|
159
|
+
# - Privilege escalation patterns
|
|
160
|
+
```
|
|
150
161
|
|
|
151
|
-
|
|
152
|
-
|--------|----------|-------------|
|
|
153
|
-
| POST | `/v1/chat/completions` | OpenAI-compatible chat |
|
|
154
|
-
| POST | `/v1/route` | Routing decision without LLM call |
|
|
155
|
-
| GET | `/v1/models` | Available models with pricing |
|
|
156
|
-
| GET | `/health` | Provider health scores |
|
|
162
|
+
### PII Detection
|
|
157
163
|
|
|
158
|
-
|
|
164
|
+
- Email addresses, phone numbers, SSNs
|
|
165
|
+
- Credit card numbers
|
|
166
|
+
- API keys and secrets
|
|
159
167
|
|
|
160
|
-
##
|
|
168
|
+
## Health Scoring
|
|
161
169
|
|
|
162
|
-
|
|
163
|
-
npm install adaptive-memory-multi-model-router
|
|
164
|
-
npx a3m-router serve # proxy at http://localhost:8787
|
|
165
|
-
```
|
|
170
|
+
Provider health updated via exponential moving average:
|
|
166
171
|
|
|
167
172
|
```python
|
|
168
|
-
|
|
173
|
+
health_score = (
|
|
174
|
+
0.7 * previous_score +
|
|
175
|
+
0.3 * (1 - error_rate)
|
|
176
|
+
) * latency_factor
|
|
169
177
|
```
|
|
170
178
|
|
|
179
|
+
Where `latency_factor` penalizes slow responses:
|
|
180
|
+
- <1s: 1.0
|
|
181
|
+
- 1-3s: 0.9
|
|
182
|
+
- 3-10s: 0.7
|
|
183
|
+
- >10s: 0.3
|
|
184
|
+
|
|
185
|
+
## Rate Limiting
|
|
186
|
+
|
|
187
|
+
### Charnov MVT Implementation
|
|
188
|
+
|
|
189
|
+
Optimal departure time from rate-limited provider:
|
|
190
|
+
|
|
191
|
+
```
|
|
192
|
+
depart_when: marginal_remaining_rate < average_rate_including_switch_cost
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Rotation Strategy
|
|
196
|
+
|
|
197
|
+
1. Track rate-limit windows per provider
|
|
198
|
+
2. When window depletes < threshold, begin rotation
|
|
199
|
+
3. Switch to next healthiest provider in tier
|
|
200
|
+
4. Track rotation frequency to avoid thrashing
|
|
201
|
+
|
|
202
|
+
## EXP3 Diversity
|
|
203
|
+
|
|
204
|
+
### Weight Update
|
|
205
|
+
|
|
171
206
|
```python
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
207
|
+
for provider in providers:
|
|
208
|
+
deviation = provider.share - (1 / n) # actual share vs fair share
|
|
209
|
+
penalty = GAMMA * deviation / provider.share
|
|
210
|
+
provider.weight *= exp(-penalty)
|
|
175
211
|
```
|
|
176
212
|
|
|
177
|
-
|
|
213
|
+
### Normalization
|
|
214
|
+
|
|
215
|
+
Weights normalized to sum to 1.0 after each update.
|
|
178
216
|
|
|
179
|
-
##
|
|
217
|
+
## Benchmark Methodology
|
|
180
218
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
219
|
+
RouterArena evaluation:
|
|
220
|
+
- 8,400 diverse queries
|
|
221
|
+
- 47 providers tested
|
|
222
|
+
- Accuracy measured via LLM judge comparison
|
|
223
|
+
- Cost tracked via actual API spend
|
|
224
|
+
- Robustness = successful requests / total requests
|
|
225
|
+
|
|
226
|
+
## API Reference
|
|
227
|
+
|
|
228
|
+
### POST /v1/chat/completions
|
|
229
|
+
|
|
230
|
+
Request:
|
|
231
|
+
```json
|
|
232
|
+
{
|
|
233
|
+
"model": "auto",
|
|
234
|
+
"messages": [{"role": "user", "content": "..."}],
|
|
235
|
+
"temperature": 0.7,
|
|
236
|
+
"max_tokens": 4096,
|
|
237
|
+
"parallel_ensemble": 1,
|
|
238
|
+
"stream": false
|
|
188
239
|
}
|
|
189
240
|
```
|
|
190
241
|
|
|
191
|
-
|
|
242
|
+
Response:
|
|
243
|
+
```json
|
|
244
|
+
{
|
|
245
|
+
"id": "chatcmpl-xxx",
|
|
246
|
+
"object": "chat.completion",
|
|
247
|
+
"created": 1234567890,
|
|
248
|
+
"model": "auto",
|
|
249
|
+
"provider": "groq",
|
|
250
|
+
"choices": [{
|
|
251
|
+
"message": {"role": "assistant", "content": "..."},
|
|
252
|
+
"finish_reason": "stop",
|
|
253
|
+
"index": 0
|
|
254
|
+
}],
|
|
255
|
+
"usage": {
|
|
256
|
+
"prompt_tokens": 20,
|
|
257
|
+
"completion_tokens": 150,
|
|
258
|
+
"total_tokens": 170
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
## Environment Variables
|
|
264
|
+
|
|
265
|
+
| Variable | Description | Default |
|
|
266
|
+
|----------|-------------|---------|
|
|
267
|
+
| A3M_PORT | Server port | 8787 |
|
|
268
|
+
| A3M_API_KEYS | JSON of provider keys | {} |
|
|
269
|
+
| A3M_BUDGET_MONTHLY | Monthly budget limit | unlimited |
|
|
270
|
+
| A3M_CACHE_TTL | Cache TTL in seconds | 3600 |
|
|
271
|
+
| A3M_LOG_LEVEL | log level | info |
|
|
272
|
+
|
|
273
|
+
## Architecture Diagram
|
|
274
|
+
|
|
275
|
+
```
|
|
276
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
277
|
+
│ Client Request │
|
|
278
|
+
└─────────────────────────┬───────────────────────────────────┘
|
|
279
|
+
│
|
|
280
|
+
┌─────────────────────────▼───────────────────────────────────┐
|
|
281
|
+
│ Guardrails │
|
|
282
|
+
│ • Prompt injection detection │
|
|
283
|
+
│ • PII filtering │
|
|
284
|
+
│ • Content safety │
|
|
285
|
+
└─────────────────────────┬───────────────────────────────────┘
|
|
286
|
+
│
|
|
287
|
+
┌─────────────────────────▼───────────────────────────────────┐
|
|
288
|
+
│ Semantic Cache │
|
|
289
|
+
│ • Embedding similarity lookup │
|
|
290
|
+
│ • Zero-cost hits │
|
|
291
|
+
└─────────────────────────┬───────────────────────────────────┘
|
|
292
|
+
│ cache miss
|
|
293
|
+
┌─────────────────────────▼───────────────────────────────────┐
|
|
294
|
+
│ Router │
|
|
295
|
+
│ • Complexity scoring │
|
|
296
|
+
│ • Tier assignment │
|
|
297
|
+
│ • Provider selection │
|
|
298
|
+
│ • EXP3 diversity weighting │
|
|
299
|
+
│ • Charnov MVT rate-limit rotation │
|
|
300
|
+
└─────────────────────────┬───────────────────────────────────┘
|
|
301
|
+
│
|
|
302
|
+
┌─────────────────┼─────────────────┐
|
|
303
|
+
│ │ │
|
|
304
|
+
┌───────▼───────┐ ┌──────▼──────┐ ┌──────▼──────┐
|
|
305
|
+
│ Provider 1 │ │ Provider 2 │ │ Provider 3 │
|
|
306
|
+
│ (Groq) │ │ (OpenAI) │ │ (DeepSeek) │
|
|
307
|
+
└───────────────┘ └─────────────┘ └─────────────┘
|
|
308
|
+
│ │ │
|
|
309
|
+
└─────────────────┼─────────────────┘
|
|
310
|
+
│
|
|
311
|
+
┌─────────────────────────▼───────────────────────────────────┐
|
|
312
|
+
│ Ensemble Scorer │
|
|
313
|
+
│ • Collect responses │
|
|
314
|
+
│ • Score on quality metrics │
|
|
315
|
+
│ • Select winner │
|
|
316
|
+
└─────────────────────────┬───────────────────────────────────┘
|
|
317
|
+
│
|
|
318
|
+
┌─────────────────────────▼───────────────────────────────────┐
|
|
319
|
+
│ Memory Layer │
|
|
320
|
+
│ • Cache response │
|
|
321
|
+
│ • Update context │
|
|
322
|
+
│ • Store embeddings │
|
|
323
|
+
└─────────────────────────┬───────────────────────────────────┘
|
|
324
|
+
│
|
|
325
|
+
┌─────────────────────────▼───────────────────────────────────┐
|
|
326
|
+
│ Response │
|
|
327
|
+
└─────────────────────────────────────────────────────────────┘
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
## License
|
|
192
331
|
|
|
193
|
-
|
|
194
|
-
- RouteWorks/RouterArena (ICLR 2025): https://github.com/RouteWorks/RouterArena
|
|
195
|
-
- MilkThink-Lab/RouterEval (EMNLP 2025): https://github.com/MilkThink-Lab/RouterEval
|
|
196
|
-
- Hunter-Wrynn/MMR-Bench (ArXiv 2026): https://github.com/Hunter-Wrynn/MMR-Bench
|
|
197
|
-
- ynulihao/LLMRouterBench (ACL 2026): https://github.com/ynulihao/LLMRouterBench
|
|
198
|
-
- Lin et al. "RouteLLM." arXiv:2404.06035, 2024
|
|
199
|
-
- Zhong et al. "RadixAttention." arXiv:2412.15115, 2024
|
|
332
|
+
MIT
|
package/docs/llms.txt
CHANGED
|
@@ -1,79 +1,118 @@
|
|
|
1
|
-
# A3M Router
|
|
1
|
+
# A3M Router — LLM Indexable Documentation
|
|
2
2
|
|
|
3
|
-
##
|
|
4
|
-
A3M Router is an OpenAI-compatible LLM routing gateway that selects the cheapest capable provider per query using multi-signal heuristic scoring. Evaluated on RouterArena across 8,400 queries: 96.77% accuracy, $0.0768/1K average cost, 1.0000 robustness, zero abnormal entries.
|
|
3
|
+
## What is A3M Router?
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
- Stateless proxy between client applications and LLM providers
|
|
8
|
-
- Pipeline: Guardrails → Semantic Cache → Multi-Signal Heuristic Routing → Provider Execution
|
|
9
|
-
- No ML training required; no GPU resources needed for routing decisions
|
|
5
|
+
A3M Router is an intelligent LLM routing proxy that automatically selects the cheapest capable model for each request across 47+ providers.
|
|
10
6
|
|
|
11
|
-
##
|
|
12
|
-
Multi-signal heuristic scoring across five dimensions:
|
|
13
|
-
1. Domain detection (legal, medical, security, finance, code, ML) — up to +0.35
|
|
14
|
-
2. Task indicators (code, math, translate, creative) — up to +0.25
|
|
15
|
-
3. Query structure (clauses, length, qualifiers) — up to +0.20
|
|
16
|
-
4. Action verb intensity (expert/mid/simple) — +0.20 to −0.10
|
|
17
|
-
5. Multi-step detection (explicit step markers) — up to +0.15
|
|
7
|
+
## Core Capabilities
|
|
18
8
|
|
|
19
|
-
|
|
9
|
+
### 1. Automatic Model Selection
|
|
10
|
+
- Analyzes query complexity (domain, task type, structure, verb intensity)
|
|
11
|
+
- Maps to tier: Free → Cheap → Mid → Premium
|
|
12
|
+
- Selects cheapest healthy provider within tier
|
|
13
|
+
- Routing happens in ~140ms overhead
|
|
20
14
|
|
|
21
|
-
|
|
15
|
+
### 2. Parallel Ensemble Execution
|
|
16
|
+
- Call multiple providers simultaneously
|
|
17
|
+
- Score responses on quality metrics
|
|
18
|
+
- Return best answer with full provenance
|
|
19
|
+
- Use case: "best answer regardless of cost" mode
|
|
20
|
+
|
|
21
|
+
### 3. Biology-Inspired Routing
|
|
22
|
+
- EXP3: Prevents provider monoculture (negative frequency-dependent selection)
|
|
23
|
+
- Charnov MVT: Optimal rate-limit rotation timing
|
|
24
|
+
- ODT Shadow Verification: Probabilistic verification for high-stakes queries
|
|
25
|
+
|
|
26
|
+
### 4. Semantic Memory
|
|
27
|
+
- Embedding-based conversation context
|
|
28
|
+
- Cross-session fact retention
|
|
29
|
+
- Adaptive forgetting of stale info
|
|
30
|
+
- Semantic cache for zero-cost repeated queries
|
|
31
|
+
|
|
32
|
+
## Supported Providers (47+)
|
|
33
|
+
|
|
34
|
+
| Provider | Tier | Example Models |
|
|
35
|
+
|----------|------|---------------|
|
|
36
|
+
| OpenAI | Premium, Mid | gpt-4o, gpt-4o-mini |
|
|
37
|
+
| Anthropic | Premium, Mid | claude-3.5-sonnet, claude-3-haiku |
|
|
38
|
+
| Google | Premium, Mid | gemini-1.5-pro, gemini-1.5-flash |
|
|
39
|
+
| Groq | Cheap | llama-3.3-70b, llama-3.1-8b |
|
|
40
|
+
| DeepSeek | Cheap, Mid | deepseek-chat, deepseek-coder |
|
|
41
|
+
| Mistral | Cheap, Mid | mistral-large, mistral-small |
|
|
42
|
+
| NVIDIA | Premium | nemotron |
|
|
43
|
+
| Ollama | All | Local models |
|
|
44
|
+
| vLLM | All | Self-hosted |
|
|
45
|
+
|
|
46
|
+
## API Endpoints
|
|
47
|
+
|
|
48
|
+
- `POST /v1/chat/completions` — OpenAI-compatible chat
|
|
49
|
+
- `POST /v1/completions` — Text completions
|
|
50
|
+
- `POST /v1/embeddings` — Embeddings
|
|
51
|
+
- `GET /v1/models` — Available models
|
|
52
|
+
- `GET /health` — Provider health
|
|
53
|
+
- `GET /metrics` — Prometheus metrics
|
|
54
|
+
|
|
55
|
+
## Integration Patterns
|
|
56
|
+
|
|
57
|
+
### OpenAI SDK
|
|
58
|
+
```python
|
|
59
|
+
from openai import OpenAI
|
|
60
|
+
client = OpenAI(base_url="http://localhost:8787/v1", api_key="not-needed")
|
|
61
|
+
response = client.chat.completions.create(model="auto", messages=[...])
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### LangChain
|
|
65
|
+
```python
|
|
66
|
+
from a3m_adapter import A3MLangChainAdapter
|
|
67
|
+
llm = A3MLangChainAdapter(model="auto", parallel_ensemble=2)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### LlamaIndex
|
|
71
|
+
```python
|
|
72
|
+
from a3m_adapter import A3MLlamaIndexAdapter
|
|
73
|
+
llm = A3MLlamaIndexAdapter(model="auto")
|
|
74
|
+
```
|
|
22
75
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
| Avg Cost / 1K tokens | $0.0768 |
|
|
28
|
-
| Robustness | 1.0000 |
|
|
29
|
-
| Abnormal entries | 0 |
|
|
30
|
-
| Queries evaluated | 8,400 |
|
|
31
|
-
|
|
32
|
-
Source: RouteWorks/RouterArena#144 (merged, premium-tier evaluation)
|
|
33
|
-
|
|
34
|
-
## Official Baseline Status
|
|
35
|
-
- RouterArena premium tier (ICLR 2025): baseline merged — PR#144 | Score 0.9404, Accuracy 96.77%
|
|
36
|
-
- RouterArena free tier (ICLR 2025): baseline submitted — PR#152 | 50.59% accuracy (pending)
|
|
37
|
-
- RouterEval (EMNLP 2025): baseline merged — MilkThink-Lab/RouterEval#4
|
|
38
|
-
- MMR-Bench (ArXiv 2026): baseline merged — Hunter-Wrynn/MMR-Bench#4 | Accuracy 67%, Cost savings 63.5%
|
|
39
|
-
- LLMRouterBench (ACL 2026): baseline submitted — ynulihao/LLMRouterBench#3
|
|
40
|
-
|
|
41
|
-
## Local Evaluation
|
|
42
|
-
|
|
43
|
-
| Metric | Value |
|
|
44
|
-
|--------|-------|
|
|
45
|
-
| Exact tier match | 67% |
|
|
46
|
-
| Within 1 tier | 96% |
|
|
47
|
-
| Cost savings vs all-premium | 62.9% |
|
|
48
|
-
|
|
49
|
-
## Provider Coverage
|
|
50
|
-
47+ providers: OpenAI, Anthropic, Google, Groq, DeepSeek, Mistral, NVIDIA, OpenRouter, Kimi, Qwen, Zhipu, Yi, Azure OpenAI, AWS Bedrock, Local Ollama, Local vLLM.
|
|
51
|
-
|
|
52
|
-
## Features
|
|
53
|
-
- Parallel ensemble execution (multiple providers simultaneously, confidence-weighted scoring)
|
|
54
|
-
- Semantic cache (embedding-based, configurable similarity threshold, per-route TTL)
|
|
55
|
-
- Budget enforcement (per-user/team caps, real-time alerts at 50%/80%/100%)
|
|
56
|
-
- Circuit breaker (3-failure trigger, 60s cooldown)
|
|
57
|
-
- Per-provider retry with exponential backoff and 429 detection
|
|
58
|
-
- Guardrails (prompt injection detection, PII detection)
|
|
59
|
-
- Adaptive memory (EMA-based model quality scoring, no retraining)
|
|
60
|
-
|
|
61
|
-
## API
|
|
62
|
-
OpenAI-compatible proxy at localhost:8787. Model selection via `model="auto"` invokes heuristic routing.
|
|
63
|
-
|
|
64
|
-
## Citation
|
|
76
|
+
### CrewAI
|
|
77
|
+
```python
|
|
78
|
+
from crewai.llms import A3MCompletion
|
|
79
|
+
agent = Agent(llm=A3MCompletion(model="auto"))
|
|
65
80
|
```
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
81
|
+
|
|
82
|
+
## Cost Savings
|
|
83
|
+
|
|
84
|
+
| Query | GPT-4o | A3M | Savings |
|
|
85
|
+
|-------|---------|-----|---------|
|
|
86
|
+
| Simple Q&A | $0.03 | $0.0001 | 99.7% |
|
|
87
|
+
| Code generation | $0.05 | $0.002 | 96% |
|
|
88
|
+
| Complex reasoning | $0.15 | $0.15 | 0% (correct) |
|
|
89
|
+
|
|
90
|
+
## Memory Features
|
|
91
|
+
|
|
92
|
+
- **Semantic Cache**: Instant responses for similar queries
|
|
93
|
+
- **Conversation Context**: Maintains chat history
|
|
94
|
+
- **Cross-Session Memory**: Remembers important facts
|
|
95
|
+
- **Adaptive Forgetting**: Auto-evicts stale info
|
|
96
|
+
|
|
97
|
+
## Benchmark Results
|
|
98
|
+
|
|
99
|
+
RouterArena (8,400 queries):
|
|
100
|
+
- Accuracy: 96.77%
|
|
101
|
+
- Cost: $0.0768/1K
|
|
102
|
+
- Robustness: 1.0000
|
|
103
|
+
|
|
104
|
+
## Installation
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
npm install adaptive-memory-multi-model-router
|
|
108
|
+
pip install adaptive-memory-multi-model-router
|
|
109
|
+
docker run -p 8787:8787 ghcr.io/das-rebel/a3m-router
|
|
73
110
|
```
|
|
74
111
|
|
|
75
|
-
##
|
|
76
|
-
|
|
77
|
-
-
|
|
78
|
-
|
|
79
|
-
|
|
112
|
+
## Keywords
|
|
113
|
+
|
|
114
|
+
llm-router, ai-gateway, model-routing, cost-optimization, multi-provider, openai-compatible, langchain, llamaindex, crewai, parallel-execution, semantic-cache, adaptive-routing, failover, guardrails, cache, budget-alerts, streaming, retries, circuit-breaker
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
MIT
|