mastra-ai/mastra · error
Failed to initiate OpenAI Codex device authorization: ${resp
Error message
Failed to initiate OpenAI Codex device authorization: ${response.status} What it means
The device-authorization POST to OpenAI's Codex device-login endpoint returned a non-OK HTTP status. The library surfaces the raw status code because the response body may not be parseable or may not contain a useful message. This indicates the request was rejected or the endpoint is unavailable at that moment.
Source
Thrown at mastracode/sdk/src/auth/providers/openai-codex.ts:425
| { status: 'failed'; error: string };
/**
* Start a Codex device-code login: request a user code and return the
* serializable pending state for subsequent polls.
*/
export async function startCodexDeviceLogin(options?: { signal?: AbortSignal }): Promise<CodexDeviceLoginPending> {
const response = await fetch(DEVICE_USER_CODE_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': 'mastracode',
},
body: JSON.stringify({ client_id: CLIENT_ID, originator: 'mastracode' }),
signal: options?.signal,
});
if (!response.ok) {
throw new Error(`Failed to initiate OpenAI Codex device authorization: ${response.status}`);
}
const deviceData = (await response.json()) as {
device_auth_id?: string;
user_code?: string;
usercode?: string;
interval?: string | number;
};
const userCode = deviceData.user_code ?? deviceData.usercode;
if (!deviceData.device_auth_id || !userCode) {
throw new Error('OpenAI Codex device authorization response missing required fields');
}
const intervalSeconds =
typeof deviceData.interval === 'number' ? deviceData.interval : Number.parseInt(deviceData.interval ?? '', 10) || 5;
View on GitHub (pinned to 75dd419e61)
Solutions
- Retry after a short delay — 5xx and 429 are usually transient; add exponential backoff.
- Check network/proxy reachability to the OpenAI device-auth endpoint (curl the URL to see the status/body).
- Update the mastracode SDK to the latest version in case the CLIENT_ID or endpoint changed.
- If 401/403 persists, verify no org/plan restriction blocks Codex device login and contact OpenAI support.
Example fix
// before: single shot, no handling
const pending = await startCodexDeviceLogin();
// after: retry transient failures
let pending;
for (let i = 0; i < 3 && !pending; i++) {
try { pending = await startCodexDeviceLogin(); }
catch (e) {
if (!/status: (5\d\d|429)/.test(String(e))) throw e;
await new Promise(r => setTimeout(r, 1000 * 2 ** i));
}
} Defensive patterns
Strategy: retry
Validate before calling
async function deviceAuthReachable(): Promise<boolean> {
try {
const res = await fetch(TOKEN_URL, { method: 'HEAD' });
return res.status < 500;
} catch { return false; }
} Try / catch
for (let attempt = 0; attempt < 4; attempt++) {
try {
pending = await loginOpenAICodexDevice({});
break;
} catch (e) {
if (!/device authorization: \d/.test(e.message) || attempt === 3) throw e;
await new Promise(r => setTimeout(r, 1000 * 2 ** attempt));
}
} Prevention
- Back off exponentially on 429/5xx before giving up.
- Check network/proxy access to OpenAI endpoints from your environment.
- Keep the SDK updated so CLIENT_ID/endpoint stay current.
- Watch OpenAI status pages during incidents.
When it happens
Trigger: fetch to the device authorization endpoint returning 4xx/5xx (e.g. 400 bad client_id, 403 blocked, 429 rate limited, 5xx outage) during startCodexDeviceLogin, called via loginOpenAICodexDevice / loginOpenAICodex.
Common situations: OpenAI API outage or degraded service; corporate proxy/firewall stripping the request; stale CLIENT_ID after an SDK/API version change; rate limiting after repeated login attempts.
Related errors
- Token exchange failed: ${error}
- Failed to fetch user info from Auth0
- Token exchange failed: ${error}
- Failed to fetch user info from Clerk
- Google token exchange failed: ${error}
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/58635506e5d82c20.
Report an issue: GitHub.