Skip to content

feat(js/plugins/anthropic): calculate API key lazily at request time - #4298

Merged
cabljac merged 3 commits into
mainfrom
@invertase/anthropic-runtime-apikey
Aug 3, 2026
Merged

feat(js/plugins/anthropic): calculate API key lazily at request time#4298
cabljac merged 3 commits into
mainfrom
@invertase/anthropic-runtime-apikey

Conversation

@cabljac

@cabljaccabljac commented Jan 27, 2026

Copy link
Copy Markdown
Contributor

This PR should resolve#4245

Checklist (if applicable):

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @cabljac, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the Anthropic plugin's API key handling by introducing lazy and per-request API key resolution. This change allows developers to defer API key provision until a model request is made, or to override a default key for specific requests. This flexibility is crucial for multi-tenant applications or scenarios where API keys need to be managed dynamically, moving the responsibility of client instantiation and API key validation closer to the point of use.

Highlights

  • Lazy API Key Calculation: The Anthropic plugin now calculates the API key lazily at request time, rather than requiring it during plugin initialization. This allows for more flexible API key management.
  • Per-Request API Key Support: Users can now provide an apiKey directly within the config object of a model request. This config.apiKey takes precedence over plugin-level or environment variable keys, enabling per-request authentication.
  • Deferred API Key Validation: The plugin can be initialized with apiKey: false to explicitly defer API key validation. In this mode, an API key must be provided with each request via config.apiKey, or an error will be thrown at request time.
  • Refactored Client Instantiation: The internal logic for creating Anthropic client instances has been refactored. Clients are now instantiated dynamically within the claudeRunner function, ensuring they are built with the correct API key at the moment of execution.
  • Enhanced API Key Resolution Logic: A new calculateApiKey utility function has been introduced to centralize the logic for determining the effective API key, prioritizing request.config.apiKey, then pluginApiKey, and finally the ANTHROPIC_API_KEY environment variable.
  • Updated Testing and Examples: New unit and live tests have been added to cover the apiKey: false scenario and per-request API key overrides. A new test application demonstrates how to use this feature.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

FeatureCommandDescription
Code Review/gemini reviewPerforms a code review for the current pull request in its current state.
Pull Request Summary/gemini summaryProvides a summary of the current pull request in its current state.
Comment@gemini-code-assistResponds in comments when explicitly tagged, both in pull request comments and review comments.
Help/gemini helpDisplays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assistgemini-code-assistBot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a valuable feature for lazily calculating the Anthropic API key at request time, which is particularly useful for multi-tenant applications. The implementation is well-structured, refactoring the client initialization logic and propagating the necessary changes throughout the plugin. The inclusion of comprehensive unit and live tests ensures the new functionality is robust. I have one suggestion to improve the readability and error messaging of the new calculateApiKey helper function, but overall, this is a solid contribution.

Comment threadjs/plugins/anthropic/src/types.ts
@cabljac
cabljac marked this pull request as ready for review January 28, 2026 10:05
@cabljac

Copy link
Copy Markdown
ContributorAuthor

/gemini review

@gemini-code-assistgemini-code-assistBot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request effectively refactors the Anthropic plugin to support lazy initialization of the API key, allowing it to be provided on a per-request basis. The changes are well-structured, with clear separation of concerns and updated type definitions. The addition of comprehensive unit tests, live tests, and an example application is excellent.

I've identified one significant performance consideration related to the creation of new Anthropic client instances on every request, which I've detailed in a specific comment. Addressing this would make the implementation more robust for production environments.

Comment threadjs/plugins/anthropic/src/models.ts
@cabljac
cabljac marked this pull request as draft January 29, 2026 15:48
@cabljac

Copy link
Copy Markdown
ContributorAuthor

don't merge this yet please, need to double check something

@cabljac
cabljac marked this pull request as ready for review February 3, 2026 17:03
@MichaelDoyle

Copy link
Copy Markdown
Contributor

@cabljac this was approved -- did you want to resolve the conflicts and get this in?

@cabljac

Copy link
Copy Markdown
ContributorAuthor

@MichaelDoyle Apologies, i will resolve things and get it in ASAP

@cabljac
cabljacforce-pushed the @invertase/anthropic-runtime-apikey branch from 500c96a to 3980f12CompareAugust 3, 2026 11:16
@cabljac
cabljac merged commit c5de5e2 into mainAug 3, 2026
14 checks passed
@cabljac
cabljac deleted the @invertase/anthropic-runtime-apikey branch August 3, 2026 11:50
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[JS] [Antropic] should support providing api key at runtime

3 participants

@cabljac@MichaelDoyle@pavelgj