When using the Vercel AI SDK with Workflow DevKit and vercel dev, OIDC authentication fails with:
GatewayAuthenticationError: Unauthenticated request to AI Gateway.
To authenticate, set the AI_GATEWAY_API_KEY environment variable with your API key.
This happens even when:
- Project is linked with
vercel link - OIDC token exists in
.env.localviavercel env pull - Running with
vercel dev(which should auto-refresh tokens)
The 'use workflow' directive runs code in a sandboxed VM that does NOT have access to process.env.
The authentication flow:
- AI SDK's
generateText()callsgetGatewayAuthToken() - This function checks
process.env.AI_GATEWAY_API_KEYandprocess.env.VERCEL_OIDC_TOKEN - Inside the workflow VM,
process.envis not accessible - Authentication fails before the fetch even happens
Use "use step" functions for AI SDK calls. Step functions have full Node.js runtime access.
import { generateText } from 'ai';
import { fetch } from 'workflow';
export async function myWorkflow(input: string) {
'use workflow';
// This approach doesn't work - auth happens in the sandboxed VM
globalThis.fetch = fetch as typeof globalThis.fetch;
const { text } = await generateText({
model: 'openai/o4-mini',
prompt: input,
});
}import { generateText } from 'ai';
// Step function has full Node.js access including process.env
async function generateWithAI(prompt: string) {
'use step';
const { text } = await generateText({
model: 'openai/o4-mini',
prompt,
});
return text;
}
export async function myWorkflow(input: string) {
'use workflow';
// Call the step function - auth works because step has env access
const result = await generateWithAI(input);
}| Context | process.env Access |
Use For |
|---|---|---|
'use workflow' |
❌ Sandboxed VM | Orchestration logic |
'use step' |
✅ Full Node.js | Side effects, API calls |
For simpler local development, you can use an API key instead of OIDC:
- Go to Vercel AI Gateway API Keys
- Create a new key
- Add to
.env.local:AI_GATEWAY_API_KEY=your_key_here
This works because the API key is read at runtime, and even in the sandboxed VM, explicit configuration can be passed to the gateway provider.
Discovered: January 26, 2026 Packages: [email protected], @ai-sdk/[email protected], [email protected]