Skip to content

Conversation

@7Sageer
Copy link
Contributor

@7Sageer 7Sageer commented Dec 15, 2025

Problem

SDK Agent fails with "Your account does not have access to Claude Code. Please run /login." error for users using BYOK (Bring Your Own Key) with third-party API proxies (e.g., ANTHROPIC_BASE_URL).

Root Cause: The SDK Agent spawns a Claude subprocess that doesn't inherit the custom API settings from ~/.claude/settings.json, causing it to try default Anthropic authentication which fails.

Additionally, when loading user settings with settingSources: ["user"], the SDK subprocess also loads the claude-mem hooks configuration, causing SDK internal prompts to be incorrectly saved to the user_prompts table.

Solution

1. BYOK Support (Source-level fix)

  • Add loadClaudeCodeApiSettings() method to worker-service.ts that reads BYOK config from ~/.claude/settings.json and sets ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN as environment variables at worker startup
  • Use settingSources: [] (empty) in SDK Agent to prevent loading any user settings including hooks
  • API authentication works through inherited environment variables

2. Defensive SDK Prompt Filter (Fallback)

  • Add defensive filter in SessionRoutes.ts to detect SDK internal sessions
  • Primary detection: If project name equals home directory name (SDK runs with workingDirectory: homedir())
  • Fallback detection: Pattern matching for known SDK prompt prefixes

Changes

  • src/services/worker-service.ts: Add loadClaudeCodeApiSettings() method to load BYOK config
  • src/services/worker/SDKAgent.ts: Use settingSources: [] to prevent loading hooks
  • src/hooks/new-hook.ts: Add SDK prompt filter at hook level
  • src/services/worker/http/routes/SessionRoutes.ts: Add SDK prompt filter at API level with home directory detection

Why This Approach

Using settingSources: [] is cleaner than filtering because:

  • Prevents the problem at source: SDK subprocess doesn't load hooks at all
  • No hardcoded prompt patterns needed: More robust against prompt changes
  • BYOK support via env vars: API auth works through process environment inheritance

Testing

  1. ✅ Verified SDK Agent successfully connects to third-party API proxy with settingSources: []
  2. ✅ Verified SDK internal prompts are no longer saved to database (count dropped from 12+ to 0)
  3. ✅ Normal user prompts are correctly saved (skipped: false)
  4. ✅ Direct API test confirms filter works for SDK sessions

- Add settingSources: ['user'] to SDK query options to load user's API
  settings from ~/.claude/settings.json (fixes BYOK authentication)
- Add workingDirectory option to ensure Claude subprocess runs in
  trusted location
- Add SDK internal prompt filters at both hook and API levels to
  prevent SDK system prompts from being saved to user_prompts table

Fixes: SDK Agent fails with 'Your account does not have access to
Claude Code' error for users using ANTHROPIC_BASE_URL proxy
- Add loadClaudeCodeApiSettings() to worker-service.ts to load BYOK config
  (ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN) from ~/.claude/settings.json
  and set as environment variables at worker startup

- Update SDKAgent.ts to use settingSources: [] which prevents loading any
  user settings including hooks, avoiding SDK subprocess triggering claude-mem
  hooks and creating infinite recursion

- Add defensive filter in SessionRoutes.ts to detect SDK internal prompts
  by checking if project name equals home directory name, with fallback
  pattern matching for known SDK prompt prefixes

This fixes an issue where BYOK users would see SDK internal prompts
(like 'You are a Claude-Mem...' and 'PROGRESS SUMMARY CHECKPOINT')
incorrectly saved to the user_prompts database table.
@thedotmack
Copy link
Owner

@claude review

@claude
Copy link

claude bot commented Dec 15, 2025

Claude encountered an error —— View job

Failed with exit code 128

I'll analyze this and get back to you.

The settingSources: [] fix prevents SDK subprocess from loading hooks,
making the pattern filtering dead code that can never execute.

Changes:
- Remove SDK prompt pattern filtering from new-hook.ts
- Remove SDK filtering and homedir detection from SessionRoutes.ts
- Add 5-second timeout to apiKeyHelper execSync to prevent hangs

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@thedotmack
Copy link
Owner

Auto code review

No issues found. Checked for bugs and CLAUDE.md compliance.

Refactor API key loading in WorkerService and SDKAgent

- Updated WorkerService to load custom API key from ~/.claude-mem/settings.json for SDK Agent.
- Removed environment variable settings from WorkerService; now only logs API key presence.
- Introduced buildSDKEnvironment method in SDKAgent to explicitly pass API credentials to subprocess.
- Enhanced logging for API key and base URL loading, ensuring better visibility during startup.
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.

2 participants