{"record":{"id":"9205e168c20d9138","repo":"gitbutlerapp/gitbutler","slug":"when-using-anthropic-in-a-bring-your-own-key-confi","errorCode":null,"errorMessage":"When using Anthropic in a bring your own key configuration, you must provide a valid token","messagePattern":"When using Anthropic in a bring your own key configuration, you must provide a valid token","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"apps/desktop/src/lib/ai/service.ts","lineNumber":356,"sourceCode":"\t\t\tconst openAIModelName = await this.getOpenAIModelName();\n\t\t\tconst openAIKey = await this.getOpenAIKey();\n\t\t\tconst openAICustomEndpoint = await this.getOpenAICustomEndpoint();\n\n\t\t\tif (!openAIKey) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"When using OpenAI in a bring your own key configuration, you must provide a valid token\",\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn new OpenAIClient(openAIKey, openAIModelName, openAICustomEndpoint);\n\t\t}\n\n\t\tif (modelKind === ModelKind.Anthropic) {\n\t\t\tconst anthropicModelName = await this.getAnthropicModelName();\n\t\t\tconst anthropicKey = await this.getAnthropicKey();\n\n\t\t\tif (!anthropicKey) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"When using Anthropic in a bring your own key configuration, you must provide a valid token\",\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn new AnthropicAIClient(anthropicKey, anthropicModelName);\n\t\t}\n\n\t\tif (modelKind === ModelKind.OpenRouter) {\n\t\t\tconst openRouterKey = (await this.getOpenRouterKey())?.trim();\n\t\t\tconst openRouterModelName = await this.getOpenRouterModelName();\n\n\t\t\tif (!openRouterKey) {\n\t\t\t\tthrow new Error(\"When using OpenRouter, you must provide a valid API key\");\n\t\t\t}\n\n\t\t\treturn new OpenAIClient(openRouterKey, openRouterModelName, \"https://openrouter.ai/api/v1\");\n\t\t}\n","sourceCodeStart":338,"sourceCodeEnd":374,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/apps/desktop/src/lib/ai/service.ts#L338-L374","documentation":"buildClient() throws this when the model kind is Anthropic in bring-your-own-key mode but no Anthropic API key is configured (getAnthropicKey() returned empty). AnthropicAIClient needs the key for authentication.","triggerScenarios":"User selects Anthropic as the AI provider without entering an API key in settings.","commonSituations":"Chose Anthropic but never filled the key; key lost after settings reset or app reinstall; key field left blank because the user expected bundled credentials.","solutions":["Enter a valid Anthropic API key in the AI settings.","Create a key at console.anthropic.com if needed.","Or switch to another provider (local model or GitButler API)."],"exampleFix":"// before\nanthropicKey: ''\n// after\nanthropicKey: 'sk-ant-...' // valid Anthropic API key","handlingStrategy":"validation","validationCode":"const key = await aiService.getAnthropicKey();\nif (!key) {\n  // prompt for the Anthropic API key before invoking AI features\n}","typeGuard":null,"tryCatchPattern":"try {\n  await aiService.summarize(...);\n} catch (e) {\n  if (e.message.includes('Anthropic') && e.message.includes('valid token')) {\n    // open settings to collect the Anthropic API key\n  } else {\n    throw e;\n  }\n}","preventionTips":["Enter the Anthropic API key when selecting the Anthropic provider.","Validate key format (starts with 'sk-ant-') on save.","Re-enter the key after settings resets or reinstalls."],"tags":["configuration","api-key","anthropic"],"backgroundTag":"missing-api-key","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}