Repository navigation
[AISAGE-735] feat(ai-gateway): send explicit session ID and affinity to Vercel - #8016
Merged
Merged
Conversation
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Executive SummaryThe follow-up commit only adds the Vercel provider to the messages-request Files Reviewed (2 files)
Previous Review Summary (commit 0061e96)Current summary above is authoritative. Previous snapshots are kept for context only. Previous review (commit 0061e96)Status: No Issues Found | Recommendation: Merge Files Reviewed (5 files)
Reviewed by deepseek-v4.1-flash-1017 · Input: 39.1K · Output: 3.2K · Cached: 323.3K Review guidance: REVIEW.md from base branch |
johnnyeric
approved these changes
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
Every request routed to Vercel AI Gateway now sets
providerOptions.gateway.sessionIdto the conversation's existing ID and setssessionAffinity: true. This happens in both branches ofapplyVercelSettings: user BYOK andconvertProviderOptions.Until now we sent no session ID. Vercel then fell back to
prompt_cache_key, which pins only the provider and not the credential, or, on Anthropic Messages, to a fingerprint of the conversation. With an explicit ID, Vercel pins the exact credential that served the session's last successful request and puts it ahead of ourorder. That keeps the prompt cache warm after a fallback, for example from Vertex to Bedrock. See the session affinity docs.Where the session ID comes from (no new ID is created):
body.prompt_cache_key.applyTrackingIdsalready sets it to the user- and task-scoped hashgenerateProviderSpecificSessionHash(userId, taskId, vercel). If there is no task ID, it falls back to the client's own key, andapplyTrackingIdshashes keys longer than 64 characters.body.session_id. Until nowapplyTrackingIdsfilled this field, and the hasheduser, for OpenRouter only. It now does the same for Vercel, so Messages requests carry the same session hash and user hash as the other request kinds. chat_completions and responses already sentuserto every provider.sessionIdis left unset.sessionAffinity: trueis still sent.VercelProviderConfig['gateway']now declaressessionId?: stringandsessionAffinity?: boolean. TheGatewayProviderOptionstype in@ai-sdk/gateway@4.0.21accepts them only through its[key: string]: unknownindex signature. Theorder/onlylogic is unchanged.Verification
I sent live requests to
https://ai-gateway.vercel.sh/v1with our production key. The bodies were built by hand to match what the new code produces; they did not go through the gateway route itself.anthropic/claude-sonnet-4.5,order/only: [vertexAnthropic, bedrock], explicitsessionIdandsessionAffinity: true. To force the fallback, request 1 set a 1000 ms BYOK timeout onvertexAnthropic.vertexAnthropicfailed andbedrockserved it.clientSessionIdSource: "explicit",affinity.outcome: "no_pin".order):bedrockserved both.clientSessionIdSource: "explicit",affinity.outcome: "promoted",pinnedProvider: "bedrock",displacedProvider: "vertexAnthropic"."confirmed"withpinnedProvider: "vertexAnthropic".useron Messages does not break requests. All returned 200:claude-haiku-4.5pinned to each ofanthropic(BYOK),bedrock(BYOK),vertexAnthropic(BYOK) andclaudeaws(system).openai/gpt-5-mini,moonshotai/kimi-k2(served by novita) andgoogle/gemini-2.5-flash-lite(served by vertex).prompt_cache_keyplusproviderOptions.gateway.sessionIdreturn 200 withclientSessionIdSource: "explicit".Automated: all
packages/web-shared/src/lib/ai-gatewayJest suites pass (104 suites, 1977 tests), and so dopnpm --filter @kilocode/web-shared typecheckandoxlint.Visual Changes
N/A
Reviewer Notes
order, but only among providers ouronly/ZDR filters already allow, and never across the BYOK/system-credential boundary. If the pinned provider fails, Vercel removes the pin and falls back as before, so genuine failures take no longer to surface.x-kilocode-taskid, a client-suppliedprompt_cache_key(64 characters or fewer) now becomes an explicit session ID, so Vercel pins the credential and not just the provider. Messages requests already forwarded a client-suppliedsession_idto Vercel as the top-level field.