{"record":{"id":"73bf8cc383e68b92","repo":"thedotmack/claude-mem","slug":"invalid-api-key-check-your-api-key-configuration","errorCode":null,"errorMessage":"Invalid API key: check your API key configuration in ~/.claude-mem/settings.json or ~/.claude-mem/.env","messagePattern":"Invalid API key: check your API key configuration in ~/\\.claude-mem/settings\\.json or ~/\\.claude-mem/\\.env","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/services/worker/ClaudeProvider.ts","lineNumber":470,"sourceCode":"            continue;\n          }\n\n          const discoveryTokens = (session.cumulativeInputTokens + session.cumulativeOutputTokens) - discoveryTokenBaseline;\n\n          const originalTimestamp = session.earliestPendingTimestamp;\n\n          if (responseSize > 0) {\n            const truncatedResponse = responseSize > 100\n              ? textContent.substring(0, 100) + '...'\n              : textContent;\n            logger.dataOut('SDK', `Response received (${responseSize} chars)`, {\n              sessionId: session.sessionDbId,\n              promptNumber: session.lastPromptNumber\n            }, truncatedResponse);\n          }\n\n          if (typeof textContent === 'string' && textContent.includes('Invalid API key')) {\n            throw new Error('Invalid API key: check your API key configuration in ~/.claude-mem/settings.json or ~/.claude-mem/.env');\n          }\n\n          await processAgentResponse(\n            textContent,\n            session,\n            this.dbManager,\n            this.sessionManager,\n            worker,\n            discoveryTokens,\n            originalTimestamp,\n            'SDK',\n            cwdTracker.lastCwd,\n            modelId,\n            activeResponseContext.current\n          );\n\n          discoveryTokenBaseline = session.cumulativeInputTokens + session.cumulativeOutputTokens;\n          turnDispatchedText = true;","sourceCodeStart":452,"sourceCodeEnd":488,"githubUrl":"https://github.com/thedotmack/claude-mem/blob/d8bc9755e74915e5c3b999181e10a67c889bce2a/src/services/worker/ClaudeProvider.ts#L452-L488","documentation":"ClaudeProvider.startSession inspects the agent's text response and, if it contains 'Invalid API key', converts it into a clear, actionable error pointing at the config files (~/.claude-mem/settings.json or ~/.claude-mem/.env). The underlying Claude CLI/SDK rejected the configured Anthropic API key.","triggerScenarios":"Running a worker session via the Claude provider when the ANTHROPIC_API_KEY (or configured key) is missing, empty, revoked, or mistyped, so the model response text contains 'Invalid API key'.","commonSituations":"Fresh machine install with no .env populated; key rotated/revoked upstream; key copied with whitespace or quotes; switching between project and global settings with conflicting keys.","solutions":["Set a valid Anthropic API key in ~/.claude-mem/.env (ANTHROPIC_API_KEY) or ~/.claude-mem/settings.json.","Verify the key on the provider console — regenerate if revoked.","Check for stray whitespace/quotes around the key value.","Restart the worker after updating config so the new key is loaded."],"exampleFix":"// before (~/.claude-mem/.env)\n# ANTHROPIC_API_KEY=\n// after\nANTHROPIC_API_KEY=sk-ant-...your-valid-key...","handlingStrategy":"validation","validationCode":"const key = process.env.ANTHROPIC_API_KEY;\nif (!key || !key.startsWith('sk-ant-')) throw new Error('Configure ANTHROPIC_API_KEY in ~/.claude-mem/.env');","typeGuard":null,"tryCatchPattern":"try { await provider.startSession(session); } catch (e) { if (e.message.includes('Invalid API key')) { /* prompt user to fix ~/.claude-mem/.env */ } }","preventionTips":["Populate ~/.claude-mem/.env during install with a valid key.","Validate key format before starting sessions.","Re-test auth after any key rotation.","Keep a single source of truth for the key to avoid conflicting configs."],"tags":["api-key","authentication","config"],"backgroundTag":"missing-api-key","analyzedSha":"d8bc9755e74915e5c3b999181e10a67c889bce2a","analyzedAt":"2026-09-17T16:40:26.182Z","contentChangedAt":"2026-09-17T16:40:26.182Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}