Repository navigation
perf(ai-gateway): read model metadata once per BYOK availability pass - #8388
Merged
Merged
Conversation
…ty pass addUserByokAvailability reached the Vercel metadata map through getModelUserByokProviders for every model, and that helper enumerated the whole map's keys to test emptiness each time. Fetch the map once per pass, test emptiness once, and derive each model's providers from that map through one private helper that getModelUserByokProviders now shares. Its export, output, and per-model diagnostics are unchanged, as are the hasUserByokAvailable values, the Kilo-exclusive short circuit, and the empty-metadata result where no model reports BYOK availability. The empty-metadata error is logged once per pass instead of once per model. Add a hermetic test that pins one metadata read per pass and the per-model availability derivation (owned provider, unowned provider, model missing from the map, Kilo-exclusive model, empty map) plus the codestral prepend order.
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge The refactor is output-preserving: Files Reviewed (2 files)
Reviewed by deepseek-v4.1-flash-1017 · Input: 52.7K · Output: 12.7K · Cached: 650.1K Review guidance: REVIEW.md from base branch |
chrarnoldus
approved these changes
Oct 21, 2026
github-merge-queue
Bot
removed this pull request from the merge queue due to a conflict with the base branch
Oct 21, 2026
…nce-per-pass # Conflicts: # packages/web-shared/src/lib/ai-gateway/byok/index.ts
This was referenced Oct 22, 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.
What changed
packages/web-shared/src/lib/ai-gateway/byok/index.tsaddUserByokAvailabilityreached the Vercel model metadata map throughgetModelUserByokProvidersonce per model, and that helper enumerated the wholemap's keys (
Object.keys(vercelModelMetadata).length === 0) for every model it wasasked about. The pass therefore visited every metadata key once per model.
diagnostic, and the codestral prepend) moves into one private helper
deriveModelUserByokProviders(modelId, vercelModelMetadata)that takes thealready-fetched map.
getModelUserByokProviderskeeps its exported signature, its output, and itsper-model diagnostics; it fetches the map, keeps the empty-metadata early return,
and delegates to the helper.
addUserByokAvailabilityfetches the map once, tests emptiness once, and deriveseach model from it with a keyed lookup. The
hasUserByokAvailablevalues, theisKiloExclusiveModelshort circuit and the empty-metadata result (no model reportsBYOK availability) are unchanged. The empty-metadata error is logged once per pass
(
[addUserByokAvailability] no Vercel model metadata) instead of once per model.mainnow carries drain PR #8348, which shares the owner lookups(
getEnabledByokProviderIds,getByokRowsForOwner) and addsbyok/index.test.ts.The branch is merged with that state: those helpers stay exactly as
mainwrotethem, and only the metadata fetch, the per-model derivation helper and the
availability pass differ.
Regression test
New hermetic suite
packages/web-shared/src/lib/ai-gateway/byok/availability.test.ts(mocks
getVercelModelsMetadataFromDatabaseandmapModelIdToVercel, no databaserows) pins:
owned provider
true, unowned providerfalse, model absent from the mapfalse,Kilo-exclusive model
false, and the public model fields left unmutated;false, still one metadata read;getModelUserByokProvidersparity: deduped providers,codestralfirst, empty listfor a model missing from the map and for an empty map.
Against the previous implementation the first test fails with
Expected number of calls: 1, Received number of calls: 4.Verification
Environment: local dev stack on this VM (nextjs
:3000, ai-gateway:3010, composePostgres/Redis), owned fake account
byok-perf-20261021@example.comcreated directlyin the local database,
app:byok-e2e-fixture minimax minimax/minimax-m2.5seeding oneenabled BYOK key for
minimaxplus a Vercel metadata snapshot entry for that model,and a local API token from
app:api-token.GET /api/v1/modelswith that bearer token, after restarting ai-gateway to clear the60-second catalog and 300-second metadata caches:
hasUserByokAvailable: trueminimax/minimax-m2.5main(implementation stashed, gateway restarted)minimax/minimax-m2.5The two response bodies are identical (same ids, same order, same flags, full JSON
equality), so the pass keeps its output while reading the metadata once.
Unit suites, run on the VM with a migrated local Postgres:
openrouter-models.test.tsdrives the realaddUserByokAvailabilitythrough itsstaged-concurrency cases, so the pass still composes with the surrounding catalog
branches.
After merging
maininto the branch, the same suites plus the owner-lookup suitepackages/web-shared/src/lib/ai-gateway/byok/index.test.tsrun on the merged tree:CI on the merged head: 55 checks passed, 0 failed. The one metadata read per pass
and the unchanged availability flags still hold; the merge only adopts
main'sowner-lookup helpers.
No screenshots: the affected surface is a JSON API response, and the evidence above is
the observed response bodies.
Proxy answers
The owner's proxy gave these answers in the owner's place.
Answer: New byok/availability.test.ts, hermetic mocks
Confidence: 0.80 · Precedent: Keep CI checks that catch real errors in PRs: session 99e88001c4b4, "we want to catch errors in PRs, as early as possible"
Answer: [addUserByokAvailability] no Vercel model metadata
Confidence: 0.80 · Precedent: Autonomy: make executive decisions with "is this reasonable?" as the test (session 7f3470e0ba6f); the brief requires one log per pass
Answer: No, hoist only the metadata map
Confidence: 0.75 · Precedent: Keep each PR diff to files that the feature needs: session df491946927a, "PR should be checked for stray changes before merging"
Answer: Dev stack, /api/v1/models before vs after
Confidence: 0.85 · Precedent: Prove behavior with a real request through real clients: session 4807b1d78fc7, "you can use it to make an actual request"