Skip to content

Commit cca8638

Browse files
authored
Merge pull request #2298 from dgageot/board/remove-trailing-headers-rate-limit-handl-873dc5c9
Remove trailing headers handling for rate limit headers
2 parents a70ca27 + 1e8d0e1 commit cca8638

File tree

7 files changed

+18
-79
lines changed

7 files changed

+18
-79
lines changed

pkg/chat/chat.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,12 @@ type MessageStreamChoice struct {
146146

147147
// MessageStreamResponse represents a streaming response from the model
148148
type MessageStreamResponse struct {
149-
ID string `json:"id"`
150-
Object string `json:"object"`
151-
Created int64 `json:"created"`
152-
Model string `json:"model"`
153-
Choices []MessageStreamChoice `json:"choices"`
154-
Usage *Usage `json:"usage,omitempty"`
155-
RateLimit *RateLimit `json:"rate_limit,omitempty"`
149+
ID string `json:"id"`
150+
Object string `json:"object"`
151+
Created int64 `json:"created"`
152+
Model string `json:"model"`
153+
Choices []MessageStreamChoice `json:"choices"`
154+
Usage *Usage `json:"usage,omitempty"`
156155
}
157156

158157
type Usage struct {
@@ -163,13 +162,6 @@ type Usage struct {
163162
ReasoningTokens int64 `json:"reasoning_tokens,omitempty"`
164163
}
165164

166-
type RateLimit struct {
167-
Limit int64 `json:"limit,omitempty"`
168-
Remaining int64 `json:"remaining,omitempty"`
169-
Reset int64 `json:"reset,omitempty"`
170-
RetryAfter int64 `json:"retry_after,omitempty"`
171-
}
172-
173165
// MessageStream interface represents a stream of chat completions
174166
type MessageStream interface {
175167
// Recv gets the next completion chunk

pkg/model/provider/anthropic/adapter.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"net/http"
8-
"strconv"
98
"strings"
109

1110
"github.com/anthropics/anthropic-sdk-go"
@@ -20,17 +19,15 @@ import (
2019
type streamAdapter struct {
2120
retryableStream[anthropic.MessageStreamEventUnion]
2221

23-
trackUsage bool
24-
toolCall bool
25-
toolID string
26-
getResponseTrailer func() http.Header
22+
trackUsage bool
23+
toolCall bool
24+
toolID string
2725
}
2826

2927
func (c *Client) newStreamAdapter(stream *ssestream.Stream[anthropic.MessageStreamEventUnion], trackUsage bool) *streamAdapter {
3028
return &streamAdapter{
31-
retryableStream: retryableStream[anthropic.MessageStreamEventUnion]{stream: stream},
32-
trackUsage: trackUsage,
33-
getResponseTrailer: c.getResponseTrailer,
29+
retryableStream: retryableStream[anthropic.MessageStreamEventUnion]{stream: stream},
30+
trackUsage: trackUsage,
3431
}
3532
}
3633

@@ -151,30 +148,11 @@ func (a *streamAdapter) Recv() (chat.MessageStreamResponse, error) {
151148
} else {
152149
response.Choices[0].FinishReason = chat.FinishReasonStop
153150
}
154-
155-
// MessageStopEvent is the last event. Let's drain the response to get the trailing headers.
156-
trailers := a.getResponseTrailer()
157-
if trailers.Get("X-RateLimit-Limit") != "" {
158-
response.RateLimit = &chat.RateLimit{
159-
Limit: parseHeaderInt64(trailers.Get("X-RateLimit-Limit")),
160-
Remaining: parseHeaderInt64(trailers.Get("X-RateLimit-Remaining")),
161-
Reset: parseHeaderInt64(trailers.Get("X-RateLimit-Reset")),
162-
RetryAfter: parseHeaderInt64(trailers.Get("Retry-After")),
163-
}
164-
}
165151
}
166152

167153
return response, nil
168154
}
169155

170-
func parseHeaderInt64(headerValue string) int64 {
171-
value, err := strconv.ParseInt(headerValue, 10, 64)
172-
if err != nil {
173-
return 0
174-
}
175-
return value
176-
}
177-
178156
// Close closes the stream
179157
func (a *streamAdapter) Close() {
180158
a.stream.Close()

pkg/model/provider/anthropic/beta_adapter.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package anthropic
33
import (
44
"fmt"
55
"log/slog"
6-
"net/http"
76

87
"github.com/anthropics/anthropic-sdk-go"
98
"github.com/anthropics/anthropic-sdk-go/packages/ssestream"
@@ -16,18 +15,16 @@ import (
1615
type betaStreamAdapter struct {
1716
retryableStream[anthropic.BetaRawMessageStreamEventUnion]
1817

19-
trackUsage bool
20-
toolCall bool
21-
toolID string
22-
getResponseTrailer func() http.Header
18+
trackUsage bool
19+
toolCall bool
20+
toolID string
2321
}
2422

2523
// newBetaStreamAdapter creates a new Beta stream adapter
2624
func (c *Client) newBetaStreamAdapter(stream *ssestream.Stream[anthropic.BetaRawMessageStreamEventUnion], trackUsage bool) *betaStreamAdapter {
2725
return &betaStreamAdapter{
28-
retryableStream: retryableStream[anthropic.BetaRawMessageStreamEventUnion]{stream: stream},
29-
trackUsage: trackUsage,
30-
getResponseTrailer: c.getResponseTrailer,
26+
retryableStream: retryableStream[anthropic.BetaRawMessageStreamEventUnion]{stream: stream},
27+
trackUsage: trackUsage,
3128
}
3229
}
3330

pkg/model/provider/anthropic/client.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9-
"io"
109
"log/slog"
11-
"net/http"
1210
"net/url"
1311
"strings"
1412

@@ -33,21 +31,8 @@ import (
3331
type Client struct {
3432
base.Config
3533

36-
clientFn func(context.Context) (anthropic.Client, error)
37-
lastHTTPResponse *http.Response
38-
fileManager *FileManager
39-
}
40-
41-
func (c *Client) getResponseTrailer() http.Header {
42-
if c.lastHTTPResponse == nil {
43-
return nil
44-
}
45-
46-
if c.lastHTTPResponse.Body != nil {
47-
_, _ = io.Copy(io.Discard, c.lastHTTPResponse.Body)
48-
}
49-
50-
return c.lastHTTPResponse.Trailer
34+
clientFn func(context.Context) (anthropic.Client, error)
35+
fileManager *FileManager
5136
}
5237

5338
// adjustMaxTokensForThinking checks if max_tokens needs adjustment for thinking_budget.
@@ -207,7 +192,6 @@ func NewClient(ctx context.Context, cfg *latest.ModelConfig, env environment.Pro
207192
}
208193

209194
client := anthropic.NewClient(
210-
option.WithResponseInto(&anthropicClient.lastHTTPResponse),
211195
option.WithAuthToken(authToken),
212196
option.WithAPIKey(authToken),
213197
option.WithBaseURL(baseURL),

pkg/runtime/event.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ type Usage struct {
286286
// It embeds chat.Usage and adds Cost, Model, and FinishReason fields.
287287
type MessageUsage struct {
288288
chat.Usage
289-
chat.RateLimit
290289

291290
Cost float64
292291
Model string

pkg/runtime/loop.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,6 @@ func (r *LocalRuntime) recordAssistantMessage(
471471
Model: messageModel,
472472
FinishReason: res.FinishReason,
473473
}
474-
if res.RateLimit != nil {
475-
msgUsage.RateLimit = *res.RateLimit
476-
}
477474
return msgUsage
478475
}
479476

pkg/runtime/streaming.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ type streamResult struct {
2828
Stopped bool
2929
FinishReason chat.FinishReason
3030
Usage *chat.Usage
31-
RateLimit *chat.RateLimit
3231
}
3332

3433
// handleStream reads a chat.MessageStream to completion, emitting streaming
@@ -44,7 +43,6 @@ func (r *LocalRuntime) handleStream(ctx context.Context, stream chat.MessageStre
4443
var thoughtSignature []byte
4544
var toolCalls []tools.ToolCall
4645
var messageUsage *chat.Usage
47-
var messageRateLimit *chat.RateLimit
4846
var providerFinishReason chat.FinishReason
4947

5048
toolCallIndex := make(map[string]int) // toolCallID -> index in toolCalls slice
@@ -89,10 +87,6 @@ func (r *LocalRuntime) handleStream(ctx context.Context, stream chat.MessageStre
8987
messageUsage = response.Usage
9088
}
9189

92-
if response.RateLimit != nil {
93-
messageRateLimit = response.RateLimit
94-
}
95-
9690
if len(response.Choices) == 0 {
9791
continue
9892
}
@@ -113,7 +107,6 @@ func (r *LocalRuntime) handleStream(ctx context.Context, stream chat.MessageStre
113107
Stopped: true,
114108
FinishReason: choice.FinishReason,
115109
Usage: messageUsage,
116-
RateLimit: messageRateLimit,
117110
}, nil
118111
}
119112

@@ -236,7 +229,6 @@ func (r *LocalRuntime) handleStream(ctx context.Context, stream chat.MessageStre
236229
Stopped: stoppedDueToNoOutput,
237230
FinishReason: finishReason,
238231
Usage: messageUsage,
239-
RateLimit: messageRateLimit,
240232
}, nil
241233
}
242234

0 commit comments

Comments
 (0)