Skip to content

Auto-detect API keys and default to latest models for zero-config startup #14

@parshvadaftari

Description

@parshvadaftari

Problem

Currently, gitclaw requires explicit model configuration to start. If a user has API keys set in their shell (e.g. ANTHROPIC_API_KEY, OPENAI_API_KEY) or in a .env file,
gitclaw does not detect them or pick a default model automatically. Instead it fails with:

No model configured. Either:

  • Set model.preferred in agent.yaml (e.g., "anthropic:claude-sonnet-4-5-20250929")
  • Pass --model provider:model on the command line

Users must always either edit agent.yaml or pass --model provider:model manually, even when valid API keys are already available in the environment.

Additionally, there is no .env file support — API keys must be exported in the shell.

Expected Behavior

gitclaw should work out of the box when an API key is present:

  1. Auto-detect available API keys — On startup, check for known environment variables (ANTHROPIC_API_KEY, OPENAI_API_KEY, GOOGLE_API_KEY, XAI_API_KEY,
    GROQ_API_KEY, MISTRAL_API_KEY)
  2. Pick a sensible default model — If no model is configured in agent.yaml or via --model, automatically select the latest/best model for the first available API key.
    Suggested defaults:
    • ANTHROPIC_API_KEYanthropic:claude-sonnet-4-5-20250929
    • OPENAI_API_KEYopenai:gpt-4o
    • GOOGLE_API_KEYgoogle:gemini-2.0-flash
    • (etc.)
  3. Support .env files — Load .env from the agent directory (and/or home directory) so users can persist their API keys without exporting in every shell session

The resolution priority should remain:
--model flag > agent.yaml model.preferred > env config override > auto-detected from API key

Current Code

API key validation (src/index.ts:468-484) already has the provider-to-env-var mapping:

const apiKeyEnvVars: Record<string, string> = {                                                                                                                               
  anthropic: "ANTHROPIC_API_KEY",                                                                                                                                               
  openai: "OPENAI_API_KEY",
  google: "GOOGLE_API_KEY",                                                                                                                                                     
  xai: "XAI_API_KEY",                                                                                                                                                         
  groq: "GROQ_API_KEY",                                                                                                                                                         
  mistral: "MISTRAL_API_KEY",                                                                                                                                                 
};                                                                                                                                                                              

But this is only used to validate after a model is already chosen — not to auto-detect which provider to use.

Model resolution (src/loader.ts:354-360) throws an error when no model is configured instead of falling back to auto-detection.

Suggested Fix

  1. Add .env loading — Use dotenv (or Node's --env-file) to load .env from the agent directory early in startup
  2. Add auto-detection in loadAgent() — When no model is configured via any existing method, iterate through apiKeyEnvVars and pick the first available provider with a sensible
    default model
  3. Log the choice — Print which model was auto-selected so the user knows (e.g. Using anthropic:claude-sonnet-4-5-20250929 (detected ANTHROPIC_API_KEY))

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions