Skip to main content

KLAW Router

CI PyPI Python 3.10+ License: MIT

K-104 Intelligent Model Router. Route AI queries to the cheapest model that can handle them. Up to 48x cheaper than Claude Opus — without sacrificing quality.

Install

Install from this repo (not yet on PyPI): `cd openclaw && pip install -e .`

Quick Start

from klaw import KlawRouter

router = KlawRouter()
result = router.route("How do I center a div?")

print(result["response"])    # The answer
print(result["cost"])        # $0.0001
print(result["savings"])     # $0.0029 saved vs Sonnet baseline
print(result["tier_name"])   # "template" (free)

Pass API keys directly or via environment variables:

router = KlawRouter(api_keys={
    "ANTHROPIC_API_KEY": "sk-ant-...",
    "OPENAI_API_KEY": "sk-...",
    "GEMINI_API_KEY": "AI...",
})

How It Works

Every query gets classified into a K-104 semantic address (4 domains × 13 ranks × 2 polarities = 104 rooms) and routed to the minimum capable model:

Tier Models Cost/query
0 — Template Built-in corpus (1,000+ patterns) $0.00
1 — Local Ollama, OpenRouter free tier $0.00
2 — Cheap Haiku, GPT-4o-mini, Gemini Flash ~$0.001
3 — Mid Sonnet, GPT-4o, Gemini Pro ~$0.01
4 — Premium Opus, GPT-4 ~$0.05

The classifier routes ~80% of everyday queries to tier 0–1 (free). Only genuinely complex reasoning reaches premium tiers.

Empirical result: Transformer activations cluster by K-104 suit with silhouette score 0.312 — the geometry is real, not imposed.

Claude Code MCP Integration

Add to your .mcp.json:

{
  "mcpServers": {
    "klaw": {
      "command": "klaw",
      "args": ["mcp"]
    }
  }
}

Then use klaw_route, klaw_classify, and klaw_stats tools in Claude Code.

CLI

klaw route "What is Python?"
klaw classify "Explain quantum physics"
klaw stats
klaw demo
klaw setup

API Keys

Set via environment variables:

export ANTHROPIC_API_KEY=sk-ant-...
export OPENAI_API_KEY=sk-...
export GEMINI_API_KEY=AI...
export OPENROUTER_API_KEY=sk-or-...

Or pass directly to KlawRouter(api_keys={...}). No keys required for tier 0 template routing.

Classify Only

from klaw import KlawRouter

r = KlawRouter()
c = r.classify("debug this async Python function")
print(c["suit"])       # "spades"
print(c["tier"])       # 2
print(c["k_address"])  # "+5S"

Testing

cd openclaw
pip install pytest
python -m pytest tests/ -v

All 10 tests pass without API keys (template tier is free).

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

klaw_router-0.1.0-py3-none-any.whl (31.4 kB view details)

Uploaded Python 3

File details

Details for the file klaw_router-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: klaw_router-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 31.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for klaw_router-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cab1db71287ab1e838b85264b2ebed5ead0357e34db0a1c736f03858f0615546
MD5 f511d683b015e1631558fc6b7636ca4e
BLAKE2b-256 3832366b96527db37c2b058411350746382b0e26dcfa9486dbb46299b8821884

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.2

1 file

0.1.1

1 file

This release

0.1.0 This release

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page