{"record":{"id":"fa587142469d5e67","repo":"JuliusBrussee/caveman","slug":"rewriter-api-key-is-required","errorCode":null,"errorMessage":"rewriter: api key is required","messagePattern":"rewriter: api key is required","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"rewriter/rewriter.go","lineNumber":137,"sourceCode":"\t// on rejected calls so a corpus study can size the rewrites the gate threw\n\t// away. The bytes themselves never leave on a rejection.\n\tCandidateTokens int\n}\n\n// New validates cfg and returns a Client. An unknown provider is an error\n// rather than a default: a silently mis-routed rewriter would produce bytes no\n// gate was designed for.\nfunc New(cfg Config) (*Client, error) {\n\tprovider := strings.ToLower(strings.TrimSpace(cfg.Provider))\n\tif provider != providerAnthropic && provider != providerOpenAI {\n\t\treturn nil, fmt.Errorf(\"rewriter: unsupported provider %q\", cfg.Provider)\n\t}\n\tmodel := strings.TrimSpace(cfg.Model)\n\tif model == \"\" {\n\t\treturn nil, errors.New(\"rewriter: model is required\")\n\t}\n\tif strings.TrimSpace(cfg.APIKey) == \"\" {\n\t\treturn nil, errors.New(\"rewriter: api key is required\")\n\t}\n\ttheta := cfg.Theta\n\tif theta == 0 {\n\t\ttheta = DefaultTheta\n\t}\n\tif theta < 0 {\n\t\treturn nil, fmt.Errorf(\"rewriter: theta must not be negative, got %d\", theta)\n\t}\n\tdoer := cfg.HTTPDoer\n\tif doer == nil {\n\t\tdoer = http.DefaultClient.Do\n\t}\n\treturn &Client{provider: provider, model: model, apiKey: cfg.APIKey, theta: theta, doer: doer}, nil\n}\n\n// Rewrite condenses req.StepBytes, or explains why it did not.\n//\n// Gate 1 (invocation) skips the API call entirely when the step is at or under","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/rewriter/rewriter.go#L119-L155","documentation":"rewriter.New requires cfg.APIKey to be non-empty; without a credential the downstream provider will reject every request, so the constructor fails fast with this sentinel instead of producing a useless client.","triggerScenarios":"Calling rewriter.New with a Config whose APIKey is \"\" or whitespace — e.g. the key env var (ANTHROPIC_API_KEY / OPENAI_API_KEY) was not loaded into the config.","commonSituations":"Secrets not mounted in CI, an .env file not loaded, key rotated and placeholder left empty, or the config struct field renamed so the key never reaches cfg.APIKey.","solutions":["Populate cfg.APIKey with the provider API key before calling New","Verify the environment variable or secret store actually feeds the rewriter config (e.g. os.Getenv(\"ANTHROPIC_API_KEY\"))","Check deployment/CI secret mounting so the key exists in the process environment"],"exampleFix":"// before\nrw, err := rewriter.New(rewriter.Config{Provider: \"openai\", Model: \"gpt-5\"})\n// after\nrw, err := rewriter.New(rewriter.Config{Provider: \"openai\", Model: \"gpt-5\", APIKey: os.Getenv(\"OPENAI_API_KEY\")})\n","handlingStrategy":"validation","validationCode":"key := os.Getenv(\"ANTHROPIC_API_KEY\")\nif key == \"\" {\n\treturn errors.New(\"ANTHROPIC_API_KEY not set; cannot init rewriter\")\n}\ncfg.APIKey = key\n","typeGuard":null,"tryCatchPattern":"rw, err := rewriter.New(cfg)\nif err != nil {\n\tif strings.Contains(err.Error(), \"api key is required\") {\n\t\t// surface missing-secret guidance\n\t}\n\treturn err\n}\n","preventionTips":["Verify provider API key env vars at process startup","Mount secrets in CI/deploy before launching","Do not ship configs with empty api-key placeholders"],"tags":["rewriter","api-key","missing-credential"],"backgroundTag":"missing-api-key","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}