thedotmack/claude-mem · error

Invalid API key: check your API key configuration in…

Error message

Invalid API key: check your API key configuration in ~/.claude-mem/settings.json or ~/.claude-mem/.env

What it means

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.

Solutions

  1. Set a valid Anthropic API key in ~/.claude-mem/.env (ANTHROPIC_API_KEY) or ~/.claude-mem/settings.json.
  2. Verify the key on the provider console — regenerate if revoked.
  3. Check for stray whitespace/quotes around the key value.
  4. Restart the worker after updating config so the new key is loaded.

Example fix

// before (~/.claude-mem/.env)
# ANTHROPIC_API_KEY=
// after
ANTHROPIC_API_KEY=sk-ant-...your-valid-key...
Defensive patterns

Strategy: validation

Validate before calling

const key = process.env.ANTHROPIC_API_KEY;
if (!key || !key.startsWith('sk-ant-')) throw new Error('Configure ANTHROPIC_API_KEY in ~/.claude-mem/.env');

Try / catch

try { await provider.startSession(session); } catch (e) { if (e.message.includes('Invalid API key')) { /* prompt user to fix ~/.claude-mem/.env */ } }

Prevention

When it happens

Trigger: 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'.

Common situations: 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.

Understand the failure class

Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.

Related errors


AI-assisted analysis of thedotmack/claude-mem@d8bc9755e7 (2026-09-17). Data as JSON: /api/errors/73bf8cc383e68b92. Report an issue: GitHub.

Appendix: source

Thrown at src/services/worker/ClaudeProvider.ts:470

            continue;
          }

          const discoveryTokens = (session.cumulativeInputTokens + session.cumulativeOutputTokens) - discoveryTokenBaseline;

          const originalTimestamp = session.earliestPendingTimestamp;

          if (responseSize > 0) {
            const truncatedResponse = responseSize > 100
              ? textContent.substring(0, 100) + '...'
              : textContent;
            logger.dataOut('SDK', `Response received (${responseSize} chars)`, {
              sessionId: session.sessionDbId,
              promptNumber: session.lastPromptNumber
            }, truncatedResponse);
          }

          if (typeof textContent === 'string' && textContent.includes('Invalid API key')) {
            throw new Error('Invalid API key: check your API key configuration in ~/.claude-mem/settings.json or ~/.claude-mem/.env');
          }

          await processAgentResponse(
            textContent,
            session,
            this.dbManager,
            this.sessionManager,
            worker,
            discoveryTokens,
            originalTimestamp,
            'SDK',
            cwdTracker.lastCwd,
            modelId,
            activeResponseContext.current
          );

          discoveryTokenBaseline = session.cumulativeInputTokens + session.cumulativeOutputTokens;
          turnDispatchedText = true;

View on GitHub (pinned to d8bc9755e7)