gitagent icon indicating copy to clipboard operation
gitagent copied to clipboard

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

Open parshvadaftari opened this issue 8 months ago • 3 comments

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_KEY → anthropic:claude-sonnet-4-5-20250929
    • OPENAI_API_KEY → openai:gpt-4o
    • GOOGLE_API_KEY → google: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))

parshvadaftari avatar Mar 17 '26 15:03 parshvadaftari

@shreyas-lyzr can you assign this to me? Can definitely work on this. Thanks!

parshvadaftari avatar Mar 17 '26 16:03 parshvadaftari

@parshvadaftari I think @jah-yee has made some progress, can we work on his PR?

shreyas-lyzr avatar Mar 19 '26 07:03 shreyas-lyzr

Apologies for the long wait @parshvadaftari — this is overdue.

Status: still wanted, still on the roadmap, partially overlapping with PR #2 (which proposed using pi-ai's getEnvApiKey() for credential detection). PR #2 needs a rebase but the credential-detection piece is exactly what you're asking for.

What I'd commit to landing under this issue:

  1. API-key auto-detect on startup — check (in order) ANTHROPIC_API_KEY, OPENAI_API_KEY, GOOGLE_API_KEY, XAI_API_KEY, GROQ_API_KEY, MISTRAL_API_KEY. Use pi-ai's getEnvApiKey() rather than a hardcoded list so we automatically pick up Bedrock / Vertex / Azure / Copilot etc.
  2. Sensible default model per provider — Anthropic → claude-sonnet-4-6, OpenAI → gpt-4o, Google → gemini-2.0-flash, etc. Latest stable per provider, refreshed quarterly.
  3. .env file support — load .env from CWD and from agent directory at startup. dotenv is one dependency, well-understood.
  4. Verbose detection log on startup — print "detected ANTHROPIC_API_KEY, defaulting to claude-sonnet-4-6" so users know what's happening.

Going to thread this through PR #2's rebase (or pick it up myself if the original author is out). Either way, this should be in the next release. Thank you for the patience.

shreyas-lyzr avatar May 13 '26 13:05 shreyas-lyzr