{{ message }}
add ai gateway phase headers#73491
Open
alex-m-brown wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Hmm, up until now we've been pretty intentional about making the signature of the AI Gateway APIs consistent with the vercel AI SDK APIs they wrap. I'm not sure if we should start breaking away just yet, especially if we're considering moving some of the safety calls over to the gateway side anyway. Let's discuss on Slack?
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.

Sets an
x-ai-gateway-phaseheader on each AI Gateway request from the client so the worker can tag Sentry transactions with which step of the AI Chat pipeline they belong to. Companion to the gateway-side change code-dot-org/ai-gateway#41 which reads the header and attaches it as acontext.phaseattribute on the root span.Why
A single AI Chat user message produces three sequential gateway calls from the client:
isTextSafe(userInput))generateText({...}))isTextSafe(modelOutput))Plus a separate
/transcribeendpoint for voice-to-text. All four currently look identical in Sentry — same hostname, same span shape, no way to slice metrics by step. Adding a per-call phase header lets dashboards and alerts distinguish them:input_filter— user-input moderationgeneration— main model calloutput_filter— model-output moderationtranscription— speech-to-textWhat changed
apps/src/aiGateway/generateText.ts— adds an optionalphasefield to the gateway client API. When set, the value is sent as thex-ai-gateway-phaseheader.apps/src/aichat/api/client/helpers/safetyHelpers.ts—isTextSafenow takes a requiredphaseargument ('input_filter'or'output_filter') and passes it through togenerateText.apps/src/aichat/api/client/generateChatResponse.ts— passes the appropriate phase at all three gateway call sites in the AI Chat flow.apps/src/aiGateway/transcribe.ts— hardcodes'transcription'as the phase on the single transcribe call site (no API change since the value is invariant).Links
Testing story
context.phasetagged asinput_filter,generation, andoutput_filterrespectively.context.phase: transcription.USER_PROFANITY/MODEL_PROFANITYstatuses; transcribe still returns the correct text.Deployment notes
Must deploy after ai-gateway#41 ships to production. That PR allows
x-ai-gateway-phasethrough the worker's CORS preflight. If this PR ships first, every AI request from the browser fails at the preflight stage because the worker'sAccess-Control-Allow-Headersdoesn't include the new header.Safe order:
context.phase.