{"record":{"id":"7e7c08133af4500c","repo":"decolua/9router","slug":"zed-did-not-return-an-llm-token","errorCode":null,"errorMessage":"Zed did not return an LLM token","messagePattern":"Zed did not return an LLM token","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"open-sse/shared/zedAuth.js","lineNumber":286,"sourceCode":"    \"Content-Type\": \"application/json\",\n    Accept: \"application/json\",\n    Authorization: buildZedUserAuthHeader(credentials),\n  };\n  const systemId = getSystemId(credentials);\n  if (systemId) headers[ZED_HEADERS.systemId] = systemId;\n\n  const data = await fetchJson(\n    zedUrl(config, \"cloudBaseUrl\", \"/client/llm_tokens\", ZED_CLOUD_BASE_URL),\n    {\n      method: \"POST\",\n      headers,\n      body: JSON.stringify({ organization_id: organizationId }),\n      signal: options.signal ?? undefined,\n    },\n  );\n  const token =\n    typeof data?.token === \"string\" ? data.token : data?.token?.[0] || data?.token?.value;\n  if (!token) throw new Error(\"Zed did not return an LLM token\");\n  llmTokenCache.set(cacheKey, { token, expiresAt: Date.now() + LLM_TOKEN_TTL_MS });\n  return token;\n}\n\nexport function shouldRefreshZedLlmToken(response) {\n  return (\n    response?.status === 401 ||\n    !!response?.headers?.has?.(ZED_HEADERS.expiredToken) ||\n    !!response?.headers?.has?.(ZED_HEADERS.outdatedToken)\n  );\n}\n\nexport async function zedLlmFetch(credentials, path, options = {}) {\n  const config = options.config || {};\n  const url = zedUrl(config, \"llmBaseUrl\", path, ZED_LLM_BASE_URL);\n  const buildRequest = async (forceRefresh) => {\n    const token = await fetchZedLlmToken(credentials, { ...options, forceRefresh });\n    return proxyAwareFetch(url, {","sourceCodeStart":268,"sourceCodeEnd":304,"githubUrl":"https://github.com/decolua/9router/blob/90b52e06ffd666b7929554211474d01588f6b1f8/open-sse/shared/zedAuth.js#L268-L304","documentation":"fetchZedLlmToken POSTs to Zed's LLM-token endpoint with the organization id and extracts the token from the response, accepting `token` as a string, or as an object/array with `token[0]` or `token.value`. If none of these shapes yield a non-empty token it throws \"Zed did not return an LLM token\".","triggerScenarios":"The Zed LLM-token endpoint responds 200 but with an empty/malformed body — e.g. `{\"token\":[]}`, `{}`, or a body whose `token` field is null — typically when the org has no LLM access or the endpoint contract changed.","commonSituations":"Organization lacks LLM entitlement/seat; Zed API version changed the response shape (token now under a different key); proxy or captive portal returned a 200 HTML page parsed as {raw:...}.","solutions":["Log the raw response body and check which shape `token` actually has; if it moved to a new key, adjust the extraction.","Confirm the selected organization has LLM/active-seat access on Zed cloud; try another organizationId.","Re-authenticate (forceRefresh or new sign-in) — an expired session can yield an empty success response.","Check the response isn't an HTML/error page being JSON-parsed into {raw: text}; inspect Content-Type."],"exampleFix":"// before\nconst data = { token: null }; // server returned empty\nif (!token) throw ...\n// after — verify upstream entitlement, or extract from alternate shape:\nconst token = data?.token?.value || data?.token?.[0] || data?.llm_token;","handlingStrategy":"fallback","validationCode":"// can't validate pre-call; validate post-call instead:\nif (res.ok) {\n  const data = await res.json();\n  const token = data?.token?.value || (Array.isArray(data?.token) ? data.token[0] : data?.token) || data?.llm_token;\n  if (!token) throw new Error(`Unexpected LLM token payload: ${JSON.stringify(data).slice(0, 200)}`);\n}\n","typeGuard":"const hasLlmToken = (d) => typeof d?.token === \"string\"\n  || (Array.isArray(d?.token) && typeof d.token[0] === \"string\")\n  || typeof d?.token?.value === \"string\" || typeof d?.llm_token === \"string\";","tryCatchPattern":"try {\n  const token = await fetchZedLlmToken(credentials, { organizationId });\n} catch (e) {\n  if (e.message === \"Zed did not return an LLM token\") {\n    // check org entitlement / forceRefresh / inspect raw body, then retry once\n  } else throw e;\n}","preventionTips":["Confirm the organization has active LLM seats before requesting tokens.","Log raw response bodies for the token endpoint to catch contract changes early.","Force re-auth when a 200 response yields an empty token — it usually means a stale session."],"tags":["zed","oauth","api-response","network"],"backgroundTag":"unexpected-api-response","analyzedSha":"90b52e06ffd666b7929554211474d01588f6b1f8","analyzedAt":"2026-08-30T21:05:45.952Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}