anthropic.go

  1package provider
  2
  3import (
  4	"context"
  5	"encoding/json"
  6	"errors"
  7	"fmt"
  8	"io"
  9	"log/slog"
 10	"regexp"
 11	"strconv"
 12	"strings"
 13	"time"
 14
 15	"github.com/anthropics/anthropic-sdk-go"
 16	"github.com/anthropics/anthropic-sdk-go/bedrock"
 17	"github.com/anthropics/anthropic-sdk-go/option"
 18	"github.com/anthropics/anthropic-sdk-go/vertex"
 19	"github.com/charmbracelet/catwalk/pkg/catwalk"
 20	"github.com/charmbracelet/crush/internal/config"
 21	"github.com/charmbracelet/crush/internal/llm/tools"
 22	"github.com/charmbracelet/crush/internal/log"
 23	"github.com/charmbracelet/crush/internal/message"
 24)
 25
 26// Pre-compiled regex for parsing context limit errors.
 27var contextLimitRegex = regexp.MustCompile(`input length and ` + "`max_tokens`" + ` exceed context limit: (\d+) \+ (\d+) > (\d+)`)
 28
 29type anthropicClient struct {
 30	providerOptions   providerClientOptions
 31	tp                AnthropicClientType
 32	client            anthropic.Client
 33	adjustedMaxTokens int // Used when context limit is hit
 34}
 35
 36type AnthropicClient ProviderClient
 37
 38type AnthropicClientType string
 39
 40const (
 41	AnthropicClientTypeNormal  AnthropicClientType = "normal"
 42	AnthropicClientTypeBedrock AnthropicClientType = "bedrock"
 43	AnthropicClientTypeVertex  AnthropicClientType = "vertex"
 44)
 45
 46func newAnthropicClient(opts providerClientOptions, tp AnthropicClientType) AnthropicClient {
 47	return &anthropicClient{
 48		providerOptions: opts,
 49		tp:              tp,
 50		client:          createAnthropicClient(opts, tp),
 51	}
 52}
 53
 54func createAnthropicClient(opts providerClientOptions, tp AnthropicClientType) anthropic.Client {
 55	anthropicClientOptions := []option.RequestOption{}
 56
 57	// Check if Authorization header is provided in extra headers
 58	hasBearerAuth := false
 59	if opts.extraHeaders != nil {
 60		for key := range opts.extraHeaders {
 61			if strings.ToLower(key) == "authorization" {
 62				hasBearerAuth = true
 63				break
 64			}
 65		}
 66	}
 67
 68	isBearerToken := strings.HasPrefix(opts.apiKey, "Bearer ")
 69
 70	if opts.apiKey != "" && !hasBearerAuth {
 71		if isBearerToken {
 72			slog.Debug("API key starts with 'Bearer ', using as Authorization header")
 73			anthropicClientOptions = append(anthropicClientOptions, option.WithHeader("Authorization", opts.apiKey))
 74		} else {
 75			// Use standard X-Api-Key header
 76			anthropicClientOptions = append(anthropicClientOptions, option.WithAPIKey(opts.apiKey))
 77		}
 78	} else if hasBearerAuth {
 79		slog.Debug("Skipping X-Api-Key header because Authorization header is provided")
 80	}
 81
 82	if config.Get().Options.Debug {
 83		httpClient := log.NewHTTPClient()
 84		anthropicClientOptions = append(anthropicClientOptions, option.WithHTTPClient(httpClient))
 85	}
 86
 87	switch tp {
 88	case AnthropicClientTypeBedrock:
 89		anthropicClientOptions = append(anthropicClientOptions, bedrock.WithLoadDefaultConfig(context.Background()))
 90	case AnthropicClientTypeVertex:
 91		project := opts.extraParams["project"]
 92		location := opts.extraParams["location"]
 93		anthropicClientOptions = append(anthropicClientOptions, vertex.WithGoogleAuth(context.Background(), location, project))
 94	}
 95	for key, header := range opts.extraHeaders {
 96		anthropicClientOptions = append(anthropicClientOptions, option.WithHeaderAdd(key, header))
 97	}
 98	for key, value := range opts.extraBody {
 99		anthropicClientOptions = append(anthropicClientOptions, option.WithJSONSet(key, value))
100	}
101	return anthropic.NewClient(anthropicClientOptions...)
102}
103
104func (a *anthropicClient) convertMessages(messages []message.Message) (anthropicMessages []anthropic.MessageParam) {
105	for i, msg := range messages {
106		cache := false
107		if i > len(messages)-3 {
108			cache = true
109		}
110		switch msg.Role {
111		case message.User:
112			content := anthropic.NewTextBlock(msg.Content().String())
113			if cache && !a.providerOptions.disableCache {
114				content.OfText.CacheControl = anthropic.CacheControlEphemeralParam{
115					Type: "ephemeral",
116				}
117			}
118			var contentBlocks []anthropic.ContentBlockParamUnion
119			contentBlocks = append(contentBlocks, content)
120			for _, binaryContent := range msg.BinaryContent() {
121				base64Image := binaryContent.String(catwalk.InferenceProviderAnthropic)
122				imageBlock := anthropic.NewImageBlockBase64(binaryContent.MIMEType, base64Image)
123				contentBlocks = append(contentBlocks, imageBlock)
124			}
125			anthropicMessages = append(anthropicMessages, anthropic.NewUserMessage(contentBlocks...))
126
127		case message.Assistant:
128			blocks := []anthropic.ContentBlockParamUnion{}
129
130			// Add thinking blocks first if present (required when thinking is enabled with tool use)
131			if reasoningContent := msg.ReasoningContent(); reasoningContent.Thinking != "" {
132				thinkingBlock := anthropic.NewThinkingBlock(reasoningContent.Signature, reasoningContent.Thinking)
133				blocks = append(blocks, thinkingBlock)
134			}
135
136			if msg.Content().String() != "" {
137				content := anthropic.NewTextBlock(msg.Content().String())
138				if cache && !a.providerOptions.disableCache {
139					content.OfText.CacheControl = anthropic.CacheControlEphemeralParam{
140						Type: "ephemeral",
141					}
142				}
143				blocks = append(blocks, content)
144			}
145
146			for _, toolCall := range msg.ToolCalls() {
147				var inputMap map[string]any
148				err := json.Unmarshal([]byte(toolCall.Input), &inputMap)
149				if err != nil {
150					continue
151				}
152				blocks = append(blocks, anthropic.NewToolUseBlock(toolCall.ID, inputMap, toolCall.Name))
153			}
154
155			if len(blocks) == 0 {
156				slog.Warn("There is a message without content, investigate, this should not happen")
157				continue
158			}
159			anthropicMessages = append(anthropicMessages, anthropic.NewAssistantMessage(blocks...))
160
161		case message.Tool:
162			results := make([]anthropic.ContentBlockParamUnion, len(msg.ToolResults()))
163			for i, toolResult := range msg.ToolResults() {
164				results[i] = anthropic.NewToolResultBlock(toolResult.ToolCallID, toolResult.Content, toolResult.IsError)
165			}
166			anthropicMessages = append(anthropicMessages, anthropic.NewUserMessage(results...))
167		}
168	}
169	return
170}
171
172func (a *anthropicClient) convertTools(tools []tools.BaseTool) []anthropic.ToolUnionParam {
173	anthropicTools := make([]anthropic.ToolUnionParam, len(tools))
174
175	for i, tool := range tools {
176		info := tool.Info()
177		toolParam := anthropic.ToolParam{
178			Name:        info.Name,
179			Description: anthropic.String(info.Description),
180			InputSchema: anthropic.ToolInputSchemaParam{
181				Properties: info.Parameters,
182				// TODO: figure out how we can tell claude the required fields?
183			},
184		}
185
186		if i == len(tools)-1 && !a.providerOptions.disableCache {
187			toolParam.CacheControl = anthropic.CacheControlEphemeralParam{
188				Type: "ephemeral",
189			}
190		}
191
192		anthropicTools[i] = anthropic.ToolUnionParam{OfTool: &toolParam}
193	}
194
195	return anthropicTools
196}
197
198func (a *anthropicClient) finishReason(reason string) message.FinishReason {
199	switch reason {
200	case "end_turn":
201		return message.FinishReasonEndTurn
202	case "max_tokens":
203		return message.FinishReasonMaxTokens
204	case "tool_use":
205		return message.FinishReasonToolUse
206	case "stop_sequence":
207		return message.FinishReasonEndTurn
208	default:
209		return message.FinishReasonUnknown
210	}
211}
212
213func (a *anthropicClient) isThinkingEnabled() bool {
214	cfg := config.Get()
215	modelConfig := cfg.Models[config.SelectedModelTypeLarge]
216	if a.providerOptions.modelType == config.SelectedModelTypeSmall {
217		modelConfig = cfg.Models[config.SelectedModelTypeSmall]
218	}
219	return a.Model().CanReason && modelConfig.Think
220}
221
222func (a *anthropicClient) preparedMessages(messages []anthropic.MessageParam, tools []anthropic.ToolUnionParam) anthropic.MessageNewParams {
223	model := a.providerOptions.model(a.providerOptions.modelType)
224	var thinkingParam anthropic.ThinkingConfigParamUnion
225	cfg := config.Get()
226	modelConfig := cfg.Models[config.SelectedModelTypeLarge]
227	if a.providerOptions.modelType == config.SelectedModelTypeSmall {
228		modelConfig = cfg.Models[config.SelectedModelTypeSmall]
229	}
230	temperature := anthropic.Float(0)
231
232	maxTokens := model.DefaultMaxTokens
233	if modelConfig.MaxTokens > 0 {
234		maxTokens = modelConfig.MaxTokens
235	}
236	if a.isThinkingEnabled() {
237		thinkingParam = anthropic.ThinkingConfigParamOfEnabled(int64(float64(maxTokens) * 0.8))
238		temperature = anthropic.Float(1)
239	}
240	// Override max tokens if set in provider options
241	if a.providerOptions.maxTokens > 0 {
242		maxTokens = a.providerOptions.maxTokens
243	}
244
245	// Use adjusted max tokens if context limit was hit
246	if a.adjustedMaxTokens > 0 {
247		maxTokens = int64(a.adjustedMaxTokens)
248	}
249
250	systemBlocks := []anthropic.TextBlockParam{}
251
252	// Add custom system prompt prefix if configured
253	if a.providerOptions.systemPromptPrefix != "" {
254		systemBlocks = append(systemBlocks, anthropic.TextBlockParam{
255			Text: a.providerOptions.systemPromptPrefix,
256		})
257	}
258
259	systemBlocks = append(systemBlocks, anthropic.TextBlockParam{
260		Text: a.providerOptions.systemMessage,
261		CacheControl: anthropic.CacheControlEphemeralParam{
262			Type: "ephemeral",
263		},
264	})
265
266	return anthropic.MessageNewParams{
267		Model:       anthropic.Model(model.ID),
268		MaxTokens:   maxTokens,
269		Temperature: temperature,
270		Messages:    messages,
271		Tools:       tools,
272		Thinking:    thinkingParam,
273		System:      systemBlocks,
274	}
275}
276
277func (a *anthropicClient) send(ctx context.Context, messages []message.Message, tools []tools.BaseTool) (response *ProviderResponse, err error) {
278	attempts := 0
279	for {
280		attempts++
281		// Prepare messages on each attempt in case max_tokens was adjusted
282		preparedMessages := a.preparedMessages(a.convertMessages(messages), a.convertTools(tools))
283
284		var opts []option.RequestOption
285		if a.isThinkingEnabled() {
286			opts = append(opts, option.WithHeaderAdd("anthropic-beta", "interleaved-thinking-2025-05-14"))
287		}
288		anthropicResponse, err := a.client.Messages.New(
289			ctx,
290			preparedMessages,
291			opts...,
292		)
293		// If there is an error we are going to see if we can retry the call
294		if err != nil {
295			slog.Error("Anthropic API error", "error", err.Error(), "attempt", attempts, "max_retries", maxRetries)
296			retry, after, retryErr := a.shouldRetry(attempts, err)
297			if retryErr != nil {
298				return nil, retryErr
299			}
300			if retry {
301				slog.Warn("Retrying due to rate limit", "attempt", attempts, "max_retries", maxRetries)
302				select {
303				case <-ctx.Done():
304					return nil, ctx.Err()
305				case <-time.After(time.Duration(after) * time.Millisecond):
306					continue
307				}
308			}
309			return nil, retryErr
310		}
311
312		content := ""
313		for _, block := range anthropicResponse.Content {
314			if text, ok := block.AsAny().(anthropic.TextBlock); ok {
315				content += text.Text
316			}
317		}
318
319		return &ProviderResponse{
320			Content:   content,
321			ToolCalls: a.toolCalls(*anthropicResponse),
322			Usage:     a.usage(*anthropicResponse),
323		}, nil
324	}
325}
326
327func (a *anthropicClient) stream(ctx context.Context, messages []message.Message, tools []tools.BaseTool) <-chan ProviderEvent {
328	attempts := 0
329	eventChan := make(chan ProviderEvent)
330	go func() {
331		for {
332			attempts++
333			// Prepare messages on each attempt in case max_tokens was adjusted
334			preparedMessages := a.preparedMessages(a.convertMessages(messages), a.convertTools(tools))
335
336			var opts []option.RequestOption
337			if a.isThinkingEnabled() {
338				opts = append(opts, option.WithHeaderAdd("anthropic-beta", "interleaved-thinking-2025-05-14"))
339			}
340
341			anthropicStream := a.client.Messages.NewStreaming(
342				ctx,
343				preparedMessages,
344				opts...,
345			)
346			accumulatedMessage := anthropic.Message{}
347
348			currentToolCallID := ""
349			for anthropicStream.Next() {
350				event := anthropicStream.Current()
351				err := accumulatedMessage.Accumulate(event)
352				if err != nil {
353					slog.Warn("Error accumulating message", "error", err)
354					continue
355				}
356
357				switch event := event.AsAny().(type) {
358				case anthropic.ContentBlockStartEvent:
359					switch event.ContentBlock.Type {
360					case "text":
361						eventChan <- ProviderEvent{Type: EventContentStart}
362					case "tool_use":
363						currentToolCallID = event.ContentBlock.ID
364						eventChan <- ProviderEvent{
365							Type: EventToolUseStart,
366							ToolCall: &message.ToolCall{
367								ID:       event.ContentBlock.ID,
368								Name:     event.ContentBlock.Name,
369								Finished: false,
370							},
371						}
372					}
373
374				case anthropic.ContentBlockDeltaEvent:
375					if event.Delta.Type == "thinking_delta" && event.Delta.Thinking != "" {
376						eventChan <- ProviderEvent{
377							Type:     EventThinkingDelta,
378							Thinking: event.Delta.Thinking,
379						}
380					} else if event.Delta.Type == "signature_delta" && event.Delta.Signature != "" {
381						eventChan <- ProviderEvent{
382							Type:      EventSignatureDelta,
383							Signature: event.Delta.Signature,
384						}
385					} else if event.Delta.Type == "text_delta" && event.Delta.Text != "" {
386						eventChan <- ProviderEvent{
387							Type:    EventContentDelta,
388							Content: event.Delta.Text,
389						}
390					} else if event.Delta.Type == "input_json_delta" {
391						if currentToolCallID != "" {
392							eventChan <- ProviderEvent{
393								Type: EventToolUseDelta,
394								ToolCall: &message.ToolCall{
395									ID:       currentToolCallID,
396									Finished: false,
397									Input:    event.Delta.PartialJSON,
398								},
399							}
400						}
401					}
402				case anthropic.ContentBlockStopEvent:
403					if currentToolCallID != "" {
404						eventChan <- ProviderEvent{
405							Type: EventToolUseStop,
406							ToolCall: &message.ToolCall{
407								ID: currentToolCallID,
408							},
409						}
410						currentToolCallID = ""
411					} else {
412						eventChan <- ProviderEvent{Type: EventContentStop}
413					}
414
415				case anthropic.MessageStopEvent:
416					content := ""
417					for _, block := range accumulatedMessage.Content {
418						if text, ok := block.AsAny().(anthropic.TextBlock); ok {
419							content += text.Text
420						}
421					}
422
423					eventChan <- ProviderEvent{
424						Type: EventComplete,
425						Response: &ProviderResponse{
426							Content:      content,
427							ToolCalls:    a.toolCalls(accumulatedMessage),
428							Usage:        a.usage(accumulatedMessage),
429							FinishReason: a.finishReason(string(accumulatedMessage.StopReason)),
430						},
431						Content: content,
432					}
433				}
434			}
435
436			err := anthropicStream.Err()
437			if err == nil || errors.Is(err, io.EOF) {
438				close(eventChan)
439				return
440			}
441
442			// If there is an error we are going to see if we can retry the call
443			retry, after, retryErr := a.shouldRetry(attempts, err)
444			if retryErr != nil {
445				eventChan <- ProviderEvent{Type: EventError, Error: retryErr}
446				close(eventChan)
447				return
448			}
449			if retry {
450				slog.Warn("Retrying due to rate limit", "attempt", attempts, "max_retries", maxRetries)
451				select {
452				case <-ctx.Done():
453					// context cancelled
454					if ctx.Err() != nil {
455						eventChan <- ProviderEvent{Type: EventError, Error: ctx.Err()}
456					}
457					close(eventChan)
458					return
459				case <-time.After(time.Duration(after) * time.Millisecond):
460					continue
461				}
462			}
463			if ctx.Err() != nil {
464				eventChan <- ProviderEvent{Type: EventError, Error: ctx.Err()}
465			}
466
467			close(eventChan)
468			return
469		}
470	}()
471	return eventChan
472}
473
474func (a *anthropicClient) shouldRetry(attempts int, err error) (bool, int64, error) {
475	var apiErr *anthropic.Error
476	if !errors.As(err, &apiErr) {
477		return false, 0, err
478	}
479
480	if attempts > maxRetries {
481		return false, 0, fmt.Errorf("maximum retry attempts reached for rate limit: %d retries", maxRetries)
482	}
483
484	if apiErr.StatusCode == 401 {
485		a.providerOptions.apiKey, err = config.Get().Resolve(a.providerOptions.config.APIKey)
486		if err != nil {
487			return false, 0, fmt.Errorf("failed to resolve API key: %w", err)
488		}
489		a.client = createAnthropicClient(a.providerOptions, a.tp)
490		return true, 0, nil
491	}
492
493	// Handle context limit exceeded error (400 Bad Request)
494	if apiErr.StatusCode == 400 {
495		if adjusted, ok := a.handleContextLimitError(apiErr); ok {
496			a.adjustedMaxTokens = adjusted
497			slog.Debug("Adjusted max_tokens due to context limit", "new_max_tokens", adjusted)
498			return true, 0, nil
499		}
500	}
501
502	isOverloaded := strings.Contains(apiErr.Error(), "overloaded") || strings.Contains(apiErr.Error(), "rate limit exceeded")
503	if apiErr.StatusCode != 429 && apiErr.StatusCode != 529 && !isOverloaded {
504		return false, 0, err
505	}
506
507	retryMs := 0
508	retryAfterValues := apiErr.Response.Header.Values("Retry-After")
509
510	backoffMs := 2000 * (1 << (attempts - 1))
511	jitterMs := int(float64(backoffMs) * 0.2)
512	retryMs = backoffMs + jitterMs
513	if len(retryAfterValues) > 0 {
514		if _, err := fmt.Sscanf(retryAfterValues[0], "%d", &retryMs); err == nil {
515			retryMs = retryMs * 1000
516		}
517	}
518	return true, int64(retryMs), nil
519}
520
521// handleContextLimitError parses context limit error and returns adjusted max_tokens
522func (a *anthropicClient) handleContextLimitError(apiErr *anthropic.Error) (int, bool) {
523	// Parse error message like: "input length and max_tokens exceed context limit: 154978 + 50000 > 200000"
524	errorMsg := apiErr.Error()
525
526	matches := contextLimitRegex.FindStringSubmatch(errorMsg)
527
528	if len(matches) != 4 {
529		return 0, false
530	}
531
532	inputTokens, err1 := strconv.Atoi(matches[1])
533	contextLimit, err2 := strconv.Atoi(matches[3])
534
535	if err1 != nil || err2 != nil {
536		return 0, false
537	}
538
539	// Calculate safe max_tokens with a buffer of 1000 tokens
540	safeMaxTokens := contextLimit - inputTokens - 1000
541
542	// Ensure we don't go below a minimum threshold
543	safeMaxTokens = max(safeMaxTokens, 1000)
544
545	return safeMaxTokens, true
546}
547
548func (a *anthropicClient) toolCalls(msg anthropic.Message) []message.ToolCall {
549	var toolCalls []message.ToolCall
550
551	for _, block := range msg.Content {
552		switch variant := block.AsAny().(type) {
553		case anthropic.ToolUseBlock:
554			toolCall := message.ToolCall{
555				ID:       variant.ID,
556				Name:     variant.Name,
557				Input:    string(variant.Input),
558				Type:     string(variant.Type),
559				Finished: true,
560			}
561			toolCalls = append(toolCalls, toolCall)
562		}
563	}
564
565	return toolCalls
566}
567
568func (a *anthropicClient) usage(msg anthropic.Message) TokenUsage {
569	return TokenUsage{
570		InputTokens:         msg.Usage.InputTokens,
571		OutputTokens:        msg.Usage.OutputTokens,
572		CacheCreationTokens: msg.Usage.CacheCreationInputTokens,
573		CacheReadTokens:     msg.Usage.CacheReadInputTokens,
574	}
575}
576
577func (a *anthropicClient) Model() catwalk.Model {
578	return a.providerOptions.model(a.providerOptions.modelType)
579}