on main help sign in
fix(claude): simplify code and fix potential unauthorized error
9f536baAndrey Nering
internal/agent/coordinator.go+7−19
@@ -475,27 +475,15 @@ func (c *coordinator) buildAgentModels(ctx context.Context) (Model, Model, error
475}
475}
476
476
477func (c *coordinator) buildAnthropicProvider(baseURL, apiKey string, headers map[string]string) (fantasy.Provider, error) {
477func (c *coordinator) buildAnthropicProvider(baseURL, apiKey string, headers map[string]string) (fantasy.Provider, error) {
478− hasBearerAuth := false
479− for key := range headers {
480− if strings.ToLower(key) == "authorization" {
481− hasBearerAuth = true
482− break
483− }
484− }
485−
486− isBearerToken := strings.HasPrefix(apiKey, "Bearer ")
487−
488 var opts []anthropic.Option
478 var opts []anthropic.Option
489− if apiKey != "" && !hasBearerAuth {
490− if isBearerToken {
491− slog.Debug("API key starts with 'Bearer ', using as Authorization header")
492− headers["Authorization"] = apiKey
493− apiKey = "" // clear apiKey to avoid using X-Api-Key header
494− }
495− }
496
479
480+ if strings.HasPrefix(apiKey, "Bearer ") {
481+ // NOTE: Prevent the SDK from picking up the API key from env.
482+ os.Setenv("ANTHROPIC_API_KEY", "")
483+
484+ headers["Authorization"] = apiKey
497− if apiKey != "" {
485+ } else if apiKey != "" {
498− // Use standard X-Api-Key header
486+ // X-Api-Key header
499 opts = append(opts, anthropic.WithAPIKey(apiKey))
487 opts = append(opts, anthropic.WithAPIKey(apiKey))
500 }
488 }
501
489
internal/config/config.go+1−3
@@ -117,9 +117,7 @@ type ProviderConfig struct {
117}
117}
118
118
119func (pc *ProviderConfig) SetupClaudeCode() {
119func (pc *ProviderConfig) SetupClaudeCode() {
120− if !strings.HasPrefix(pc.APIKey, "Bearer ") {
121− pc.APIKey = fmt.Sprintf("Bearer %s", pc.APIKey)
120+ pc.APIKey = fmt.Sprintf("Bearer %s", pc.OAuthToken.AccessToken)
122− }
123 pc.SystemPromptPrefix = "You are Claude Code, Anthropic's official CLI for Claude."
121 pc.SystemPromptPrefix = "You are Claude Code, Anthropic's official CLI for Claude."
124 pc.ExtraHeaders["anthropic-version"] = "2023-06-01"
122 pc.ExtraHeaders["anthropic-version"] = "2023-06-01"
125
123