feat: per-provider quota measurement and credit-aware routing attenuation #40
Reference in New Issue
Block a user
Delete Branch "feat/per-provider-balance-attenuation"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What this PR does
Per-provider quota measurement (fixes NeuralWatt-only global balance/burn)
provider_balance_observationsaccount-balance table and polls any provider withbalance_urlinside the existing 2h catalog poller.balance_url: https://openrouter.ai/api/v1/credits; its real credit balance now surfaces in/metrics.metrics.quota_balance_and_burnto return per-provider balance/burn/runway underquota.by_providerplus atotal_balance_usdsum.balance_usd,burn_rate_usd_per_hour,runway_*) from/metrics— a clean break, all in-repo consumers updated.quota_burnmeters kWh only against telemetry-capable providers (avoiding emptyIN ()).Opt-in credit-aware routing attenuation
objective.credit_attenuationconfig block (enabled: falseby default).credit_attenuation_multiplierand a newrank_candidateseffective_costtiebreak: healthy providers stay at 1.0; low-balance providers get a multiplier up tomax_multiplier.balance_urlset (prepaid-account path). Telemetry-allowance-billed providers (NeuralWatt) are pinned to multiplier 1.0, so normal ~-0.004 USD overage readings never bias routing toward OpenRouter.est_cost_usdand loggedcostremain honest catalog estimates;effective_costis routing-only.refresh_seconds) and emits a structured log line when multipliers != 1.0.Reviewer note: F2 mutation-testing catch-and-fix
The initial end-to-end attenuation test priced NeuralWatt cheaper than OpenRouter, so the test "flipped" to the healthy provider even without attenuation (tautological). A final-wave reviewer temporarily removed the
provider_cost_multiplierspassthrough fromroute()and the whole suite stayed green — the wiring had zero regression coverage. This was fixed by repricing the fixture so OpenRouter is raw-cheaper and only a working passthrough produces the flip; the mutation check now fails exactly the two flip tests (assert 'openrouter' == 'neuralwatt'). Full suite remains green at 1463 tests.Verification
python -m pytest -q→ 1463 passed, 0 failures./metricsshows independent per-provider balances; attenuation probe flips selection from OpenRouter → NeuralWatt when OpenRouter credit is low.config/config.local.yamlchanges; port 8080/production untouched.Post-merge action
After merge, restart the production dispatcher:
systemctl --user restart llm-router.service.