Skip to content

feat: auto-detect API keys from env vars for zero-config startup#15

Open
Jah-yee wants to merge 1 commit intoopen-gitagent:mainfrom
Jah-yee:feat/auto-detect-api-keys
Open

feat: auto-detect API keys from env vars for zero-config startup#15
Jah-yee wants to merge 1 commit intoopen-gitagent:mainfrom
Jah-yee:feat/auto-detect-api-keys

Conversation

@Jah-yee
Copy link

@Jah-yee Jah-yee commented Mar 17, 2026

Summary

Adds auto-detection of API keys from environment variables (shell env or .env file) to enable zero-configuration startup. When no model is explicitly configured, gitclaw now checks for common API key environment variables and automatically selects an appropriate default model.

Changes

  • Add autoDetectModelFromEnv() function that checks for common API key env vars
  • Supports: ANTHROPIC_API_KEY, OPENAI_API_KEY, GOOGLE_API_KEY, OLLAMA_HOST, GROQ_API_KEY, MISTRAL_API_KEY, DEEPSEEK_API_KEY, XAI_API_KEY, and more
  • Also checks .env file in the current directory
  • Maps each API key to a sensible default model (e.g., ANTHROPIC_API_KEY -> claude-sonnet-4-5)
  • Updates error message to mention the auto-detection option

Usage

Users can now run gitclaw without any configuration if they have an API key set:

export ANTHROPIC_API_KEY=sk-...
gitclaw # Automatically uses claude-sonnet-4-5

Or via .env file:

# .env
ANTHROPIC_API_KEY=sk-...

Fixes #14

- Add autoDetectModelFromEnv() function that checks common API key env vars
- Supports: ANTHROPIC_API_KEY, OPENAI_API_KEY, GOOGLE_API_KEY, OLLAMA_HOST, etc.
- Also checks .env file in current directory
- Maps API keys to default models (e.g., ANTHROPIC_API_KEY -> claude-sonnet-4-5)
- Updates error message to mention auto-detection option
Copy link
Contributor

@shreyas-lyzr shreyas-lyzr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea — zero-config startup is a nice DX win. A few issues to address before merging:

Bugs

  1. Typo: ANYSACLE_API_KEY should be ANYSCALE_API_KEY — this will silently never match.

  2. .env reading uses cwd(), not the agent directory. readFileSync(".env") reads from the process working directory, which may not be the agent dir. This should use the agent directory path (consistent with how .env is loaded elsewhere in the codebase). However, the agent dir isn't available at this call site — see design concern below.

  3. Duplicate iteration logic. When a .env file exists, the process.env keys are checked inside the try block. If no .env exists, they're checked again after the catch. This means when .env does exist but doesn't contain any matching keys, process.env keys are still checked in the first loop — so the second loop is dead code in that path. Simpler to load .env vars into a merged object once, then iterate once.

  4. AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY map to the same model separately. Having just an access key without a secret (or vice versa) would falsely trigger auto-detection. These should be checked as a pair (&& not ||).

Design concerns

  1. Stale model IDs. Hardcoded model IDs like claude-sonnet-4-5-20250505 and gpt-4o-2024-11-20 will go stale. Consider using versionless aliases (claude-sonnet-4-5, gpt-4o) or pulling defaults from a config, so this doesn't need code changes every model release.

  2. .env parsing doesn't handle quotes or comments. Lines like ANTHROPIC_API_KEY="sk-..." will include the quotes in the value. Lines starting with # will be parsed as key-value pairs. The existing .env parser in server.ts has the same minimal parsing but at least trims — consider reusing that logic or adding quote stripping and comment handling.

  3. Function placement. This auto-detection runs in loadAgent() which doesn't receive the agent directory. The .env file should be read relative to the agent dir, not cwd. You may want to accept the agent dir as a parameter, or move the .env loading to the caller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants