DOSRouter

module
v0.13.0-go Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2026 License: MIT

README

DOSRouter

High-performance LLM router written in Go. Routes requests to the optimal model based on prompt complexity, cost targets, and capability requirements.

Go port of ClawRouter (TypeScript) - rewritten for lower latency and easier deployment.

Production-proven at DOS.AI - powering the DOS.AI inference API that serves thousands of LLM requests daily with automatic model selection, cost optimization, and multi-provider failover.

Features

  • 15-dimension weighted scorer - classifies prompts in ~0.04ms (40us) without any LLM call
  • Tier-based routing - SIMPLE / MEDIUM / COMPLEX / REASONING with configurable model pools
  • Cost optimization - automatic savings calculation vs baseline (opus-class) pricing
  • Multi-provider failover - primary + fallback providers per model
  • Agentic detection - identifies tool-calling patterns, routes to capable models
  • Session pinning - maintains model consistency within conversations
  • Streaming proxy - OpenAI-compatible SSE proxy with cost breakdown injection
  • 55+ models - built-in catalog with pricing, capabilities, and aliases
  • Zero dependencies for core routing - only stdlib

Architecture

router/          Core routing logic
  types.go       Type definitions (Tier, ScoringResult, RoutingDecision, etc.)
  config.go      Default config with multilingual keywords (9 languages)
  rules.go       15-dimension weighted scorer (<0.04ms per classification)
  selector.go    Tier -> Model selection with cost/savings calculation
  strategy.go    Strategy pattern + promotions + agentic detection
  llm_classifier.go  LLM fallback for ambiguous requests
  router.go      Entry point (Route function)

models/          Model catalog
  models.go      55+ model definitions, aliases, pricing, capabilities

proxy/           OpenAI-compatible proxy
  proxy.go       HTTP server with smart routing, SSE streaming, /debug endpoint

session/         Session management
  session.go     Conversation pinning with TTL and explicit user overrides

cmd/dosrouter/   CLI
  main.go        serve, classify, models commands

15-Dimension Weighted Scorer

# Dimension Weight Detection
1 reasoningMarkers 0.18 "prove", "theorem", "step by step"
2 codePresence 0.15 "function", "class", "import", code blocks
3 multiStepPatterns 0.12 regex: first.*then, step \d, \d.\s
4 technicalTerms 0.10 "algorithm", "kubernetes", "database"
5 tokenCount 0.08 <50 tokens=-1.0, >500=+1.0
6 creativeMarkers 0.05 "story", "poem", "brainstorm"
7 questionComplexity 0.05 >3 question marks
8 agenticTask 0.04 "edit", "deploy", "debug", "step 1"
9 constraintCount 0.04 "at most", "O(n)", "limit"
10 imperativeVerbs 0.03 "build", "create", "implement"
11 outputFormat 0.03 "json", "yaml", "table"
12 simpleIndicators 0.02 "what is", "hello" (negative!)
13 referenceComplexity 0.02 "the code above", "the docs"
14 domainSpecificity 0.02 "quantum", "FPGA", "genomics"
15 negationComplexity 0.01 "don't", "avoid", "never"

Tier boundaries: SIMPLE < 0.0 < MEDIUM < 0.3 < COMPLEX < 0.5 < REASONING

Confidence: Sigmoid calibration 1/(1+exp(-12*distance)), threshold 0.7

Quick Start

CLI
# Classify a prompt
go run ./cmd/dosrouter classify "hello world"
go run ./cmd/dosrouter classify "Prove the Riemann hypothesis step by step"

# Start proxy server
go run ./cmd/dosrouter serve --port 8080 --upstream https://api.example.com --api-key sk-xxx

# List models
go run ./cmd/dosrouter models
As a Library
import (
    "github.com/DOS/DOSRouter/router"
    "github.com/DOS/DOSRouter/models"
)

config := router.DefaultRoutingConfig()
pricing := models.BuildPricingMap()

