gitbutlerapp/gitbutler · error
When using Anthropic in a bring your own key configuration…
Error message
When using Anthropic in a bring your own key configuration, you must provide a valid token
What it means
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.
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).
Example fix
// before anthropicKey: '' // after anthropicKey: 'sk-ant-...' // valid Anthropic API key
Defensive patterns
Strategy: validation
Validate before calling
const key = await aiService.getAnthropicKey();
if (!key) {
// prompt for the Anthropic API key before invoking AI features
} Try / catch
try {
await aiService.summarize(...);
} catch (e) {
if (e.message.includes('Anthropic') && e.message.includes('valid token')) {
// open settings to collect the Anthropic API key
} else {
throw e;
}
} Prevention
- 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.
When it happens
Trigger: User selects Anthropic as the AI provider without entering an API key in settings.
Common situations: 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.
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
- Enter an Anthropic API key
- Enter an Anthropic API key
- Enter an OpenAI API key
- Environment variable ANTHROPIC_API_KEY is not set
- Environment variable OPENAI_API_KEY is not set
AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18).
Data as JSON: /api/errors/9205e168c20d9138.
Report an issue: GitHub.
Appendix: source
Thrown at apps/desktop/src/lib/ai/service.ts:356
const openAIModelName = await this.getOpenAIModelName();
const openAIKey = await this.getOpenAIKey();
const openAICustomEndpoint = await this.getOpenAICustomEndpoint();
if (!openAIKey) {
throw new Error(
"When using OpenAI in a bring your own key configuration, you must provide a valid token",
);
}
return new OpenAIClient(openAIKey, openAIModelName, openAICustomEndpoint);
}
if (modelKind === ModelKind.Anthropic) {
const anthropicModelName = await this.getAnthropicModelName();
const anthropicKey = await this.getAnthropicKey();
if (!anthropicKey) {
throw new Error(
"When using Anthropic in a bring your own key configuration, you must provide a valid token",
);
}
return new AnthropicAIClient(anthropicKey, anthropicModelName);
}
if (modelKind === ModelKind.OpenRouter) {
const openRouterKey = (await this.getOpenRouterKey())?.trim();
const openRouterModelName = await this.getOpenRouterModelName();
if (!openRouterKey) {
throw new Error("When using OpenRouter, you must provide a valid API key");
}
return new OpenAIClient(openRouterKey, openRouterModelName, "https://openrouter.ai/api/v1");
}
View on GitHub (pinned to 58e5313667)