eyaltoledano/claude-task-master · error · LoadAPIKeyError

Grok CLI API key not found. Set GROK_CLI_API_KEY environment

Error message

Grok CLI API key not found. Set GROK_CLI_API_KEY environment variable or configure grok-cli.

What it means

After confirming the Grok CLI binary is installed, doGenerate calls getApiKey() to obtain credentials. If no API key is found — neither the GROK_CLI_API_KEY environment variable nor grok-cli's own stored configuration — it throws an authentication error telling you where to set the key. The provider cannot authenticate requests to Grok without it.

Source

Thrown at packages/ai-sdk-provider-grok-cli/src/grok-cli-language-model.ts:268

	/**
	 * Generate text using Grok CLI
	 */
	async doGenerate(options: LanguageModelV2CallOptions) {
		// Handle abort signal early
		if (options.abortSignal?.aborted) {
			throw options.abortSignal.reason || new Error('Request aborted');
		}

		// Check CLI installation
		const isInstalled = await this.checkGrokCliInstallation();
		if (!isInstalled) {
			throw createInstallationError({});
		}

		// Get API key
		const apiKey = await this.getApiKey();
		if (!apiKey) {
			throw createAuthenticationError({
				message:
					'Grok CLI API key not found. Set GROK_CLI_API_KEY environment variable or configure grok-cli.'
			});
		}

		const prompt = createPromptFromMessages(options.prompt);
		const warnings = this.generateAllWarnings(options, prompt);

		// Build command arguments
		const args = ['--prompt', escapeShellArg(prompt)];

		// Add model if specified
		if (this.modelId && this.modelId !== 'default') {
			args.push('--model', this.modelId);
		}

		// Skip API key parameter if it's likely already configured to avoid hanging
		// The CLI seems to hang when trying to save API keys for grok-4 models

View on GitHub (pinned to c0c98d367c)

Solutions

  1. Set the environment variable: export GROK_CLI_API_KEY=<your-key> (or add it to .env and ensure it is loaded).
  2. Alternatively run `grok` interactively once and complete its auth/config setup so the key is stored.
  3. In CI/production, add GROK_CLI_API_KEY to the secret manager and inject it into the process environment.
  4. Verify from inside the same process: console.log(Boolean(process.env.GROK_CLI_API_KEY)).

Example fix

// before (shell)
node app.js   // GROK_CLI_API_KEY unset
// after (shell)
export GROK_CLI_API_KEY=xai-... && node app.js
Defensive patterns

Strategy: validation

Validate before calling

if (!process.env.GROK_CLI_API_KEY) {
  throw new Error('GROK_CLI_API_KEY is required before using the Grok CLI provider');
}

Try / catch

try {
  const result = await model.doGenerate({ prompt });
} catch (e) {
  if (/API key not found/.test(e.message)) {
    throw new Error('Set GROK_CLI_API_KEY in the environment (or run `grok` once to configure).');
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling doGenerate when GROK_CLI_API_KEY is unset in the environment and grok-cli has no configured credentials file/session.

Common situations: Forgetting to export the env var in the shell or CI; env var set in one shell but not the process running the app; .env file not loaded by the runtime; key configured for a different tool (e.g. XAI_API_KEY) instead of GROK_CLI_API_KEY.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of eyaltoledano/claude-task-master@c0c98d367c (2026-08-29). Data as JSON: /api/errors/fd2e4e172288f454. Report an issue: GitHub.