feat: daily budget throttling with rate-aware proxy sleep#469
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a configurable daily USD spending cap to the Lore gateway. When the budget is approached, the gateway applies an invisible proxy-level sleep before forwarding requests upstream — progressively slowing agents rather than hard-blocking them.
How It Works
Set budget via the UI dashboard (
/ui/costs) or env varLORE_DAILY_BUDGET. Persisted in SQLitekv_metatable — survives restarts, easy to tune.Rate detection: Cost-rate EMA (α=0.15) tracks spending velocity across all sessions. Time-gap-adjusted alpha means long idle gaps naturally decay the EMA.
Throttle curve:
60s × pressure² × tanh(overshoot/3)— smooth, C∞ continuous. Starts at sub-second delays at 60% budget, maxes at 60s under extreme overshoot. No cliff edges.Invisible proxy sleep: The agent doesn't know it's being throttled — the upstream just appears to take a little longer. A 3-5s sleep is imperceptible during a normal coding turn.
Cache TTL safety cap: Sleep is capped at 50% of remaining cache TTL window to prevent cache busts that would increase costs.
Throttle Behavior
Configuration
/ui/costs)LORE_DAILY_BUDGET=10.00(overrides DB value)Dashboard
What's NOT Throttled
Files Changed
packages/gateway/src/cost-tracker.tspackages/gateway/src/pipeline.tsforwardToUpstream()with cache TTL safety cappackages/gateway/src/server.tspackages/gateway/src/ui.tspackages/gateway/test/budget-throttle.test.tsNo new files (except test). No DB schema changes. No core package changes.