Anthropic Authentication Plugin for OpenCode
This package, `opencode-claude-code-auth`, is an OpenCode plugin designed to restore and manage Anthropic (Claude) authentication methods within the OpenCode ecosystem. It provides three distinct authentication paths: reading local Claude Code session credentials, initiating a browser-based OAuth flow, or utilizing a plain Anthropic API key. The plugin addresses the removal of built-in Anthropic authentication from OpenCode v1.3.x, ensuring continued access to Claude models for users. It is currently at version 0.1.6, indicating active development with frequent minor updates. Key differentiators include its flexible login options, token management (refreshing Claude CLI tokens or using OAuth refresh tokens), and seamless integration into OpenCode's provider system to intercept and manage all Anthropic API traffic. It targets Node.js environments version 20 or higher.
Common errors
-
Error: Cannot find module 'opencode-claude-code-auth'
cause The `opencode-claude-code-auth` plugin was not correctly specified in `opencode.json` or OpenCode failed to install it automatically.fixVerify that `"opencode-claude-code-auth"` is present in the `plugin` array within your `opencode.json` file. Ensure OpenCode has internet access to download npm packages. -
Authentication Failed: Anthropic API returned a 401 or 403 error.
cause Your authentication tokens (session, OAuth, or API key) have expired, are invalid, or lack the necessary permissions.fixRun `opencode auth login -p anthropic` again to re-authenticate and refresh your tokens. If using an API key, double-check its validity. -
node: --enable-source-maps is not allowed in NODE_OPTIONS
cause This error typically indicates an incompatible Node.js version, especially when `NODE_OPTIONS` conflicts with internal module loading or features not present in older versions.fixUpgrade your Node.js environment to version 20 or higher, as required by the package. Use `nvm install 20` or your preferred Node.js version manager.
Warnings
- breaking OpenCode v1.3.x and newer versions removed built-in Anthropic authentication, making this plugin necessary to restore the functionality. Without it, direct Anthropic integration in OpenCode will fail.
- gotcha The package requires Node.js version 20 or higher. Running with older Node.js versions will result in execution errors.
- gotcha When using the 'Claude Code session' login path, the plugin relies on the `claude` CLI being installed and configured to read credentials from the macOS Keychain (or `~/.claude/.credentials.json` on Linux/Windows). If the `claude` CLI is not present or its tokens are invalid, this authentication method will fail.
- gotcha Token refresh mechanisms differ significantly between the 'Claude Code session' and 'Browser sign-in' paths. Developers should be aware of these differences, especially in automated environments where manual intervention for refreshes is undesirable.
Install
-
npm install opencode-claude-code-auth -
yarn add opencode-claude-code-auth -
pnpm add opencode-claude-code-auth
Imports
- configurePlugin
import { configurePlugin } from 'opencode-claude-code-auth/cli'; - AnthropicAuthPlugin
import { AnthropicAuthPlugin } from 'opencode-claude-code-auth'; - AnthropicProvider
import type { AnthropicProvider } from 'opencode-claude-code-auth/types';
Quickstart
{
"plugin": ["opencode-claude-code-auth"]
}
# Save the above JSON to your opencode.json file
# Then, in your terminal:
opencode auth login -p anthropic
# Follow the prompts to pick a login path (Claude Code session, browser sign-in, or API key).
# If OpenCode asks to configure Anthropic models, say yes.
# Verify your setup:
opencode run "Hello, Claude! What's new?" -m anthropic/claude-sonnet-4-5