66 "encoding/json"
77 "errors"
88 "fmt"
9+ "io"
910 "log/slog"
11+ "net/http"
1012 "net/url"
1113 "strings"
1214
@@ -27,7 +29,20 @@ import (
2729// It holds the anthropic client and model config
2830type Client struct {
2931 base.Config
30- clientFn func (context.Context ) (anthropic.Client , error )
32+ clientFn func (context.Context ) (anthropic.Client , error )
33+ lastHTTPResponse * http.Response
34+ }
35+
36+ func (c * Client ) getResponseTrailer () http.Header {
37+ if c .lastHTTPResponse == nil {
38+ return nil
39+ }
40+
41+ if c .lastHTTPResponse .Body != nil {
42+ _ , _ = io .Copy (io .Discard , c .lastHTTPResponse .Body )
43+ }
44+
45+ return c .lastHTTPResponse .Trailer
3146}
3247
3348// adjustMaxTokensForThinking checks if max_tokens needs adjustment for thinking_budget.
@@ -116,7 +131,14 @@ func NewClient(ctx context.Context, cfg *latest.ModelConfig, env environment.Pro
116131 }
117132 }
118133
119- var clientFn func (context.Context ) (anthropic.Client , error )
134+ anthropicClient := & Client {
135+ Config : base.Config {
136+ ModelConfig : * cfg ,
137+ ModelOptions : globalOptions ,
138+ Env : env ,
139+ },
140+ }
141+
120142 if gateway := globalOptions .Gateway (); gateway == "" {
121143 authToken , _ := env .Get (ctx , "ANTHROPIC_API_KEY" )
122144 if authToken == "" {
@@ -132,7 +154,7 @@ func NewClient(ctx context.Context, cfg *latest.ModelConfig, env environment.Pro
132154 requestOptions = append (requestOptions , option .WithBaseURL (cfg .BaseURL ))
133155 }
134156 client := anthropic .NewClient (requestOptions ... )
135- clientFn = func (context.Context ) (anthropic.Client , error ) {
157+ anthropicClient . clientFn = func (context.Context ) (anthropic.Client , error ) {
136158 return client , nil
137159 }
138160 } else {
@@ -143,7 +165,7 @@ func NewClient(ctx context.Context, cfg *latest.ModelConfig, env environment.Pro
143165 }
144166
145167 // When using a Gateway, tokens are short-lived.
146- clientFn = func (ctx context.Context ) (anthropic.Client , error ) {
168+ anthropicClient . clientFn = func (ctx context.Context ) (anthropic.Client , error ) {
147169 // Query a fresh auth token each time the client is used
148170 authToken , _ := env .Get (ctx , environment .DockerDesktopTokenEnv )
149171 if authToken == "" {
@@ -168,6 +190,7 @@ func NewClient(ctx context.Context, cfg *latest.ModelConfig, env environment.Pro
168190 }
169191
170192 client := anthropic .NewClient (
193+ option .WithResponseInto (& anthropicClient .lastHTTPResponse ),
171194 option .WithAuthToken (authToken ),
172195 option .WithAPIKey (authToken ),
173196 option .WithBaseURL (baseURL ),
@@ -180,14 +203,7 @@ func NewClient(ctx context.Context, cfg *latest.ModelConfig, env environment.Pro
180203
181204 slog .Debug ("Anthropic client created successfully" , "model" , cfg .Model )
182205
183- return & Client {
184- Config : base.Config {
185- ModelConfig : * cfg ,
186- ModelOptions : globalOptions ,
187- Env : env ,
188- },
189- clientFn : clientFn ,
190- }, nil
206+ return anthropicClient , nil
191207}
192208
193209// CreateChatCompletionStream creates a streaming chat completion request
@@ -304,7 +320,7 @@ func (c *Client) CreateChatCompletionStream(
304320
305321 stream := client .Messages .NewStreaming (ctx , params , betaHeader )
306322 trackUsage := c .ModelConfig .TrackUsage == nil || * c .ModelConfig .TrackUsage
307- ad := newStreamAdapter (stream , trackUsage )
323+ ad := c . newStreamAdapter (stream , trackUsage )
308324
309325 // Set up single retry for context length errors
310326 ad .retryFn = func () * streamAdapter {
@@ -321,7 +337,7 @@ func (c *Client) CreateChatCompletionStream(
321337 slog .Warn ("Retrying with clamped max_tokens after context length error" , "original max_tokens" , maxTokens , "clamped max_tokens" , newMaxTokens , "used tokens" , used )
322338 retryParams := params
323339 retryParams .MaxTokens = newMaxTokens
324- return newStreamAdapter (client .Messages .NewStreaming (ctx , retryParams , betaHeader ), trackUsage )
340+ return c . newStreamAdapter (client .Messages .NewStreaming (ctx , retryParams , betaHeader ), trackUsage )
325341 }
326342
327343 slog .Debug ("Anthropic chat completion stream created successfully" , "model" , c .ModelConfig .Model )
0 commit comments