{"record":{"id":"dfac1581e66d5fc1","repo":"micro/go-micro","slug":"anthropic-stream-error-s","errorCode":null,"errorMessage":"anthropic stream error: %s","messagePattern":"anthropic stream error: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/anthropic/anthropic.go","lineNumber":330,"sourceCode":"\t\t\tif chunk.Delta.Type == \"text_delta\" && chunk.Delta.Text != \"\" {\n\t\t\t\treturn &ai.Response{Reply: chunk.Delta.Text}, nil\n\t\t\t}\n\t\tcase \"message_start\":\n\t\t\tif chunk.Message.Usage.InputTokens > 0 || chunk.Message.Usage.OutputTokens > 0 {\n\t\t\t\treturn &ai.Response{Usage: usage(chunk.Message.Usage.InputTokens, chunk.Message.Usage.OutputTokens)}, nil\n\t\t\t}\n\t\tcase \"message_delta\":\n\t\t\tif chunk.Delta.StopReason != \"\" || chunk.Usage != nil {\n\t\t\t\tresponse := &ai.Response{StopReason: chunk.Delta.StopReason}\n\t\t\t\tif chunk.Usage != nil {\n\t\t\t\t\tresponse.Usage = usage(chunk.Usage.InputTokens, chunk.Usage.OutputTokens)\n\t\t\t\t}\n\t\t\t\treturn response, nil\n\t\t\t}\n\t\tcase \"message_stop\":\n\t\t\treturn nil, io.EOF\n\t\tcase \"error\":\n\t\t\treturn nil, fmt.Errorf(\"anthropic stream error: %s\", data)\n\t\t}\n\t}\n\tif err := s.scanner.Err(); err != nil {\n\t\treturn nil, err\n\t}\n\treturn nil, io.EOF\n}\n\nfunc (s *streamReader) Close() error {\n\tif s.closed {\n\t\treturn nil\n\t}\n\ts.closed = true\n\treturn s.body.Close()\n}\n\nfunc usage(input, output int) ai.Usage {\n\treturn ai.Usage{InputTokens: input, OutputTokens: output, TotalTokens: input + output}","sourceCodeStart":312,"sourceCodeEnd":348,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/anthropic/anthropic.go#L312-L348","documentation":"Returned by streamReader.Recv when Anthropic sends a terminal \"error\" event in the SSE stream. The raw JSON payload of the error event is embedded in the message so the developer can see Anthropic's error type (overloaded_error, api_error, etc.) that aborted the stream mid-generation.","triggerScenarios":"Anthropic emits an SSE event with type \"error\" during streaming — typically overloaded_error (server capacity), api_error, or a mid-stream invalid_request_error after the stream already started with 200 OK.","commonSituations":"Long generations interrupted by Anthropic 529/overloaded conditions, transient infra failures mid-stream, or requests hitting degraded regions.","solutions":["Parse the embedded JSON payload to read error.type; retry the whole stream (with backoff) for overloaded_error or api_error since partial output may exist before the error.","Check Anthropic's status page (status.anthropic.com) if overloaded_error repeats.","Implement resumable handling: keep already-received chunks and restart the request if the stream errors.","Reduce max_tokens/request size if overloaded errors correlate with large requests."],"exampleFix":"// before\nresp, err := stream.Recv()\nif err != nil { return err }\n// after\nresp, err := stream.Recv()\nif err != nil {\n\tif strings.Contains(err.Error(), \"overloaded_error\") {\n\t\ttime.Sleep(backoff); return retryStream(ctx, req)\n\t}\n\treturn err\n}","handlingStrategy":"retry","validationCode":null,"typeGuard":"func isOverloadedStreamErr(err error) bool {\n\treturn err != nil && strings.Contains(err.Error(), \"overloaded_error\")\n}","tryCatchPattern":"for attempt := 0; attempt < 3; attempt++ {\n\tstream, err := provider.Stream(ctx, req)\n\tif err != nil { return err }\n\tfor {\n\t\tresp, err := stream.Recv()\n\t\tif errors.Is(err, io.EOF) { return nil }\n\t\tif err != nil {\n\t\t\tif isOverloadedStreamErr(err) { time.Sleep(backoff(attempt)); break } // restart stream\n\t\t\treturn err\n\t\t}\n\t\t// accumulate resp.Reply\n\t}\n}","preventionTips":["Retry whole streams on overloaded_error — Anthropic may abort mid-generation under load","Keep received partial output before restarting so nothing is lost","Follow status.anthropic.com for recurring overload windows","Reduce request size if overloads correlate with large payloads"],"tags":["sse","streaming","anthropic","overloaded","mid-stream-error"],"backgroundTag":"anthropic-stream-error-event","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}