blob: 2afc69f618869df4748c41b06ff8d3a73cf612a2 [file] [log] [blame]
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001package ant
2
3import (
4 "bytes"
5 "cmp"
6 "context"
7 "encoding/json"
Josh Bleecher Snydera4500c92025-05-15 15:38:32 -07008 "errors"
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07009 "fmt"
10 "io"
11 "log/slog"
12 "math/rand/v2"
13 "net/http"
Josh Bleecher Snyderf2b5ee02025-07-21 16:42:53 -070014 "os"
15 "path/filepath"
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070016 "strings"
17 "testing"
18 "time"
19
20 "sketch.dev/llm"
21)
22
23const (
Josh Bleecher Snyder0efb29d2025-05-22 21:05:04 -070024 DefaultModel = Claude4Sonnet
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070025 // See https://docs.anthropic.com/en/docs/about-claude/models/all-models for
26 // current maximums. There's currently a flag to enable 128k output (output-128k-2025-02-19)
27 DefaultMaxTokens = 8192
28 DefaultURL = "https://api.anthropic.com/v1/messages"
29)
30
31const (
32 Claude35Sonnet = "claude-3-5-sonnet-20241022"
33 Claude35Haiku = "claude-3-5-haiku-20241022"
34 Claude37Sonnet = "claude-3-7-sonnet-20250219"
Josh Bleecher Snyder0e8073a2025-05-22 21:04:51 -070035 Claude4Sonnet = "claude-sonnet-4-20250514"
36 Claude4Opus = "claude-opus-4-20250514"
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070037)
38
Philip Zeyligerb8a8f352025-06-02 07:39:37 -070039// TokenContextWindow returns the maximum token context window size for this service
40func (s *Service) TokenContextWindow() int {
41 model := s.Model
42 if model == "" {
43 model = DefaultModel
44 }
45
46 switch model {
47 case Claude35Sonnet, Claude37Sonnet:
48 return 200000
49 case Claude35Haiku:
50 return 200000
51 case Claude4Sonnet, Claude4Opus:
52 return 200000
53 default:
54 // Default for unknown models
55 return 200000
56 }
57}
58
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070059// Service provides Claude completions.
60// Fields should not be altered concurrently with calling any method on Service.
61type Service struct {
62 HTTPC *http.Client // defaults to http.DefaultClient if nil
63 URL string // defaults to DefaultURL if empty
64 APIKey string // must be non-empty
65 Model string // defaults to DefaultModel if empty
66 MaxTokens int // defaults to DefaultMaxTokens if zero
67}
68
69var _ llm.Service = (*Service)(nil)
70
71type content struct {
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070072 // https://docs.anthropic.com/en/api/messages
73 ID string `json:"id,omitempty"`
74 Type string `json:"type,omitempty"`
Philip Zeyliger72252cb2025-05-10 17:00:08 -070075
76 // Subtly, an empty string appears in tool results often, so we have
77 // to distinguish between empty string and no string.
78 // Underlying error looks like one of:
79 // "messages.46.content.0.tool_result.content.0.text.text: Field required""
80 // "messages.1.content.1.tool_use.text: Extra inputs are not permitted"
81 //
82 // I haven't found a super great source for the API, but
83 // https://github.com/anthropics/anthropic-sdk-typescript/blob/main/src/resources/messages/messages.ts
84 // is somewhat acceptable but hard to read.
85 Text *string `json:"text,omitempty"`
86 MediaType string `json:"media_type,omitempty"` // for image
87 Source json.RawMessage `json:"source,omitempty"` // for image
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070088
89 // for thinking
90 Thinking string `json:"thinking,omitempty"`
Philip Zeyliger72252cb2025-05-10 17:00:08 -070091 Data string `json:"data,omitempty"` // for redacted_thinking or image
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070092 Signature string `json:"signature,omitempty"` // for thinking
93
94 // for tool_use
95 ToolName string `json:"name,omitempty"`
96 ToolInput json.RawMessage `json:"input,omitempty"`
97
98 // for tool_result
Philip Zeyliger72252cb2025-05-10 17:00:08 -070099 ToolUseID string `json:"tool_use_id,omitempty"`
100 ToolError bool `json:"is_error,omitempty"`
101 // note the recursive nature here; message looks like:
102 // {
103 // "role": "user",
104 // "content": [
105 // {
106 // "type": "tool_result",
107 // "tool_use_id": "toolu_01A09q90qw90lq917835lq9",
108 // "content": [
109 // {"type": "text", "text": "15 degrees"},
110 // {
111 // "type": "image",
112 // "source": {
113 // "type": "base64",
114 // "media_type": "image/jpeg",
115 // "data": "/9j/4AAQSkZJRg...",
116 // }
117 // }
118 // ]
119 // }
120 // ]
121 //}
122 ToolResult []content `json:"content,omitempty"`
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700123
124 // timing information for tool_result; not sent to Claude
125 StartTime *time.Time `json:"-"`
126 EndTime *time.Time `json:"-"`
127
128 CacheControl json.RawMessage `json:"cache_control,omitempty"`
129}
130
131// message represents a message in the conversation.
132type message struct {
133 Role string `json:"role"`
134 Content []content `json:"content"`
135 ToolUse *toolUse `json:"tool_use,omitempty"` // use to control whether/which tool to use
136}
137
138// toolUse represents a tool use in the message content.
139type toolUse struct {
140 ID string `json:"id"`
141 Name string `json:"name"`
142}
143
144// tool represents a tool available to Claude.
145type tool struct {
146 Name string `json:"name"`
147 // Type is used by the text editor tool; see
148 // https://docs.anthropic.com/en/docs/build-with-claude/tool-use/text-editor-tool
149 Type string `json:"type,omitempty"`
150 Description string `json:"description,omitempty"`
151 InputSchema json.RawMessage `json:"input_schema,omitempty"`
152}
153
154// usage represents the billing and rate-limit usage.
155type usage struct {
156 InputTokens uint64 `json:"input_tokens"`
157 CacheCreationInputTokens uint64 `json:"cache_creation_input_tokens"`
158 CacheReadInputTokens uint64 `json:"cache_read_input_tokens"`
159 OutputTokens uint64 `json:"output_tokens"`
160 CostUSD float64 `json:"cost_usd"`
161}
162
163func (u *usage) Add(other usage) {
164 u.InputTokens += other.InputTokens
165 u.CacheCreationInputTokens += other.CacheCreationInputTokens
166 u.CacheReadInputTokens += other.CacheReadInputTokens
167 u.OutputTokens += other.OutputTokens
168 u.CostUSD += other.CostUSD
169}
170
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700171// response represents the response from the message API.
172type response struct {
173 ID string `json:"id"`
174 Type string `json:"type"`
175 Role string `json:"role"`
176 Model string `json:"model"`
177 Content []content `json:"content"`
178 StopReason string `json:"stop_reason"`
179 StopSequence *string `json:"stop_sequence,omitempty"`
180 Usage usage `json:"usage"`
181}
182
183type toolChoice struct {
184 Type string `json:"type"`
185 Name string `json:"name,omitempty"`
186}
187
188// https://docs.anthropic.com/en/api/messages#body-system
189type systemContent struct {
190 Text string `json:"text,omitempty"`
191 Type string `json:"type,omitempty"`
192 CacheControl json.RawMessage `json:"cache_control,omitempty"`
193}
194
195// request represents the request payload for creating a message.
196type request struct {
197 Model string `json:"model"`
198 Messages []message `json:"messages"`
199 ToolChoice *toolChoice `json:"tool_choice,omitempty"`
200 MaxTokens int `json:"max_tokens"`
201 Tools []*tool `json:"tools,omitempty"`
202 Stream bool `json:"stream,omitempty"`
203 System []systemContent `json:"system,omitempty"`
204 Temperature float64 `json:"temperature,omitempty"`
205 TopK int `json:"top_k,omitempty"`
206 TopP float64 `json:"top_p,omitempty"`
207 StopSequences []string `json:"stop_sequences,omitempty"`
208
209 TokenEfficientToolUse bool `json:"-"` // DO NOT USE, broken on Anthropic's side as of 2025-02-28
210}
211
Josh Bleecher Snyderf2b5ee02025-07-21 16:42:53 -0700212const dumpText = false // debugging toggle to dump raw communications with Claude using dumpToFile
213
214// dumpToFile writes the content to a timestamped file in ~/.cache/sketch/, with typ in the filename.
215func dumpToFile(typ string, content []byte) error {
216 if !dumpText {
217 return nil
218 }
219 homeDir, err := os.UserHomeDir()
220 if err != nil {
221 return err
222 }
223 cacheDir := filepath.Join(homeDir, ".cache", "sketch")
224 err = os.MkdirAll(cacheDir, 0o700)
225 if err != nil {
226 return err
227 }
228 now := time.Now()
229 filename := fmt.Sprintf("%d_%s.txt", now.UnixMilli(), typ)
230 filePath := filepath.Join(cacheDir, filename)
231 return os.WriteFile(filePath, content, 0o600)
232}
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700233
234func mapped[Slice ~[]E, E, T any](s Slice, f func(E) T) []T {
235 out := make([]T, len(s))
236 for i, v := range s {
237 out[i] = f(v)
238 }
239 return out
240}
241
242func inverted[K, V cmp.Ordered](m map[K]V) map[V]K {
243 inv := make(map[V]K)
244 for k, v := range m {
245 if _, ok := inv[v]; ok {
246 panic(fmt.Errorf("inverted map has multiple keys for value %v", v))
247 }
248 inv[v] = k
249 }
250 return inv
251}
252
253var (
254 fromLLMRole = map[llm.MessageRole]string{
255 llm.MessageRoleAssistant: "assistant",
256 llm.MessageRoleUser: "user",
257 }
258 toLLMRole = inverted(fromLLMRole)
259
260 fromLLMContentType = map[llm.ContentType]string{
261 llm.ContentTypeText: "text",
262 llm.ContentTypeThinking: "thinking",
263 llm.ContentTypeRedactedThinking: "redacted_thinking",
264 llm.ContentTypeToolUse: "tool_use",
265 llm.ContentTypeToolResult: "tool_result",
266 }
267 toLLMContentType = inverted(fromLLMContentType)
268
269 fromLLMToolChoiceType = map[llm.ToolChoiceType]string{
270 llm.ToolChoiceTypeAuto: "auto",
271 llm.ToolChoiceTypeAny: "any",
272 llm.ToolChoiceTypeNone: "none",
273 llm.ToolChoiceTypeTool: "tool",
274 }
275
276 toLLMStopReason = map[string]llm.StopReason{
277 "stop_sequence": llm.StopReasonStopSequence,
278 "max_tokens": llm.StopReasonMaxTokens,
279 "end_turn": llm.StopReasonEndTurn,
280 "tool_use": llm.StopReasonToolUse,
Josh Bleecher Snyder0e8073a2025-05-22 21:04:51 -0700281 "refusal": llm.StopReasonRefusal,
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700282 }
283)
284
285func fromLLMCache(c bool) json.RawMessage {
286 if !c {
287 return nil
288 }
289 return json.RawMessage(`{"type":"ephemeral"}`)
290}
291
292func fromLLMContent(c llm.Content) content {
Philip Zeyliger72252cb2025-05-10 17:00:08 -0700293 var toolResult []content
294 if len(c.ToolResult) > 0 {
295 toolResult = make([]content, len(c.ToolResult))
296 for i, tr := range c.ToolResult {
297 // For image content inside a tool_result, we need to map it to "image" type
298 if tr.MediaType != "" && tr.MediaType == "image/jpeg" || tr.MediaType == "image/png" {
299 // Format as an image for Claude
300 toolResult[i] = content{
301 Type: "image",
302 Source: json.RawMessage(fmt.Sprintf(`{"type":"base64","media_type":"%s","data":"%s"}`,
303 tr.MediaType, tr.Data)),
304 }
305 } else {
306 toolResult[i] = fromLLMContent(tr)
307 }
308 }
309 }
310
311 d := content{
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700312 ID: c.ID,
313 Type: fromLLMContentType[c.Type],
Philip Zeyliger72252cb2025-05-10 17:00:08 -0700314 MediaType: c.MediaType,
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700315 Thinking: c.Thinking,
316 Data: c.Data,
317 Signature: c.Signature,
318 ToolName: c.ToolName,
319 ToolInput: c.ToolInput,
320 ToolUseID: c.ToolUseID,
321 ToolError: c.ToolError,
Philip Zeyliger72252cb2025-05-10 17:00:08 -0700322 ToolResult: toolResult,
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700323 CacheControl: fromLLMCache(c.Cache),
324 }
Philip Zeyliger72252cb2025-05-10 17:00:08 -0700325 // Anthropic API complains if Text is specified when it shouldn't be
326 // or not specified when it's the empty string.
327 if c.Type != llm.ContentTypeToolResult && c.Type != llm.ContentTypeToolUse {
328 d.Text = &c.Text
329 }
330 return d
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700331}
332
333func fromLLMToolUse(tu *llm.ToolUse) *toolUse {
334 if tu == nil {
335 return nil
336 }
337 return &toolUse{
338 ID: tu.ID,
339 Name: tu.Name,
340 }
341}
342
343func fromLLMMessage(msg llm.Message) message {
344 return message{
345 Role: fromLLMRole[msg.Role],
346 Content: mapped(msg.Content, fromLLMContent),
347 ToolUse: fromLLMToolUse(msg.ToolUse),
348 }
349}
350
351func fromLLMToolChoice(tc *llm.ToolChoice) *toolChoice {
352 if tc == nil {
353 return nil
354 }
355 return &toolChoice{
356 Type: fromLLMToolChoiceType[tc.Type],
357 Name: tc.Name,
358 }
359}
360
361func fromLLMTool(t *llm.Tool) *tool {
362 return &tool{
363 Name: t.Name,
364 Type: t.Type,
365 Description: t.Description,
366 InputSchema: t.InputSchema,
367 }
368}
369
370func fromLLMSystem(s llm.SystemContent) systemContent {
371 return systemContent{
372 Text: s.Text,
373 Type: s.Type,
374 CacheControl: fromLLMCache(s.Cache),
375 }
376}
377
378func (s *Service) fromLLMRequest(r *llm.Request) *request {
379 return &request{
380 Model: cmp.Or(s.Model, DefaultModel),
381 Messages: mapped(r.Messages, fromLLMMessage),
382 MaxTokens: cmp.Or(s.MaxTokens, DefaultMaxTokens),
383 ToolChoice: fromLLMToolChoice(r.ToolChoice),
384 Tools: mapped(r.Tools, fromLLMTool),
385 System: mapped(r.System, fromLLMSystem),
386 }
387}
388
389func toLLMUsage(u usage) llm.Usage {
390 return llm.Usage{
391 InputTokens: u.InputTokens,
392 CacheCreationInputTokens: u.CacheCreationInputTokens,
393 CacheReadInputTokens: u.CacheReadInputTokens,
394 OutputTokens: u.OutputTokens,
395 CostUSD: u.CostUSD,
396 }
397}
398
399func toLLMContent(c content) llm.Content {
Philip Zeyliger72252cb2025-05-10 17:00:08 -0700400 // Convert toolResult from []content to []llm.Content
401 var toolResultContents []llm.Content
402 if len(c.ToolResult) > 0 {
403 toolResultContents = make([]llm.Content, len(c.ToolResult))
404 for i, tr := range c.ToolResult {
405 toolResultContents[i] = toLLMContent(tr)
406 }
407 }
408
409 ret := llm.Content{
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700410 ID: c.ID,
411 Type: toLLMContentType[c.Type],
Philip Zeyliger72252cb2025-05-10 17:00:08 -0700412 MediaType: c.MediaType,
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700413 Thinking: c.Thinking,
414 Data: c.Data,
415 Signature: c.Signature,
416 ToolName: c.ToolName,
417 ToolInput: c.ToolInput,
418 ToolUseID: c.ToolUseID,
419 ToolError: c.ToolError,
Philip Zeyliger72252cb2025-05-10 17:00:08 -0700420 ToolResult: toolResultContents,
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700421 }
Philip Zeyliger72252cb2025-05-10 17:00:08 -0700422 if c.Text != nil {
423 ret.Text = *c.Text
424 }
425 return ret
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700426}
427
428func toLLMResponse(r *response) *llm.Response {
429 return &llm.Response{
430 ID: r.ID,
431 Type: r.Type,
432 Role: toLLMRole[r.Role],
433 Model: r.Model,
434 Content: mapped(r.Content, toLLMContent),
435 StopReason: toLLMStopReason[r.StopReason],
436 StopSequence: r.StopSequence,
437 Usage: toLLMUsage(r.Usage),
438 }
439}
440
441// Do sends a request to Anthropic.
442func (s *Service) Do(ctx context.Context, ir *llm.Request) (*llm.Response, error) {
443 request := s.fromLLMRequest(ir)
444
445 var payload []byte
446 var err error
447 if dumpText || testing.Testing() {
448 payload, err = json.MarshalIndent(request, "", " ")
449 } else {
450 payload, err = json.Marshal(request)
451 payload = append(payload, '\n')
452 }
453 if err != nil {
454 return nil, err
455 }
456
457 if false {
458 fmt.Printf("claude request payload:\n%s\n", payload)
459 }
460
461 backoff := []time.Duration{15 * time.Second, 30 * time.Second, time.Minute}
462 largerMaxTokens := false
463 var partialUsage usage
464
465 url := cmp.Or(s.URL, DefaultURL)
466 httpc := cmp.Or(s.HTTPC, http.DefaultClient)
467
468 // retry loop
Josh Bleecher Snydera4500c92025-05-15 15:38:32 -0700469 var errs error // accumulated errors across all attempts
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700470 for attempts := 0; ; attempts++ {
Josh Bleecher Snydera4500c92025-05-15 15:38:32 -0700471 if attempts > 10 {
472 return nil, fmt.Errorf("anthropic request failed after %d attempts: %w", attempts, errs)
473 }
474 if attempts > 0 {
475 sleep := backoff[min(attempts, len(backoff)-1)] + time.Duration(rand.Int64N(int64(time.Second)))
476 slog.WarnContext(ctx, "anthropic request sleep before retry", "sleep", sleep, "attempts", attempts)
477 time.Sleep(sleep)
478 }
Josh Bleecher Snyderf2b5ee02025-07-21 16:42:53 -0700479 if err := dumpToFile("request", payload); err != nil {
480 slog.WarnContext(ctx, "failed to dump request to file", "error", err)
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700481 }
482 req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(payload))
483 if err != nil {
Josh Bleecher Snydera4500c92025-05-15 15:38:32 -0700484 return nil, errors.Join(errs, err)
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700485 }
486
487 req.Header.Set("Content-Type", "application/json")
488 req.Header.Set("X-API-Key", s.APIKey)
489 req.Header.Set("Anthropic-Version", "2023-06-01")
490
491 var features []string
492 if request.TokenEfficientToolUse {
493 features = append(features, "token-efficient-tool-use-2025-02-19")
494 }
495 if largerMaxTokens {
496 features = append(features, "output-128k-2025-02-19")
497 request.MaxTokens = 128 * 1024
498 }
499 if len(features) > 0 {
500 req.Header.Set("anthropic-beta", strings.Join(features, ","))
501 }
502
503 resp, err := httpc.Do(req)
504 if err != nil {
Josh Bleecher Snyder3b5646f2025-05-23 16:47:53 +0000505 // Don't retry httprr cache misses
506 if strings.Contains(err.Error(), "cached HTTP response not found") {
507 return nil, err
508 }
Josh Bleecher Snydera4500c92025-05-15 15:38:32 -0700509 errs = errors.Join(errs, err)
510 continue
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700511 }
Josh Bleecher Snydera4500c92025-05-15 15:38:32 -0700512 buf, err := io.ReadAll(resp.Body)
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700513 resp.Body.Close()
Josh Bleecher Snydera4500c92025-05-15 15:38:32 -0700514 if err != nil {
515 errs = errors.Join(errs, err)
516 continue
517 }
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700518
519 switch {
520 case resp.StatusCode == http.StatusOK:
Josh Bleecher Snyderf2b5ee02025-07-21 16:42:53 -0700521 if err := dumpToFile("response", buf); err != nil {
522 slog.WarnContext(ctx, "failed to dump response to file", "error", err)
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700523 }
524 var response response
525 err = json.NewDecoder(bytes.NewReader(buf)).Decode(&response)
526 if err != nil {
Josh Bleecher Snydera4500c92025-05-15 15:38:32 -0700527 return nil, errors.Join(errs, err)
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700528 }
529 if response.StopReason == "max_tokens" && !largerMaxTokens {
Josh Bleecher Snyder29fea842025-05-06 01:51:09 +0000530 slog.InfoContext(ctx, "anthropic_retrying_with_larger_tokens", "message", "Retrying Anthropic API call with larger max tokens size")
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700531 // Retry with more output tokens.
532 largerMaxTokens = true
Josh Bleecher Snyder59bb27d2025-06-05 07:32:10 -0700533 response.Usage.CostUSD = llm.CostUSDFromResponse(resp.Header)
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700534 partialUsage = response.Usage
535 continue
536 }
537
538 // Calculate and set the cost_usd field
539 if largerMaxTokens {
540 response.Usage.Add(partialUsage)
541 }
Josh Bleecher Snyder59bb27d2025-06-05 07:32:10 -0700542 response.Usage.CostUSD = llm.CostUSDFromResponse(resp.Header)
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700543
544 return toLLMResponse(&response), nil
545 case resp.StatusCode >= 500 && resp.StatusCode < 600:
Josh Bleecher Snydera4500c92025-05-15 15:38:32 -0700546 // server error, retry
547 slog.WarnContext(ctx, "anthropic_request_failed", "response", string(buf), "status_code", resp.StatusCode)
548 errs = errors.Join(errs, fmt.Errorf("status %v: %s", resp.Status, buf))
549 continue
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700550 case resp.StatusCode == 429:
Josh Bleecher Snydera4500c92025-05-15 15:38:32 -0700551 // rate limited, retry
552 slog.WarnContext(ctx, "anthropic_request_rate_limited", "response", string(buf))
553 errs = errors.Join(errs, fmt.Errorf("status %v: %s", resp.Status, buf))
554 continue
555 case resp.StatusCode >= 400 && resp.StatusCode < 500:
556 // some other 400, probably unrecoverable
557 slog.WarnContext(ctx, "anthropic_request_failed", "response", string(buf), "status_code", resp.StatusCode)
558 return nil, errors.Join(errs, fmt.Errorf("status %v: %s", resp.Status, buf))
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700559 default:
Josh Bleecher Snydera4500c92025-05-15 15:38:32 -0700560 // ...retry, I guess?
561 slog.WarnContext(ctx, "anthropic_request_failed", "response", string(buf), "status_code", resp.StatusCode)
562 errs = errors.Join(errs, fmt.Errorf("status %v: %s", resp.Status, buf))
563 continue
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700564 }
565 }
566}