Repository navigation
[AISAGE-737] perf(ai-gateway): read the replica before inference - #8073
Merged
Merged
Conversation
Gateway functions also run in sfo1, but several pre-inference reads used the Frankfurt primary: the org model-access policy transaction on every org request, the ChatGPT connection lookup, balance lookups in three handlers, and the 60s config caches. Route them through readDb and document the rule in the web-shared and ai-gateway AGENTS.md files.
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Executive SummaryThe replica-routing change is consistent: every gateway pre-inference caller of the org policy, balance, ChatGPT connection, and gateway config helpers now passes Files Reviewed (31 files)
Reviewed by deepseek-v4.1-flash-1017 · Input: 118.9K · Output: 35K · Cached: 3.9M Review guidance: REVIEW.md from base branch |
# Conflicts: # packages/web-shared/src/lib/ai-gateway/auto-routing-denied-models.test.ts # packages/web-shared/src/lib/ai-gateway/auto-routing-denied-models.ts # packages/web-shared/src/lib/organizations/organization-models.ts
johnnyeric
approved these changes
Oct 19, 2026
This was referenced Oct 19, 2026
This was referenced Oct 19, 2026
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
kilocode-ai-gatewayruns infra1andsfo1, butdbalways connects to the Frankfurt primary. Several reads that happen before the upstream inference request useddb, sosfo1requests paid transatlantic round trips before the provider was called. These now usereadDb, which is the US replica insfo1(POSTGRES_REPLICA_US_URLis set on the gateway's production project).getOrganizationGroupPolicyContextran a read-onlyrepeatable readtransaction on the primary: BEGIN, up to four SELECTs, COMMIT. It now takes an optionalfromDb. Gateway callers passreadDb: LLM proxy, FIM, edit, embeddings, audio, systemone, models, models-by-provider, embedding-models, transcription-models, and the auto-routing denied-models route. Web callers keep the primary default.getBalanceAndOrgSettingswithout a client, so the org balance row was read from the primary. They now passreadDb, as the LLM proxy, FIM and edit already did.FOR UPDATE.packages/web-shared/AGENTS.mdhas a new "AI gateway database access" section: no primary reads, transactions, locks or writes before inference; passreadDbexplicitly because helpers default todb; keepPOSTGRES_REPLICA_US_URLset.apps/ai-gateway/AGENTS.mdpoints to it.ENVIRONMENT.mdnow documents the replica variables.Not changed, and intentionally still on the primary before inference:
isAutoTopUpInFlight: read-after-write, and only on the out-of-credits path, which returns an error and never reaches inference.Verification
vercel env lsthatPOSTGRES_REPLICA_US_URLis set for production onkilocode-ai-gateway.readDbis the primary in development), so the latency effect can only be observed insfo1production traces.Visual Changes
N/A
Reviewer Notes
fra1,readDbnow spreads these reads across the EU replicas instead of the primary.