decision, err := router.Route(
    "Write a distributed system with kubernetes",
    "",    // system prompt
    4096,  // max output tokens
    router.RouterOptions{
        Config:         config,
        ModelPricing:   pricing,
        RoutingProfile: "auto", // "auto", "eco", "premium"
    },
)

fmt.Printf("Model: %s, Tier: %s, Savings: %.0f%%\n",
    decision.Model, decision.Tier, decision.Savings*100)

Build & Test

go build ./...
go test ./router/ -v
go test ./router/ -bench=. -benchmem

Performance

  • Classification: ~0.04ms per request (40us)
  • Zero external dependencies for routing
  • LLM fallback only for ambiguous cases (~20-30%)

Upstream Sync

This is a Go port of BlockRunAI/ClawRouter. Routing logic is synced periodically from upstream releases. Payment, plugin lifecycle, and CLI-specific features are excluded. See UPSTREAM_SYNC.md for details.

Current sync: v0.12.146

License

MIT

Directories

Path Synopsis
Package cache provides a TTL + LRU response cache for LLM completions.
Package cache provides a TTL + LRU response cache for LLM completions.
cmd
dosrouter command
DOSRouter CLI - Smart LLM Router
DOSRouter CLI - Smart LLM Router
Package dedup provides request deduplication for LLM proxy requests.
Package dedup provides request deduplication for LLM proxy requests.
Package errors provides typed error types for blockchain wallet and RPC failures in the DOSRouter system.
Package errors provides typed error types for blockchain wallet and RPC failures in the DOSRouter system.
Package excludemodels manages a user-configurable list of excluded models persisted at ~/.dosrouter/exclude-models.json.
Package excludemodels manages a user-configurable list of excluded models persisted at ~/.dosrouter/exclude-models.json.
Package journal provides per-session memory of key actions extracted from assistant responses, enabling context recall when users ask about earlier work.
Package journal provides per-session memory of key actions extracted from assistant responses, enabling context recall when users ask about earlier work.
Package logger provides append-only JSONL usage logging to daily files under ~/.dosrouter/logs/.
Package logger provides append-only JSONL usage logging to daily files under ~/.dosrouter/logs/.
Package models provides model definitions, aliases, and pricing for the DOSRouter smart LLM routing system.
Package models provides model definitions, aliases, and pricing for the DOSRouter smart LLM routing system.
Package partners provides partner service definitions and tool builders for integrating external data APIs into the DOSRouter system.
Package partners provides partner service definitions and tool builders for integrating external data APIs into the DOSRouter system.
Package payment implements the x402 micropayment protocol for DOSRouter.
Package payment implements the x402 micropayment protocol for DOSRouter.
Package proxy implements an OpenAI-compatible HTTP proxy server with smart routing.
Package proxy implements an OpenAI-compatible HTTP proxy server with smart routing.
Package retry provides HTTP request execution with exponential backoff.
Package retry provides HTTP request execution with exponential backoff.
Package router provides smart LLM request routing with 15-dimension weighted scoring and sigmoid confidence calibration.
Package router provides smart LLM request routing with 15-dimension weighted scoring and sigmoid confidence calibration.
Package session provides in-memory session persistence with timeout-based expiry, three-strike escalation, and per-session cost tracking.
Package session provides in-memory session persistence with timeout-based expiry, three-strike escalation, and per-session cost tracking.
Package spendcontrol enforces spending limits for LLM requests using rolling time windows and per-session budgets.
Package spendcontrol enforces spending limits for LLM requests using rolling time windows and per-session budgets.
Package stats parses JSONL usage log files and computes aggregate statistics with ASCII-formatted output.
Package stats parses JSONL usage log files and computes aggregate statistics with ASCII-formatted output.
Package wallet manages EVM-compatible wallets for DOSRouter payment.
Package wallet manages EVM-compatible wallets for DOSRouter payment.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL