Ultra-low-latency LLM router using a compression-distance heuristic inspired by Kolmogorov complexity.
Project description
โก Kolmo v0.0.2
The Information-Theoretic LLM Router Zero VRAM. Microsecond latency. Dictionary-based compression distance heuristic.
๐งฌ What is Kolmo?
Kolmo is a high-performance LLM router that leverages a dictionary-based compression distance heuristic inspired by Normalized Compression Distance (NCD) to classify and route prompts to the most appropriate model. No neural networks. No GPU memory. Pure computational elegance.
from kolmo import KolmoRouter
# Initialize with your fallback model
router = KolmoRouter(default_model="gpt-4o-mini")
# Train domains from HuggingFace datasets or local samples
router.add_domain(
name="code",
source="huggingface:codeparrot/github-code",
target_model="deepseek-coder"
)
router.add_domain(
name="science",
samples=[b"Photosynthesis converts light energy into...", b"Quantum mechanics states that..."],
target_model="claude-3-opus"
)
# Route in microseconds โก
result = router.route("Implement a Red-Black tree in Python")
print(result.winner) # "code"
print(result.target_model) # "deepseek-coder"
print(f"{result.latency_ms:.3f}ms") # ~0.042ms
๐ Why Kolmo?
| Feature | Traditional Routers | Kolmo |
|---|---|---|
| Latency | Milliseconds | Microseconds |
| VRAM Usage | 2-20GB | Zero ๐ฏ |
| Explainability | Black-box embeddings | Compression ratios |
| Language Support | Requires training data | Universal (byte-level) |
| Deploy Cost | GPU required | CPU-only |
| Cold Start | Model loading time | Instant |
๐ฆ Installation
pip install kolmo
Requires Python 3.10+.
๐ฎ Quick Start
Python API
from kolmo import KolmoRouter
router = KolmoRouter()
# Add domains from various sources
router.add_domain(
name="medical",
source="huggingface:medalpaca/medical_meadow_medical_flashcards",
target_model="meditron-7b",
sample_count=5000
)
# Or train from raw samples
router.add_domain(
name="legal",
samples=[b"Contract clause 1...", b"In accordance with Section 2..."],
target_model="lawgpt"
)
# Route with confidence
result = router.route("Patient presents with tachycardia and chest pain...")
print(f"Domain: {result.winner}") # "medical"
print(f"Model: {result.target_model}") # "meditron-7b"
print(f"Confidence: {result.confidence:.2%}") # 0.94
print(f"Latency: {result.latency_ms:.3f}ms") # 0.038ms
# Save as portable bundle
router.save_bundle("medical_router.kolmo")
CLI Interface
Kolmo ships with a powerful CLI for training and routing without writing code:
# Create config.json
{
"default_model": "gpt-4o-mini",
"dict_size": 112640,
"domains": [
{
"name": "code",
"source": "huggingface:codeparrot/github-code",
"target_model": "deepseek-coder",
"sample_count": 1000
},
{
"name": "general",
"samples": ["Hello world", "How are you?"],
"target_model": "gpt-4o-mini"
}
]
}
# Train your bundle
kolmo train config.json router.kolmo
# Route a prompt
kolmo route router.kolmo "Write a Python function to reverse a string"
# Get bundle info
kolmo info router.kolmo
๐ง The Science: Compression Distance Heuristic
Kolmo is inspired by Kolmogorov Complexity theory and uses a dictionary-based compression distance heuristic (not strict NCD). For context, the strict NCD definition is below.
The Foundation
Kolmogorov Complexity K(x) is the theoretical minimum information needed to represent a string x. While K(x) is uncomputable, compression provides an excellent upper bound.
Strict NCD Formula
$$ \mathrm{NCD}(x, y) = \frac{C(xy) - \min(C(x), C(y))}{\max(C(x), C(y))} $$
Where:
- C(x) = compressed size of x
- C(xy) = compressed size of x concatenated with y
- When x and y share structure โ C(xy) < C(x) + C(y)
How Kolmo Uses a Compression Heuristic
Kolmo approximates the NCD idea with zstd dictionaries by treating each dictionary as a compact "reference string":
score(prompt, dict) = (C(dict) + C(prompt | dict) - min(C(prompt), C(dict))) / max(C(prompt), C(dict))
This is a dictionary-based compression distance heuristic, not strict NCD, because it uses dictionary-conditional compression to approximate (C(xy)).
The domain with the LOWEST score wins โ the prompt compresses best with that domain's dictionary, indicating highest similarity.
๐๏ธ Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Kolmo v0.0.2 โข Router โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ Input Prompt โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โ โ zstd + Dict โ โ zstd + Dict โ โ zstd + Dict โ โ
โ โ (Code) โ โ (Medical) โ โ (Legal) โ โ
โ โโโโโโโโฌโโโโโโโโ โโโโโโโโฌโโโโโโโโ โโโโโโโโฌโโโโโโโโ โ
โ โ โ โ โ
โ โผ โผ โผ โ
โ NCD: 0.12 NCD: 0.34 NCD: 0.89 โ
โ โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ Winner: Code (lowest NCD) โ
โ โ โ
โ Route โ deepseek-coder โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ฅ v0.0.2 โข Features
โก Batch Routing
Process thousands of prompts efficiently:
prompts = [
"Write a Python function",
"Explain quantum entanglement",
"Draft a legal contract clause"
]
batch = router.route_many(prompts)
print(f"Throughput: {batch.throughput_rps:.0f} req/s")
print(f"Avg latency: {batch.avg_latency_ms:.3f}ms")
๐ฏ Thresholded Routing with Ambiguity Detection
Handle uncertain decisions gracefully:
result = router.route_thresholded(
prompt,
min_confidence=0.3, # Minimum confidence to accept
ambiguity_gap=0.05, # Gap between top candidates
max_candidates=3 # Return top-N candidates
)
if result.ambiguous:
print(f"Uncertain ({result.reason}): {result.candidates}")
# Fallback to general-purpose model
else:
model = result.target_model
๐ Pluggable Metrics Hooks
Integrate with your monitoring stack:
def prometheus_hook(metrics: RoutingMetrics):
latency.observe(metrics.latency_ms)
requests.labels(domain=metrics.winner).inc()
router = KolmoRouter(metrics_hooks=[prometheus_hook])
๐ ๏ธ Fine-Grained Controls
Tune dictionary size per domain:
router.add_domain(
name="code",
samples=code_samples,
dict_size=50000, # Custom dictionary size
)
๐ Performance
Run the reproducible benchmark script to generate the table for your hardware:
python examples/quickstart_benchmark.py --iterations 500 --domains 5
It logs CPU details, domain count, iteration count, and prompt lengths, then prints a README-ready table.
| Prompt Length | Domains | Avg Latency (ms) | p50 (ms) | p95 (ms) | Throughput (req/s) | Accuracy |
|---|---|---|---|---|---|---|
| (run script) | 5 | (run script) | (run) | (run) | (run script) | (run) |
Memory footprint: ~50KB per domain dictionary
๐ Language Agnostic
Kolmo operates at the byte level using statistical compression:
- โ English, Spanish, Mandarin, Japanese...
- โ Python, Rust, Go, C++, JavaScript...
- โ Log files, JSON, XML, Protocol Buffers...
- โ Mixed-domain prompts
No tokenization. No language-specific training.
๐ Advanced Examples
Coding vs. Reasoning Router
router = KolmoRouter()
router.add_domain(
"coding",
source="huggingface:codeparrot/github-code",
target_model="deepseek-coder"
)
router.add_domain(
"reasoning",
source="huggingface:sci_q",
target_model="deepseek-r1"
)
# Coding prompt โ deepseek-coder
res = router.route("Write a FastAPI endpoint for user login")
# Reasoning prompt โ deepseek-r1
res = router.route("If 3x + 5 = 20, what is x?")
Language Detection
router.add_domain("en", samples=english_samples, target_model="gpt-4o")
router.add_domain("es", samples=spanish_samples, target_model="gpt-4o-es")
res = router.route("Hola, ยฟcรณmo estรกs?")
print(res.winner) # "es"
Using Pre-trained Dictionaries
# Load a pre-trained zstd dictionary
dict_bytes = open("medical.dict", "rb").read()
router.add_domain(
name="medical",
dict_bytes=dict_bytes,
target_model="meditron-7b"
)
๐งช API Reference
KolmoRouter
| Method | Description |
|---|---|
add_domain(name, ...) |
Add a routing domain |
route(prompt) |
Route a single prompt |
route_thresholded(prompt, ...) |
Route with ambiguity detection |
route_many(prompts) |
Batch routing with aggregate stats |
save_bundle(path) |
Save router to .kolmo file |
load_bundle(path) |
Load router from .kolmo file |
CLI Commands
kolmo train <config.json> <output.kolmo> # Train a bundle
kolmo route <bundle.kolmo> <prompt> # Route a prompt
kolmo info <bundle.kolmo> # Show bundle info
๐บ๏ธ Roadmap
โ Implemented (Current)
- Batch routing with throughput stats
- Thresholded routing with ambiguity detection
- CLI tooling (train, route, info)
- Pluggable metrics hooks
- Fine-grained dictionary sizing controls
๐ Next Steps (High Priority)
- Dictionary optimization and pruning tools
- Config-driven domain registry (YAML/JSON)
- Comprehensive benchmark suite
- Caching layer for repeated prompts
- Improved dataset ingestion with progress tracking
๐บ๏ธ Future Horizons (Backlog)
- Hierarchical multi-stage routing
- Adaptive dictionary refresh
- REST/gRPC service layer
- SIMD-optimized compression kernels
๐ค Contributing
We welcome contributions! Kolmo is in active development.
git clone https://github.com/charlybgai/kolmo
cd kolmo
uv sync --frozen
uv tool install pre-commit
pre-commit install
๐ License
MIT License โ See LICENSE for details.
๐ Acknowledgments
Kolmo is inspired by research in:
- Normalized Compression Distance (Li et al.)
- The Similarity Metric (Li et al., 2004)
- Modern zstd dictionary training techniques (Facebook)
Built by Carlos Bustamante & KODEX Information theory meets production inference. โก
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kolmo-0.0.2.tar.gz.
File metadata
- Download URL: kolmo-0.0.2.tar.gz
- Upload date:
- Size: 21.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
808fdd36a7d028da69063d9dc80d502e281c19c7512ce215ec75fc6b5693b79c
|
|
| MD5 |
04e90b6118a1c1dda9d239de5d607898
|
|
| BLAKE2b-256 |
7785edae8a5d70cc11bb82c3648256b61443a9a529d40766a3231a717ffa305a
|
Provenance
The following attestation bundles were made for kolmo-0.0.2.tar.gz:
Publisher:
release.yml on charlybgai/kolmo
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kolmo-0.0.2.tar.gz -
Subject digest:
808fdd36a7d028da69063d9dc80d502e281c19c7512ce215ec75fc6b5693b79c - Sigstore transparency entry: 917184082
- Sigstore integration time:
-
Permalink:
charlybgai/kolmo@6b8a0d1b474639ed780083c11691645c475bb725 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/charlybgai
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@6b8a0d1b474639ed780083c11691645c475bb725 -
Trigger Event:
push
-
Statement type:
File details
Details for the file kolmo-0.0.2-py3-none-any.whl.
File metadata
- Download URL: kolmo-0.0.2-py3-none-any.whl
- Upload date:
- Size: 14.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
323ae12dbd61e6497910f1ca46e38db583d9aaeb53b1e229536e1c87adfd4010
|
|
| MD5 |
a229934b2acec21ab7748dbca7ee21fb
|
|
| BLAKE2b-256 |
58b2546fed339a870cd035f4b82ca1bf076ee648c4b987879b6d0a695ac76f6f
|
Provenance
The following attestation bundles were made for kolmo-0.0.2-py3-none-any.whl:
Publisher:
release.yml on charlybgai/kolmo
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kolmo-0.0.2-py3-none-any.whl -
Subject digest:
323ae12dbd61e6497910f1ca46e38db583d9aaeb53b1e229536e1c87adfd4010 - Sigstore transparency entry: 917184167
- Sigstore integration time:
-
Permalink:
charlybgai/kolmo@6b8a0d1b474639ed780083c11691645c475bb725 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/charlybgai
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@6b8a0d1b474639ed780083c11691645c475bb725 -
Trigger Event:
push
-
Statement type: