diff --git a/internal/agent/coordinator.go b/internal/agent/coordinator.go index 4bfcc0062ae9a06dc858989f2cce925976d6d32b..c01ae619726343e33992b1c0c98066697e0b5f7f 100644 --- a/internal/agent/coordinator.go +++ b/internal/agent/coordinator.go @@ -475,27 +475,15 @@ func (c *coordinator) buildAgentModels(ctx context.Context) (Model, Model, error } func (c *coordinator) buildAnthropicProvider(baseURL, apiKey string, headers map[string]string) (fantasy.Provider, error) { - hasBearerAuth := false - for key := range headers { - if strings.ToLower(key) == "authorization" { - hasBearerAuth = true - break - } - } - - isBearerToken := strings.HasPrefix(apiKey, "Bearer ") - var opts []anthropic.Option - if apiKey != "" && !hasBearerAuth { - if isBearerToken { - slog.Debug("API key starts with 'Bearer ', using as Authorization header") - headers["Authorization"] = apiKey - apiKey = "" // clear apiKey to avoid using X-Api-Key header - } - } - if apiKey != "" { - // Use standard X-Api-Key header + if strings.HasPrefix(apiKey, "Bearer ") { + // NOTE: Prevent the SDK from picking up the API key from env. + os.Setenv("ANTHROPIC_API_KEY", "") + + headers["Authorization"] = apiKey + } else if apiKey != "" { + // X-Api-Key header opts = append(opts, anthropic.WithAPIKey(apiKey)) } diff --git a/internal/config/config.go b/internal/config/config.go index 66e90e062cad91fb088d8ad97970a4f790960d92..2d1882ba876e3d0ab1ea284dce7edbbe3503013a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -117,9 +117,7 @@ type ProviderConfig struct { } func (pc *ProviderConfig) SetupClaudeCode() { - if !strings.HasPrefix(pc.APIKey, "Bearer ") { - pc.APIKey = fmt.Sprintf("Bearer %s", pc.APIKey) - } + pc.APIKey = fmt.Sprintf("Bearer %s", pc.OAuthToken.AccessToken) pc.SystemPromptPrefix = "You are Claude Code, Anthropic's official CLI for Claude." pc.ExtraHeaders["anthropic-version"] = "2023-06-01"