Skip to content

Question: How does opencode work with Claude Code OAuth tokens when AI SDK fails? #417

Closed

Description

@aShanki

Hi! I'm trying to understand how opencode successfully uses Claude Code OAuth tokens with the Anthropic API, as I'm getting restrictions when trying to replicate the same approach.

I've been studying opencode's implementation to understand how it integrates with Claude Code OAuth. Based on my analysis of the codebase, opencode uses:

  • @ai-sdk/anthropic package
  • Standard https://api.anthropic.com/v1/messages endpoint
  • Custom fetch function with Bearer ${accessToken} authorization
  • anthropic-beta: oauth-2025-04-20 header
  • Removes x-api-key header

This is exactly implemented in /packages/opencode/src/provider/provider.ts lines 42-71.

When I try to replicate this exact approach using Claude Code OAuth tokens, I get this error:

{
    "type": "error",
    "error": {
        "type": "invalid_request_error",
        "message": "system: This credential is only authorized for use with Claude Code and cannot be used for other API requests."
    }
}

So I have a few questions:

  1. How does opencode bypass this restriction? The error suggests Claude Code OAuth tokens are restricted to "Claude Code" only, but opencode successfully uses them.
  2. Is there a special endpoint or configuration? Does opencode use a different API endpoint, special headers, or application registration that allows it to work?
  3. Application whitelisting? Is opencode specifically whitelisted by Anthropic to use Claude Code OAuth tokens?
  4. Token scopes or format? Are there specific OAuth scopes or token formats that opencode requests that differ from standard Claude Code tokens?

So far I've tried

  • Used identical headers and fetch configuration as opencode
  • Tried console.anthropic.com endpoint instead of api.anthropic.com
  • Verified token format matches opencode's auth.json structure
  • Used same AI SDK version and configuration

I expected to be able to use Claude Code OAuth tokens with the AI SDK following opencode's implementation pattern, since opencode clearly demonstrates this is possible.

Environment Details

  • Node.js v22.17.0
  • @ai-sdk/anthropic v1.2.12
  • ai SDK (latest)

Any insights into how opencode's Anthropic integration works would be greatly appreciated! The codebase shows it's using standard AI SDK patterns, but there must be something special about how it's configured or registered with Anthropic.

Thanks in advance guys, and great job on the product. I've loved using it.

Activity

  1. aShanki commented on Jun 26, 2025

    @aShanki
    Author

    I'm taking my API tokens straight from the ~/.claude/.credentials.json btw. I'm not sure if that makes a huge difference or not.

  2. thdxr commented on Jun 26, 2025

    @thdxr
    Member

    hey unfortunately we can't provide any specific instructions for this

  3. self-assigned this
    on Jun 26, 2025
  4. aShanki commented on Jun 26, 2025

    @aShanki
    Author

    Disappointing. I really want to be able to use my subscription for personal use, but that seems impossible.

  5. nam20485 commented on Sep 3, 2025

    @nam20485

    Disappointing. I really want to be able to use my subscription for personal use, but that seems impossible.

    one word: repositories view

  6. baleti commented on Sep 12, 2025

    @baleti

    one word: repositories view

    what do you mean?

  7. TKasperczyk commented on Dec 7, 2025

    @TKasperczyk

    one word: repositories view

    what do you mean?

    It's possible, and the solution is so silly that I laughed when I finally figured it out. I'm not sure if I should just post it plainly here since Anthropic might block it which would affect opencode as well, but here's a hint. After you exhaust every option and you're sure the requests you're sending are identical to CC's, check the one thing that probably still isn't identical yet (hint: it comes AFTER the headers).

  8. added a commit that references this issue on Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions