{"record":{"id":"cc69b2633d2ac5fb","repo":"gastownhall/beads","slug":"w-set-anthropic-api-key-minimax-api-key-or-ai","errorCode":null,"errorMessage":"%w: set ANTHROPIC_API_KEY, MINIMAX_API_KEY, or ai.api_key in config","messagePattern":"%w: set ANTHROPIC_API_KEY, MINIMAX_API_KEY, or ai\\.api_key in config","errorType":"exception","errorClass":"errAPIKeyRequired","httpStatus":null,"severity":"error","filePath":"internal/compact/haiku.go","lineNumber":51,"sourceCode":"// haikuClient wraps the Anthropic API for issue summarization.\ntype haikuClient struct {\n\tclient         anthropic.Client\n\tmodel          anthropic.Model\n\tapiKeySource   config.AIAPIKeySource\n\tbaseURL        string\n\ttier1Template  *template.Template\n\tmaxRetries     int\n\tinitialBackoff time.Duration\n\tauditEnabled   bool\n\tauditActor     string\n}\n\n// newHaikuClient creates a new Haiku API client.\n// API key resolution order: ANTHROPIC_API_KEY env var > MINIMAX_API_KEY env var > ai.api_key config > explicit apiKey parameter.\nfunc newHaikuClient(apiKey string) (*haikuClient, error) {\n\tapiKey, keySource := config.ResolveAIAPIKey(apiKey)\n\tif apiKey == \"\" {\n\t\treturn nil, fmt.Errorf(\"%w: set ANTHROPIC_API_KEY, MINIMAX_API_KEY, or ai.api_key in config\", errAPIKeyRequired)\n\t}\n\n\tclientOptions := []option.RequestOption{option.WithAPIKey(apiKey)}\n\tbaseURL := config.DefaultAIBaseURL(keySource)\n\tif baseURL != \"\" {\n\t\tclientOptions = append(clientOptions, option.WithBaseURL(baseURL))\n\t}\n\n\tclient := anthropic.NewClient(clientOptions...)\n\n\ttier1Tmpl, err := template.New(\"tier1\").Parse(tier1PromptTemplate)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to parse tier1 template: %w\", err)\n\t}\n\n\taiMetricsOnce.Do(initAIMetrics)\n\n\treturn &haikuClient{","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/compact/haiku.go#L33-L69","documentation":"newHaikuClient requires an AI API key to construct the Anthropic (or MiniMax-compatible) client. It resolves the key via config.ResolveAIAPIKey with the order: explicit apiKey parameter > ANTHROPIC_API_KEY env > MINIMAX_API_KEY env > ai.api_key config. If all are empty it returns errAPIKeyRequired wrapped with this message — the compaction feature simply cannot authenticate without a key.","triggerScenarios":"Calling New (compact client constructor) when no ANTHROPIC_API_KEY or MINIMAX_API_KEY environment variable is set and ai.api_key is absent/empty in the bd config file.","commonSituations":"Fresh clone or CI environment where the env var was never exported; running `bd compact` under systemd/cron that does not inherit your shell profile; typos like ANTHROPIC_APIKEY; key defined in the wrong config file; key expired and removed.","solutions":["export ANTHROPIC_API_KEY=\"sk-ant-...\" in your shell or CI environment before running bd","Set MINIMAX_API_KEY instead if you use MiniMax as the provider","Add ai.api_key to the bd config file for a persistent, per-repo setting","Verify resolution order if multiple sources are set — explicit parameter wins, then ANTHROPIC_API_KEY overrides MINIMAX_API_KEY","Check that your service runner (cron/systemd/CI) actually loads the env file"],"exampleFix":"// before (key missing)\nclient, err := compact.New(\"\") // -> errAPIKeyRequired\n// after\nos.Setenv(\"ANTHROPIC_API_KEY\", os.Getenv(\"ANTHROPIC_API_KEY\")) // ensure exported\nclient, err := compact.New(\"\") // env var picked up\n// or pass explicitly:\nclient, err := compact.New(\"sk-ant-...\")","handlingStrategy":"validation","validationCode":"// check key availability before constructing the client\nif os.Getenv(\"ANTHROPIC_API_KEY\") == \"\" && os.Getenv(\"MINIMAX_API_KEY\") == \"\" {\n\tif cfg.AI.APIKey == \"\" {\n\t\treturn errors.New(\"no AI API key: set ANTHROPIC_API_KEY, MINIMAX_API_KEY, or ai.api_key\")\n\t}\n}\nclient, err := compact.New(\"\")","typeGuard":null,"tryCatchPattern":"client, err := compact.New(apiKey)\nif errors.Is(err, compact.ErrAPIKeyRequired) {\n\treturn fmt.Errorf(\"configure an AI key: %w\", err)\n}","preventionTips":["Export ANTHROPIC_API_KEY in shell profiles, CI secrets, and service units","Set ai.api_key in bd config for environments that don't inherit env vars","Never rename the env var (ANTHROPIC_APIKEY is not read)","Remember precedence: explicit param > ANTHROPIC_API_KEY > MINIMAX_API_KEY > config"],"tags":["api-key","configuration","environment-variable","anthropic"],"backgroundTag":"missing-env-var","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}