gemma4-adaptive-router
Complexity + VRAM-aware routing for local dual-tier LLM deployments.
The only public implementation of sub-millisecond complexity scoring coupled to real-time VRAM scheduling for consumer GPU on-prem inference.
Install
pip install gemma4-adaptive-router
Quickstart
from adaptive_router import AdaptiveRouter, RoutingConfig
import time
config = RoutingConfig(
complexity_threshold=0.65, # score >= this → tier_high
vram_headroom_gb=1.5, # free VRAM below this → force tier_low
latency_sla_ms=2000.0, # EMA latency above this → force tier_low
sla_warmup_seed_ms=800.0, # seed EMA to avoid cold-start burst on tier_high
)
router = AdaptiveRouter(config)
# Route a query
tier = router.route("Explain the proof of Fermat's Last Theorem step by step")
# → "tier_high" (complex math query, VRAM available)
# After you get the response, report the actual latency so the SLA rule adapts
t0 = time.monotonic()
# ... call your LLM endpoint ...
router.observe(tier, latency_ms=(time.monotonic() - t0) * 1000)
# Clean up background VRAM monitor thread
router.shutdown()
Architecture
┌─────────────────────────────────────────────────────────────┐
│ adaptive_router/ │
│ │
│ Layer 1: ComplexityScorer │
│ ├── Rule-based, 6 dimensions, sub-ms, zero external calls │
│ ├── math (0.25) · code (0.25) · depth (0.20) │
│ ├── tokens (0.15) · entities (0.10) · negation (0.05) │
│ └── Output: score in [0.0, 1.0] │
│ │
│ Layer 2: VRAMMonitor (daemon thread) │
│ ├── pynvml direct — no subprocess nvidia-smi overhead │
│ ├── Polling at 10-20ms with atomic shared state │
│ └── Thread-safe VRAMState (free_gb, used_gb, util_pct) │
│ │
│ Layer 3: RoutingDecision (chain of rules) │
│ ├── complexity_rule: score < threshold → tier_low │
│ ├── vram_rule: free_gb < headroom → tier_low │
│ ├── sla_rule: EMA latency > SLA → tier_low │
│ └── Default fallthrough: tier_high │
└─────────────────────────────────────────────────────────────┘
Configuration reference
| Field | Type | Default | Effect |
|---|---|---|---|
complexity_threshold |
float |
0.65 |
Score ≥ this routes to tier_high |
vram_headroom_gb |
float |
1.5 |
Free VRAM below this forces tier_low |
latency_sla_ms |
float |
2000.0 |
EMA latency above this forces tier_low |
vram_poll_interval_ms |
int |
15 |
VRAM polling frequency |
sla_warmup_seed_ms |
float |
0.0 |
EMA seed at startup — set to p50 of tier_high to avoid cold-start burst |
Load from YAML:
from adaptive_router.config import load_config
config = load_config("router_config.yaml")
# router_config.yaml
complexity_threshold: 0.65
vram_headroom_gb: 1.5
latency_sla_ms: 2000.0
vram_poll_interval_ms: 15
sla_warmup_seed_ms: 800.0
Deploy as FastAPI proxy
python -m adaptive_router.middleware \
--tier-high-url http://llama-cpp:8080/v1 \
--tier-low-url http://vllm:8000/v1 \
--port 9000
Exposes /v1/chat/completions (proxied), /health, and /metrics (Prometheus).
Custom routing rules
from adaptive_router import AdaptiveRouter, RoutingConfig, RouterState
def my_rule(query: str, state: RouterState):
# Force tier_high for any query mentioning "contract"
if "contract" in query.lower():
return "tier_high"
return None # abstain, let next rule decide
router = AdaptiveRouter(config, rules=[my_rule])
Known limitations
- 16GB does not fit 26B in vLLM natively. The router mitigates this by routing complex queries to llama.cpp. The real fix for 200 concurrent users wanting 26B is dual-GPU.
- Blackwell SM120 rough edges. FP8 KV and MTP require specific workarounds. See VRAM-REALITY-CHECK.md.
- EXL2 is not designed for multi-user production. TabbyAPI maintainers state this explicitly. It's in benchmarks for completeness only.
- SGLang does not always win. RadixAttention benefits depend on prefix overlap. See RADIXATTENTION-OPERATIVE-TABLE.md.
- MTP in SM120 is not plug-and-play. With BF16 + workarounds it works; with NVFP4 it produces garbage.
- Cold-start burst on tier_high. With
sla_warmup_seed_ms=0.0, the EMA starts at 0ms — below any SLA. The first ~15-20 complex queries all hit tier_high before the EMA reflects real latency. Setsla_warmup_seed_msto your expected p50. - This is v0.1. Functional and tested, but not battle-tested at thousands of users. It's the starting point.
Running tests
pip install -e ".[dev]"
pytest tests/ -v # No GPU required — pynvml is mocked in all fixtures
Metadata
Release files for gemma4-adaptive-router 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| gemma4_adaptive_router-0.1.1.tar.gz | 11.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| gemma4_adaptive_router-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.9 kB
Release files / gemma4_adaptive_router-0.1.1.tar.gz
| Download URL | gemma4_adaptive_router-0.1.1.tar.gz |
|---|---|
| Size | 11.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6ce9edd76eea1d54c0377b21a13f2720a3af7833201df8187434d2876ec94e87
|
|
BLAKE2b-256 checksum How to use checksums |
3c1bce862c990dc99f1fc838f61239b624573278272ee24f9c93ec8efa44a476
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 15, 2026.
Transparency logRelease files / gemma4_adaptive_router-0.1.1-py3-none-any.whl
| Download URL | gemma4_adaptive_router-0.1.1-py3-none-any.whl |
|---|---|
| Size | 3.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d49a14287af6dbd00e275a7adf70e771bd2122bd2b44390bdec48a08cba24d80
|
|
BLAKE2b-256 checksum How to use checksums |
55358d2347ccbd478f122de0b2bc5d7aaca9bdbdadc9be8538de73dcce36dfc3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 15, 2026.
Transparency log