earendil-works/pi · error

Failed to extract accountId from token

Error message

Failed to extract accountId from token

What it means

extractAccountId decodes the JWT-style Codex token and reads the chatgpt_account_id claim; any failure (malformed token, wrong segment count, missing claim) is collapsed into this single error. The account id is required for Codex routing headers, so auth cannot proceed without it.

Solutions

  1. Re-login; the token does not carry an extractable accountId claim.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at packages/ai/src/api/openai-codex-responses.ts:1589 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of earendil-works/pi@4af9d21d3b (2026-08-24). Data as JSON: /api/errors/16bbd6865aaa25ee. Report an issue: GitHub.

Appendix: source

Thrown at packages/ai/src/api/openai-codex-responses.ts:1589

	} catch {}

	return { message, friendlyMessage };
}

// ============================================================================
// Auth & Headers
// ============================================================================

function extractAccountId(token: string): string {
	try {
		const parts = token.split(".");
		if (parts.length !== 3) throw new Error("Invalid token");
		const payload = JSON.parse(atob(parts[1]));
		const accountId = payload?.[JWT_CLAIM_PATH]?.chatgpt_account_id;
		if (!accountId) throw new Error("No account ID in token");
		return accountId;
	} catch {
		throw new Error("Failed to extract accountId from token");
	}
}

function buildBaseCodexHeaders(
	initHeaders: Record<string, string> | undefined,
	additionalHeaders: ProviderHeaders | undefined,
	accountId: string,
	token: string,
): Headers {
	const headers = new Headers(initHeaders);
	for (const [key, value] of Object.entries(additionalHeaders || {})) {
		if (value === null) {
			headers.delete(key);
		} else {
			headers.set(key, value);
		}
	}
	headers.set("Authorization", `Bearer ${token}`);

View on GitHub (pinned to 4af9d21d3b)