free-best-router — OpenAI-compat routing layer for DSH across 7 free providers (52 tests, 7-component scoring, Wilson + Bayesian, bounded fallback) #5508
MajidAsghariTabrizi
started this conversation in
Show Your Plugins!
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
What it is
free-best-router is a local Node.js process (5 source files, ~80 KB, one dep —
undici) that exposes one OpenAI-compatible endpoint (http://127.0.0.1:4098/v1, logical modelfree-best) and continuously picks the best currently-usable free model across 7 providers: OpenRouter, OpenCode/Zen, Groq Cloud, Cerebras Inference, Mistral La Plateforme, DeepSeek, and local Ollama / LM Studio / llama.cpp. DSH treats it as one provider block; the router handles everything else.It is not a native DSH plugin. It is a routing layer that DSH can call as an OpenAI-completions provider.
Why I built it
The free-model layer under DSH has properties that a model registry doesn't capture:
If an agent calls a specific free model by name, it inherits all of that. The router sits above providers so the agent never has to know which free model is healthy at 3am.
How it works
Hard requirements (must all pass): free, provider reachable,
code_generation,tool_calling;visionwhen images are present;long_contextfor repo-wide tasks.Total score (
src/classify.mjs):reliabilityis multiplied byproviderMultiplier(1.0 healthy, 0.5 degraded, 0 cooldown),finishCompat, and a confidence-adjusted raw rate:< minSamples(default 5) -> Bayesian shrinkage toward 0.5 prior>= minSamples-> Wilson lower bound (z=1.96)So a 1/1 model can't outrank a 200/205.
Time-decay penalties (configurable halflives) subtract from
totalfor recent 429 / timeout / 5xx / tool-fail / structure-fail / finish-error events.DeepSeek Harness integration
This is the part DSH maintainers will care about. It is a config block, not a plugin:
DSH's
llm-pi-aiopenai-completions adapter hits127.0.0.1:4098/v1/chat/completions. The router selects a concrete model, preserves the upstream response'smodelfield (so DSH captures the actual picked model intoresponseModel), and exposes the same value via thex-free-router-modelresponse header.A complete worked example is at
examples/deepseek-harness/settings.yaml.Reliability / fallback behaviour
model_not_found: 30-min base, capped at 1 h - lets the free-tier cycle in/out naturally instead of hammeringRetry-After(from the soonest-to-recover cooldown, clamped to [1, 600] s) andx-free-router-next-eligible(the model that will be eligible first)maxDepth: 4xattemptTimeoutMs: 45_000= 180 s maximum per requestSmart exploration
5% of requests probe a healthy, capability-close runner-up (not the incumbent) so the winner doesn't ossify. The pool is restricted to: free-only, healthy-state, and either (a) within
exploreGap: 0.15of the incumbent score or (b) under-sampled (requests < minSamples) and capability-close. Exploration outcomes are tracked separately so we can verify the policy is doing what it claims.What's verified and what's not
node --testand runs in CI via.github/workflows/ci.yml.src/andproviders/shows the only outbound traffic is to configured providerbaseUrls. No analytics SDK, no metrics endpoint, no third-party call.engine.diagnostics()asserts that nosk-...,OPENROUTER_OX,OPENCODE_ZEN, orDEEPSEEK_APIstring ever appears in the diagnostics JSON.hardEligible(model, task)returns{ ok: false, reason: "paid" }ifmodel.freeis false. Tested.~/.storages/free-router/state.jsonand restore on boot) is the next change. Calling it out so people who use this in production know.llm-pi-aiRetry-After behaviour (see [Bug] dsh-llm-pi-ai ignores Retry-After when retrying — TPM-limited retries always fail #3128) means the router's 502/429 hints currently can't be read by DSH's retry loop without a small adapter change. Happy to send a PR for that.Repository
https://github.com/MajidAsghariTabrizi/free-best-router
README.md- full quick-start, API reference, DSH configdocs/architecture.md- engine internals, scoring math, cooldown state machinedocs/providers.md- per-provider notes (rate limits, free-tier caveats)examples/curl/,examples/openai-python/,examples/javascript/,examples/deepseek-harness/Feedback requested
dsh-quota-router/dsh-shift-router/dsh-zen-proxy/dsh-polyglot/dsh-adaptive-model-router- would love to know whether they compose cleanly or compete.model_not_foundcooldown - is that too long in practice?free-routerprovider block be a useful worked example in the self-hosted-gateways proposal (Proposal: make self-hosted gateways first-class providers in deepseek-harness (split `packages/llm`) #2529)?All reactions