JuliusBrussee/caveman · error

rewriter: api key is required

Error message

rewriter: api key is required

What it means

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.

Solutions

  1. Populate cfg.APIKey with the provider API key before calling New
  2. Verify the environment variable or secret store actually feeds the rewriter config (e.g. os.Getenv("ANTHROPIC_API_KEY"))
  3. Check deployment/CI secret mounting so the key exists in the process environment

Example fix

// before
rw, err := rewriter.New(rewriter.Config{Provider: "openai", Model: "gpt-5"})
// after
rw, err := rewriter.New(rewriter.Config{Provider: "openai", Model: "gpt-5", APIKey: os.Getenv("OPENAI_API_KEY")})
Defensive patterns

Strategy: validation

Validate before calling

key := os.Getenv("ANTHROPIC_API_KEY")
if key == "" {
	return errors.New("ANTHROPIC_API_KEY not set; cannot init rewriter")
}
cfg.APIKey = key

Try / catch

rw, err := rewriter.New(cfg)
if err != nil {
	if strings.Contains(err.Error(), "api key is required") {
		// surface missing-secret guidance
	}
	return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/fa587142469d5e67. Report an issue: GitHub.

Appendix: source

Thrown at rewriter/rewriter.go:137

	// on rejected calls so a corpus study can size the rewrites the gate threw
	// away. The bytes themselves never leave on a rejection.
	CandidateTokens int
}

// New validates cfg and returns a Client. An unknown provider is an error
// rather than a default: a silently mis-routed rewriter would produce bytes no
// gate was designed for.
func New(cfg Config) (*Client, error) {
	provider := strings.ToLower(strings.TrimSpace(cfg.Provider))
	if provider != providerAnthropic && provider != providerOpenAI {
		return nil, fmt.Errorf("rewriter: unsupported provider %q", cfg.Provider)
	}
	model := strings.TrimSpace(cfg.Model)
	if model == "" {
		return nil, errors.New("rewriter: model is required")
	}
	if strings.TrimSpace(cfg.APIKey) == "" {
		return nil, errors.New("rewriter: api key is required")
	}
	theta := cfg.Theta
	if theta == 0 {
		theta = DefaultTheta
	}
	if theta < 0 {
		return nil, fmt.Errorf("rewriter: theta must not be negative, got %d", theta)
	}
	doer := cfg.HTTPDoer
	if doer == nil {
		doer = http.DefaultClient.Do
	}
	return &Client{provider: provider, model: model, apiKey: cfg.APIKey, theta: theta, doer: doer}, nil
}

// Rewrite condenses req.StepBytes, or explains why it did not.
//
// Gate 1 (invocation) skips the API call entirely when the step is at or under

View on GitHub (pinned to 3ee70a1026)