From 6d1e20e940564e40bef360c683d020b7ad289299 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Tue, 23 Dec 2025 22:30:25 +0800 Subject: [PATCH] fix(claude_executor): update header logic for API key handling Refined header assignment to use `x-api-key` for Anthropic API requests, ensuring correct authorization behavior based on request attributes and URL validation. --- internal/runtime/executor/claude_executor.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/runtime/executor/claude_executor.go b/internal/runtime/executor/claude_executor.go index 67671026..0c31f424 100644 --- a/internal/runtime/executor/claude_executor.go +++ b/internal/runtime/executor/claude_executor.go @@ -662,7 +662,14 @@ func decodeResponseBody(body io.ReadCloser, contentEncoding string) (io.ReadClos } func applyClaudeHeaders(r *http.Request, auth *cliproxyauth.Auth, apiKey string, stream bool, extraBetas []string) { - r.Header.Set("Authorization", "Bearer "+apiKey) + useAPIKey := auth != nil && auth.Attributes != nil && strings.TrimSpace(auth.Attributes["api_key"]) != "" + isAnthropicBase := r.URL != nil && strings.EqualFold(r.URL.Scheme, "https") && strings.EqualFold(r.URL.Host, "api.anthropic.com") + if isAnthropicBase && useAPIKey { + r.Header.Del("Authorization") + r.Header.Set("x-api-key", apiKey) + } else { + r.Header.Set("Authorization", "Bearer "+apiKey) + } r.Header.Set("Content-Type", "application/json") var ginHeaders http.Header