Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Aug 18, 2025

Summary

This PR addresses issue #7196 by adding a warning notification when users have an ANTHROPIC_API_KEY environment variable set while using the Claude Code API provider. This helps prevent confusion for Claude Max/Pro subscription users who may experience authentication errors due to the environment variable taking precedence over their subscription login.

Problem

When users with Claude Max/Pro subscriptions correctly configure the extension to use Claude Code, the presence of an ANTHROPIC_API_KEY environment variable causes the CLI to use that key instead of the subscription authentication. This results in misleading "Credit balance is too low" errors for users who have valid subscriptions.

Solution

  • Added environment variable detection in the ClaudeCodeHandler constructor
  • Shows a warning notification when ANTHROPIC_API_KEY is detected
  • Includes a "Learn More" action that links to the Claude Code authentication documentation
  • Warning is shown only once per session to avoid annoying users

Changes

  • src/api/providers/claude-code.ts: Added checkForAnthropicApiKey() method to detect and warn about the environment variable
  • src/i18n/locales/en/common.json: Added translation strings for the warning message and Learn More action
  • src/api/providers/tests/claude-code.spec.ts: Added comprehensive tests for the new functionality

Testing

  • ✅ All existing tests pass
  • ✅ Added 5 new tests covering:
    • Warning shown when ANTHROPIC_API_KEY is set
    • No warning when ANTHROPIC_API_KEY is not set
    • Warning shown only once per session
    • Learn More link functionality
    • Handling when user dismisses the warning

Screenshots

The warning message will appear as a VSCode notification:

"An ANTHROPIC_API_KEY environment variable was detected. This may conflict with your Claude Max/Pro subscription login and cause authentication errors. Please unset this environment variable to ensure your subscription plan is used correctly."

With a "Learn More" button that links to the Claude Code authentication documentation.

Fixes #7196


Important

Adds a warning in ClaudeCodeHandler for ANTHROPIC_API_KEY conflicts with Claude Max/Pro subscriptions, with tests and documentation link.

  • Behavior:
    • Adds warning in ClaudeCodeHandler constructor when ANTHROPIC_API_KEY is set, potentially conflicting with Claude Max/Pro subscription.
    • Warning includes "Learn More" link to documentation and is shown only once per session.
  • Code:
    • Adds checkForAnthropicApiKey() method in claude-code.ts for environment variable detection and warning.
    • Updates common.json with new warning and action strings.
  • Testing:
    • Adds tests in claude-code.spec.ts for warning display, single session warning, and "Learn More" link functionality.

This description was created by Ellipsis for 8ad7bdc. You can customize this summary. It will automatically update as commits are pushed.

…scription

- Add environment variable detection in ClaudeCodeHandler constructor
- Show warning notification when ANTHROPIC_API_KEY is detected
- Add i18n translations for the warning message
- Include "Learn More" action that links to Claude Code authentication docs
- Add comprehensive tests for the new functionality

Fixes #7196
@roomote roomote bot requested review from cte, jr and mrubens as code owners August 18, 2025 19:46
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working documentation Improvements or additions to documentation labels Aug 18, 2025
Copy link
Contributor Author

@roomote roomote bot left a comment

Choose a reason for hiding this comment

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

Reviewing my own code is like debugging in a mirror - everything looks backwards but the bugs are still mine.

"learn_more": "Learn More"
},
"actions": {
"learn_more": "Learn More"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I notice we're adding a new "actions" object at lines 138-140, but there's already a "buttons" object at lines 133-137 that contains "learn_more". Is this intentional duplication? Could we consolidate by using the existing "buttons.learn_more" translation key instead to avoid redundancy?


private async checkForAnthropicApiKey() {
// Only check and warn once per session to avoid annoying the user
if (!ClaudeCodeHandler.hasShownApiKeyWarning && process.env.ANTHROPIC_API_KEY) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The static flag approach works for preventing repeated warnings per session, but have you considered making this dismissible with a "Don't show again" option that persists? Some users might intentionally use both ANTHROPIC_API_KEY and Claude Code subscription for different purposes.

)
}
} catch {
// Silently ignore any errors from the warning dialog
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This catch block silently swallows all errors. While I understand we don't want the warning to break the extension, should we at least log errors for debugging purposes? Something like:

this.checkForAnthropicApiKey()
}

private async checkForAnthropicApiKey() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Consider adding a comment here explaining why this check is necessary and linking to issue #7196 for future maintainers. This would help document the context behind this warning system.


afterEach(() => {
// Restore original environment
process.env = originalEnv
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great job on properly saving and restoring process.env in beforeEach/afterEach! Consider adding a test that explicitly verifies the environment is restored correctly after the tests run, just to ensure the cleanup is working as expected.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Aug 18, 2025
@daniel-lxs
Copy link
Member

See #7196 (comment)

@daniel-lxs daniel-lxs closed this Aug 19, 2025
@github-project-automation github-project-automation bot moved this from Triage to Done in Roo Code Roadmap Aug 19, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Aug 19, 2025
@daniel-lxs daniel-lxs deleted the feature/warn-anthropic-api-key-conflict branch August 19, 2025 02:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Bug: Extension Fails for Subscription Users if ANTHROPIC_API_KEY is set

4 participants