{"record":{"id":"c9ce5e008497fde3","repo":"JuliusBrussee/caveman","slug":"caveman-build-set-cave-model-when-zero-or-multipl","errorCode":null,"errorMessage":"caveman build: set CAVE_MODEL when zero or multiple provider credentials exist","messagePattern":"caveman build: set CAVE_MODEL when zero or multiple provider credentials exist","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/agent/src/cli.ts","lineNumber":1371,"sourceCode":"\nfunction localProviderModel(root: string): string | undefined {\n  try {\n    const parsed = JSON.parse(readFileSync(resolve(root, \".caveman/provider.json\"), \"utf8\")) as { model?: unknown };\n    return typeof parsed.model === \"string\" ? parsed.model : undefined;\n  } catch (error) {\n    if ((error as NodeJS.ErrnoException).code === \"ENOENT\") return undefined;\n    throw new Error(\"caveman build: invalid .caveman/provider.json\");\n  }\n}\n\nfunction detectedModel(): string {\n  const configured = [\n    process.env.ANTHROPIC_API_KEY && \"anthropic/claude-haiku-4-5\",\n    process.env.OPENAI_API_KEY && \"openai/gpt-5.4-mini\",\n    (process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY) && \"google/gemini-2.5-flash\",\n  ].filter((value): value is string => typeof value === \"string\");\n  if (configured.length !== 1) {\n    throw new Error(\"caveman build: set CAVE_MODEL when zero or multiple provider credentials exist\");\n  }\n  return configured[0]!;\n}\n\nasync function loadAgent(path: string): Promise<AgentDefinition> {\n  return agentFromImported(await importFresh(path));\n}\n\nfunction agentFromImported(imported: unknown): AgentDefinition {\n  const exported = imported as { default?: AgentDefinition; agent?: AgentDefinition };\n  const definition = exported.default ?? exported.agent;\n  if (!definition || definition.kind !== \"agent\") throw new Error(\"caveman agent: entry must export default agent()\");\n  return definition;\n}\n\nasync function readLock(root: string): Promise<CaveBuildLock> {\n  return parseCaveBuildLock(JSON.parse(await readFile(resolve(root, \".caveman/agent.lock.json\"), \"utf8\")));\n}","sourceCodeStart":1353,"sourceCodeEnd":1389,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/27d5a3981a347890211bb1bf2439e5c821a63bc9/packages/agent/src/cli.ts#L1353-L1389","documentation":"Thrown by `detectedModel()` when auto-detecting a provider model from environment credentials. It collects candidates from exactly one key per provider (`ANTHROPIC_API_KEY` → claude-haiku-4-5, `OPENAI_API_KEY` → gpt-5.4-mini, `GEMINI_API_KEY`/`GOOGLE_API_KEY` → gemini-2.5-flash) and requires exactly one provider to be credentialed. Zero keys or two-plus keys set makes auto-detection ambiguous, so the build refuses and demands an explicit `CAVE_MODEL`.","triggerScenarios":"Running `caveman build` with none of the three provider keys exported (bare CI box), or with several exported at once (a dev shell with both `ANTHROPIC_API_KEY` and `OPENAI_API_KEY`), while `allowedModels` is unset in the config so the CLI falls back to credential-based detection.","commonSituations":"Dotfiles exporting all available keys; CI secrets injected for multiple providers; switching laptops where stale keys remain exported; containers with no credentials at all.","solutions":["Set `CAVE_MODEL=provider/model-id` explicitly (e.g. `anthropic/claude-haiku-4-5`).","Or export exactly one provider's key and unset the others for the build shell.","Or pin `allowedModels` in `caveman.config.ts` so candidate generation never consults credential detection.","Check for duplicate injection: CI often sets keys via both secret store and job env."],"exampleFix":"# before: zero or multiple credentials\nexport ANTHROPIC_API_KEY=... OPENAI_API_KEY=...\nnpm run build  # Error\n\n# after\nexport CAVE_MODEL=anthropic/claude-haiku-4-5\nnpm run build","handlingStrategy":"validation","validationCode":"function credentialsUnambiguous(): boolean {\n  const providers = [\n    !!process.env.ANTHROPIC_API_KEY,\n    !!process.env.OPENAI_API_KEY,\n    !!(process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY),\n  ].filter(Boolean).length;\n  return providers === 1 || !!process.env.CAVE_MODEL;\n}","typeGuard":null,"tryCatchPattern":"try {\n  await build(args);\n} catch (error) {\n  if (error instanceof Error && error.message.includes(\"set CAVE_MODEL\")) {\n    // export CAVE_MODEL=provider/model or reduce to exactly one provider credential\n  } else throw error;\n}","preventionTips":["Set CAVE_MODEL explicitly in CI and any shell with multiple provider keys exported.","Scope provider secrets per-job instead of exporting everything everywhere.","Pin allowedModels in caveman.config.ts to remove env-based detection entirely."],"tags":["environment","provider","model-selection","build"],"backgroundTag":null,"analyzedSha":"27d5a3981a347890211bb1bf2439e5c821a63bc9","analyzedAt":"2026-08-15T09:26:11.751Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}