{"record":{"id":"fd2e4e172288f454","repo":"eyaltoledano/claude-task-master","slug":"grok-cli-api-key-not-found-set-grok-cli-api-key-e","errorCode":null,"errorMessage":"Grok CLI API key not found. Set GROK_CLI_API_KEY environment variable or configure grok-cli.","messagePattern":"Grok CLI API key not found\\. Set GROK_CLI_API_KEY environment variable or configure grok-cli\\.","errorType":"exception","errorClass":"LoadAPIKeyError","httpStatus":null,"severity":"error","filePath":"packages/ai-sdk-provider-grok-cli/src/grok-cli-language-model.ts","lineNumber":268,"sourceCode":"\t/**\n\t * Generate text using Grok CLI\n\t */\n\tasync doGenerate(options: LanguageModelV2CallOptions) {\n\t\t// Handle abort signal early\n\t\tif (options.abortSignal?.aborted) {\n\t\t\tthrow options.abortSignal.reason || new Error('Request aborted');\n\t\t}\n\n\t\t// Check CLI installation\n\t\tconst isInstalled = await this.checkGrokCliInstallation();\n\t\tif (!isInstalled) {\n\t\t\tthrow createInstallationError({});\n\t\t}\n\n\t\t// Get API key\n\t\tconst apiKey = await this.getApiKey();\n\t\tif (!apiKey) {\n\t\t\tthrow createAuthenticationError({\n\t\t\t\tmessage:\n\t\t\t\t\t'Grok CLI API key not found. Set GROK_CLI_API_KEY environment variable or configure grok-cli.'\n\t\t\t});\n\t\t}\n\n\t\tconst prompt = createPromptFromMessages(options.prompt);\n\t\tconst warnings = this.generateAllWarnings(options, prompt);\n\n\t\t// Build command arguments\n\t\tconst args = ['--prompt', escapeShellArg(prompt)];\n\n\t\t// Add model if specified\n\t\tif (this.modelId && this.modelId !== 'default') {\n\t\t\targs.push('--model', this.modelId);\n\t\t}\n\n\t\t// Skip API key parameter if it's likely already configured to avoid hanging\n\t\t// The CLI seems to hang when trying to save API keys for grok-4 models","sourceCodeStart":250,"sourceCodeEnd":286,"githubUrl":"https://github.com/eyaltoledano/claude-task-master/blob/c0c98d367c55296bfe69e65680625b6db437af02/packages/ai-sdk-provider-grok-cli/src/grok-cli-language-model.ts#L250-L286","documentation":"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.","triggerScenarios":"Calling doGenerate when GROK_CLI_API_KEY is unset in the environment and grok-cli has no configured credentials file/session.","commonSituations":"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.","solutions":["Set the environment variable: export GROK_CLI_API_KEY=<your-key> (or add it to .env and ensure it is loaded).","Alternatively run `grok` interactively once and complete its auth/config setup so the key is stored.","In CI/production, add GROK_CLI_API_KEY to the secret manager and inject it into the process environment.","Verify from inside the same process: console.log(Boolean(process.env.GROK_CLI_API_KEY))."],"exampleFix":"// before (shell)\nnode app.js   // GROK_CLI_API_KEY unset\n// after (shell)\nexport GROK_CLI_API_KEY=xai-... && node app.js","handlingStrategy":"validation","validationCode":"if (!process.env.GROK_CLI_API_KEY) {\n  throw new Error('GROK_CLI_API_KEY is required before using the Grok CLI provider');\n}","typeGuard":null,"tryCatchPattern":"try {\n  const result = await model.doGenerate({ prompt });\n} catch (e) {\n  if (/API key not found/.test(e.message)) {\n    throw new Error('Set GROK_CLI_API_KEY in the environment (or run `grok` once to configure).');\n  }\n  throw e;\n}","preventionTips":["Load .env before constructing the provider and assert GROK_CLI_API_KEY at startup.","Add the key to CI secret manager and verify injection with a startup check.","Never rely on shell-local exports for long-running services; use process-manager env config.","Use the exact variable name GROK_CLI_API_KEY, not XAI_API_KEY or similar."],"tags":["grok-cli","api-key","environment-variable","authentication"],"backgroundTag":"missing-env-var","analyzedSha":"c0c98d367c55296bfe69e65680625b6db437af02","analyzedAt":"2026-08-29T02:56:26.071Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}