From 77b794731a4eac6b1416e515c6b307e8e6eb3535 Mon Sep 17 00:00:00 2001 From: Jason Xu Date: Thu, 20 Aug 2026 00:17:39 +0800 Subject: [PATCH] feat: collect token and upstream attempt metrics --- admin.go | 5 +- gateway.go | 60 ++++++++- observability.go | 335 ++++++++++++++++++++++++++++++++++++++++++----- stream.go | 89 ++++++++++++- 4 files changed, 449 insertions(+), 40 deletions(-) diff --git a/admin.go b/admin.go index 4ec6e3f..cffae95 100644 --- a/admin.go +++ b/admin.go @@ -386,7 +386,10 @@ func (a *AdminServer) handleAccount(w http.ResponseWriter, r *http.Request) { } func (a *AdminServer) handleMonitor(w http.ResponseWriter, _ *http.Request) { - writeJSON(w, http.StatusOK, map[string]any{"version": version, "metrics": a.monitor.Snapshot(), "resources": a.manager.Resources()}) + metrics := a.monitor.Snapshot() + writeJSON(w, http.StatusOK, map[string]any{ + "version": version, "metrics": metrics, "usage": metrics.Usage, "upstream": metrics.Upstream, "resources": a.manager.Resources(), + }) } func (a *AdminServer) handleLogs(w http.ResponseWriter, r *http.Request) { diff --git a/gateway.go b/gateway.go index 7aaa901..87549e3 100644 --- a/gateway.go +++ b/gateway.go @@ -284,10 +284,18 @@ func (g *Gateway) handleInference(external Protocol) http.HandlerFunc { w.Header().Set("Cache-Control", "no-cache") w.Header().Set("X-Accel-Buffering", "no") w.WriteHeader(resp.StatusCode) + var usage bridgeUsage + var usageReported bool if external == route.Protocol { - _, err = io.Copy(w, resp.Body) + observer := newStreamUsageObserver(route.Protocol) + _, err = io.Copy(w, io.TeeReader(resp.Body, observer)) + usage = observer.Finish() + usageReported = observer.Reported() } else { - err = transcodeStream(w, resp.Body, route.Protocol, external, model) + usage, usageReported, err = transcodeStreamWithUsage(w, resp.Body, route.Protocol, external, model) + } + if meta != nil { + meta.Usage, meta.UsageReported = usage, usageReported } if err != nil && !errors.Is(err, context.Canceled) { g.logger.Debug("downstream stream ended with an error", "component", "stream", "event", "stream_failed", "request_id", ids.Request, "model", model, "tier", route.Tier, "error", err) @@ -299,6 +307,9 @@ func (g *Gateway) handleInference(external Protocol) http.HandlerFunc { writeAPIError(w, external, http.StatusBadGateway, "failed to read upstream response", "upstream_error", ids.Request) return } + if usage, reported := extractResponseUsage(route.Protocol, responseBody); meta != nil { + meta.Usage, meta.UsageReported = usage, reported + } if external != route.Protocol { responseBody, err = convertResponse(route.Protocol, external, responseBody) if err != nil { @@ -362,6 +373,7 @@ func (g *Gateway) doAnonymousUpstream(ctx context.Context, route modelRoute, bod status = resp.StatusCode } g.syncProxyResult(ctx, node.proxy, status, err) + g.recordUpstreamAttempt(route, ids, attempts, "anonymous", "anonymous", true, node.proxy, resp, err, duration) if err == nil && resp.StatusCode/100 == 2 { g.anonymous.MarkSuccess(node) g.logger.Debug("anonymous upstream accepted request", "component", "upstream", "event", "anonymous_attempt_succeeded", "request_id", ids.Request, "attempt", attempts, "tier", TierZen, "proxy", redactURL(node.proxy.name), "status", resp.StatusCode, "duration_ms", duration.Milliseconds()) @@ -434,6 +446,7 @@ func (g *Gateway) doKeyUpstream(ctx context.Context, route modelRoute, body []by status = resp.StatusCode } proxyFailed := g.syncProxyResult(ctx, proxy, status, err) + g.recordUpstreamAttempt(route, ids, attemptOffset+attempt, secretFingerprint(node.key), "key", false, proxy, resp, err, attemptDuration) if err == nil && resp.StatusCode/100 == 2 { nodes.MarkSuccess(node) g.logger.Debug("upstream accepted request", "component", "upstream", "event", "attempt_succeeded", "request_id", ids.Request, "attempt", attempt, "tier", route.Tier, "key_id", secretFingerprint(node.key), "proxy", redactURL(proxy.name), "status", resp.StatusCode, "duration_ms", attemptDuration.Milliseconds()) @@ -468,6 +481,49 @@ func (g *Gateway) doKeyUpstream(ctx context.Context, route modelRoute, body []by return nil, lastErr } +func extractResponseUsage(protocol Protocol, body []byte) (bridgeUsage, bool) { + var payload map[string]any + if json.Unmarshal(body, &payload) != nil { + return bridgeUsage{}, false + } + usage := mapAt(payload, "usage") + if len(usage) == 0 { + return bridgeUsage{}, false + } + if protocol == ProtocolAnthropic { + return decodeAnthropicUsage(usage), true + } + return decodeOpenAIUsage(usage), true +} + +func (g *Gateway) recordUpstreamAttempt(route modelRoute, ids requestIDs, attempt int, keyID, channel string, anonymous bool, proxy *proxyTransport, resp *http.Response, err error, duration time.Duration) { + if g.monitor == nil { + return + } + status := 0 + if resp != nil { + status = resp.StatusCode + } + success := err == nil && status >= 200 && status < 300 + outcome := "retryable_failure" + if success { + outcome = "success" + } else if err != nil { + outcome = "transport_error" + } else if isNonRetryableClientResponse(resp, nil) { + outcome = "rejected" + } + proxyName := "unavailable" + if proxy != nil { + proxyName = redactURL(proxy.name) + } + g.monitor.RecordAttempt(UpstreamAttempt{ + Time: time.Now().UTC(), RequestID: ids.Request, Model: route.ID, Tier: string(route.Tier), Attempt: attempt, + KeyID: keyID, Channel: channel, Anonymous: anonymous, Proxy: proxyName, Status: status, + DurationMS: max(duration.Milliseconds(), 0), Success: success, Outcome: outcome, + }) +} + func newUpstreamRequest(ctx context.Context, baseURL string, protocol Protocol, body []byte, ids requestIDs, key string) (*http.Request, error) { endpoint := strings.TrimRight(baseURL, "/") + protocolPath(protocol) req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body)) diff --git a/observability.go b/observability.go index f66fa68..dab5dc1 100644 --- a/observability.go +++ b/observability.go @@ -288,11 +288,13 @@ func (h *hubHandler) addAttr(fields map[string]any, attr slog.Attr) { } type requestMeta struct { - Model string - Tier string - Request string - Attempts int - Stream bool + Model string + Tier string + Request string + Attempts int + Stream bool + Usage bridgeUsage + UsageReported bool } type requestMetaKey struct{} @@ -303,37 +305,116 @@ func metaFromRequest(r *http.Request) *requestMeta { } type metricBucket struct { - minute int64 - total uint64 - success uint64 - errors uint64 - duration uint64 - histogram [11]uint64 - endpoints map[string]uint64 - models map[string]uint64 - tiers map[string]uint64 - statuses map[string]uint64 + minute int64 + total uint64 + success uint64 + errors uint64 + duration uint64 + histogram [11]uint64 + endpoints map[string]uint64 + models map[string]uint64 + tiers map[string]uint64 + statuses map[string]uint64 + usageRequests uint64 + usageReported uint64 + tokens TokenCounts + usageModels map[string]TokenCounts + usageTiers map[string]TokenCounts } func (b *metricBucket) reset(minute int64) { *b = metricBucket{ minute: minute, endpoints: make(map[string]uint64), models: make(map[string]uint64), tiers: make(map[string]uint64), statuses: make(map[string]uint64), + usageModels: make(map[string]TokenCounts), usageTiers: make(map[string]TokenCounts), } } -type Monitor struct { - started time.Time - active atomic.Int64 - activeStreams atomic.Int64 - total atomic.Uint64 - success atomic.Uint64 - errors atomic.Uint64 - mu sync.Mutex - buckets [60]metricBucket +type TokenCounts struct { + Input uint64 `json:"input_tokens"` + Output uint64 `json:"output_tokens"` + Cached uint64 `json:"cached_tokens"` + Reasoning uint64 `json:"reasoning_tokens"` + Total uint64 `json:"total_tokens"` } -func NewMonitor() *Monitor { return &Monitor{started: time.Now().UTC()} } +type UsagePeriod struct { + Requests uint64 `json:"requests"` + Reported uint64 `json:"reported"` + Coverage float64 `json:"coverage"` + Tokens TokenCounts `json:"tokens"` + Models map[string]TokenCounts `json:"models"` + Tiers map[string]TokenCounts `json:"tiers"` +} + +type UsageSnapshot struct { + Lifetime UsagePeriod `json:"lifetime"` + Window UsagePeriod `json:"last_hour"` +} + +type AttemptCounts struct { + Total uint64 `json:"total"` + Success uint64 `json:"success"` + Failed uint64 `json:"failed"` +} + +type AttemptAggregate struct { + AttemptCounts + SuccessRate float64 `json:"success_rate"` + Tiers map[string]AttemptCounts `json:"tiers"` + Channels map[string]AttemptCounts `json:"channels"` + Keys map[string]AttemptCounts `json:"keys"` +} + +type UpstreamAttempt struct { + Time time.Time `json:"time"` + RequestID string `json:"request_id"` + Model string `json:"model"` + Tier string `json:"tier"` + Attempt int `json:"attempt"` + KeyID string `json:"key_id"` + Channel string `json:"channel"` + Anonymous bool `json:"anonymous"` + Proxy string `json:"proxy_node"` + Status int `json:"status,omitempty"` + DurationMS int64 `json:"duration_ms"` + Success bool `json:"success"` + Outcome string `json:"outcome"` +} + +type attemptBucket struct { + minute int64 + aggregate AttemptAggregate +} + +type UpstreamSnapshot struct { + Lifetime AttemptAggregate `json:"lifetime"` + Window AttemptAggregate `json:"last_hour"` + Recent []UpstreamAttempt `json:"recent"` +} + +type Monitor struct { + started time.Time + active atomic.Int64 + activeStreams atomic.Int64 + total atomic.Uint64 + success atomic.Uint64 + errors atomic.Uint64 + mu sync.Mutex + buckets [60]metricBucket + lifetimeUsage UsagePeriod + attemptLifetime AttemptAggregate + attemptBuckets [60]attemptBucket + recentAttempts []UpstreamAttempt +} + +func NewMonitor() *Monitor { + return &Monitor{ + started: time.Now().UTC(), + lifetimeUsage: newUsagePeriod(), + attemptLifetime: newAttemptAggregate(), + } +} var latencyBounds = [...]uint64{50, 100, 250, 500, 1000, 2500, 5000, 10000, 30000, 60000} @@ -374,11 +455,51 @@ func (m *Monitor) Record(endpoint string, status int, duration time.Duration, me } if meta.Tier != "" { bucket.tiers[meta.Tier]++ + bucket.usageRequests++ + m.lifetimeUsage.Requests++ + if meta.UsageReported { + bucket.usageReported++ + m.lifetimeUsage.Reported++ + } + tokens := tokenCounts(meta.Usage) + addTokenCounts(&bucket.tokens, tokens) + addTokenCounts(&m.lifetimeUsage.Tokens, tokens) + addTokenMap(bucket.usageModels, meta.Model, tokens) + addTokenMap(bucket.usageTiers, meta.Tier, tokens) + addTokenMap(m.lifetimeUsage.Models, meta.Model, tokens) + addTokenMap(m.lifetimeUsage.Tiers, meta.Tier, tokens) } } m.mu.Unlock() } +func (m *Monitor) RecordAttempt(attempt UpstreamAttempt) { + if attempt.Time.IsZero() { + attempt.Time = time.Now().UTC() + } else { + attempt.Time = attempt.Time.UTC() + } + minute := attempt.Time.Unix() / 60 + m.mu.Lock() + recordAttemptAggregate(&m.attemptLifetime, attempt) + bucket := &m.attemptBuckets[minute%60] + if bucket.minute != minute { + *bucket = attemptBucket{minute: minute, aggregate: newAttemptAggregate()} + } + recordAttemptAggregate(&bucket.aggregate, attempt) + cutoff := attempt.Time.Add(-time.Hour) + first := 0 + for first < len(m.recentAttempts) && m.recentAttempts[first].Time.Before(cutoff) { + first++ + } + if first > 0 { + copy(m.recentAttempts, m.recentAttempts[first:]) + m.recentAttempts = m.recentAttempts[:len(m.recentAttempts)-first] + } + m.recentAttempts = append(m.recentAttempts, attempt) + m.mu.Unlock() +} + type MonitorSnapshot struct { StartedAt time.Time `json:"started_at"` UptimeSeconds int64 `json:"uptime_seconds"` @@ -391,6 +512,8 @@ type MonitorSnapshot struct { Models map[string]uint64 `json:"models"` Tiers map[string]uint64 `json:"tiers"` Statuses map[string]uint64 `json:"statuses"` + Usage UsageSnapshot `json:"usage"` + Upstream UpstreamSnapshot `json:"upstream"` } type MetricSummary struct { @@ -405,10 +528,16 @@ type MetricSummary struct { } type MetricSeries struct { - Minute time.Time `json:"minute"` - Total uint64 `json:"total"` - Success uint64 `json:"success"` - Errors uint64 `json:"errors"` + Minute time.Time `json:"minute"` + Total uint64 `json:"total"` + Success uint64 `json:"success"` + Errors uint64 `json:"errors"` + InputTokens uint64 `json:"input_tokens"` + OutputTokens uint64 `json:"output_tokens"` + CachedTokens uint64 `json:"cached_tokens"` + ReasoningTokens uint64 `json:"reasoning_tokens"` + TotalTokens uint64 `json:"total_tokens"` + UsageReported uint64 `json:"usage_reported"` } func (m *Monitor) Snapshot() MonitorSnapshot { @@ -417,6 +546,9 @@ func (m *Monitor) Snapshot() MonitorSnapshot { var histogram [11]uint64 endpoints, models, tiers, statuses := map[string]uint64{}, map[string]uint64{}, map[string]uint64{}, map[string]uint64{} series := make([]MetricSeries, 0, 60) + usageWindow := newUsagePeriod() + upstreamWindow := newAttemptAggregate() + var recentAttempts []UpstreamAttempt m.mu.Lock() for offset := int64(59); offset >= 0; offset-- { minute := nowMinute - offset @@ -424,6 +556,9 @@ func (m *Monitor) Snapshot() MonitorSnapshot { entry := MetricSeries{Minute: time.Unix(minute*60, 0).UTC()} if bucket.minute == minute { entry.Total, entry.Success, entry.Errors = bucket.total, bucket.success, bucket.errors + entry.InputTokens, entry.OutputTokens = bucket.tokens.Input, bucket.tokens.Output + entry.CachedTokens, entry.ReasoningTokens, entry.TotalTokens = bucket.tokens.Cached, bucket.tokens.Reasoning, bucket.tokens.Total + entry.UsageReported = bucket.usageReported window.Total += bucket.total window.Success += bucket.success window.Errors += bucket.errors @@ -435,10 +570,34 @@ func (m *Monitor) Snapshot() MonitorSnapshot { mergeCounts(models, bucket.models) mergeCounts(tiers, bucket.tiers) mergeCounts(statuses, bucket.statuses) + usageWindow.Requests += bucket.usageRequests + usageWindow.Reported += bucket.usageReported + addTokenCounts(&usageWindow.Tokens, bucket.tokens) + mergeTokenMaps(usageWindow.Models, bucket.usageModels) + mergeTokenMaps(usageWindow.Tiers, bucket.usageTiers) + } + attemptBucket := &m.attemptBuckets[minute%60] + if attemptBucket.minute == minute { + mergeAttemptAggregate(&upstreamWindow, attemptBucket.aggregate) } series = append(series, entry) } + usageLifetime := cloneUsagePeriod(m.lifetimeUsage) + upstreamLifetime := cloneAttemptAggregate(m.attemptLifetime) + cutoff := time.Now().Add(-time.Hour) + for _, attempt := range m.recentAttempts { + if !attempt.Time.Before(cutoff) { + recentAttempts = append(recentAttempts, attempt) + } + } + if len(recentAttempts) > 500 { + recentAttempts = recentAttempts[len(recentAttempts)-500:] + } m.mu.Unlock() + finalizeUsagePeriod(&usageLifetime) + finalizeUsagePeriod(&usageWindow) + finalizeAttemptAggregate(&upstreamLifetime) + finalizeAttemptAggregate(&upstreamWindow) if window.Total > 0 { window.SuccessRate = float64(window.Success) / float64(window.Total) window.AverageMS /= float64(window.Total) @@ -454,6 +613,8 @@ func (m *Monitor) Snapshot() MonitorSnapshot { StartedAt: m.started, UptimeSeconds: int64(time.Since(m.started).Seconds()), Active: m.active.Load(), ActiveStreams: m.activeStreams.Load(), Lifetime: lifetime, Window: window, Series: series, Endpoints: endpoints, Models: models, Tiers: tiers, Statuses: statuses, + Usage: UsageSnapshot{Lifetime: usageLifetime, Window: usageWindow}, + Upstream: UpstreamSnapshot{Lifetime: upstreamLifetime, Window: upstreamWindow, Recent: recentAttempts}, } } @@ -463,6 +624,116 @@ func mergeCounts(target, source map[string]uint64) { } } +func newUsagePeriod() UsagePeriod { + return UsagePeriod{Models: make(map[string]TokenCounts), Tiers: make(map[string]TokenCounts)} +} + +func tokenCounts(usage bridgeUsage) TokenCounts { + return TokenCounts{ + Input: uint64(max(usage.Input, 0)), Output: uint64(max(usage.Output, 0)), + Cached: uint64(max(usage.Cached, 0)), Reasoning: uint64(max(usage.Reasoning, 0)), Total: uint64(max(usage.Total, 0)), + } +} + +func addTokenCounts(target *TokenCounts, source TokenCounts) { + target.Input += source.Input + target.Output += source.Output + target.Cached += source.Cached + target.Reasoning += source.Reasoning + target.Total += source.Total +} + +func addTokenMap(target map[string]TokenCounts, key string, value TokenCounts) { + if key == "" { + return + } + current := target[key] + addTokenCounts(¤t, value) + target[key] = current +} + +func mergeTokenMaps(target, source map[string]TokenCounts) { + for key, value := range source { + addTokenMap(target, key, value) + } +} + +func cloneUsagePeriod(source UsagePeriod) UsagePeriod { + result := newUsagePeriod() + result.Requests, result.Reported, result.Tokens = source.Requests, source.Reported, source.Tokens + mergeTokenMaps(result.Models, source.Models) + mergeTokenMaps(result.Tiers, source.Tiers) + return result +} + +func finalizeUsagePeriod(period *UsagePeriod) { + if period.Requests > 0 { + period.Coverage = float64(period.Reported) / float64(period.Requests) + } +} + +func newAttemptAggregate() AttemptAggregate { + return AttemptAggregate{ + Tiers: make(map[string]AttemptCounts), Channels: make(map[string]AttemptCounts), Keys: make(map[string]AttemptCounts), + } +} + +func recordAttemptAggregate(target *AttemptAggregate, attempt UpstreamAttempt) { + recordAttemptCounts(&target.AttemptCounts, attempt.Success) + addAttemptMap(target.Tiers, attempt.Tier, attempt.Success) + addAttemptMap(target.Channels, attempt.Channel, attempt.Success) + addAttemptMap(target.Keys, attempt.KeyID, attempt.Success) +} + +func recordAttemptCounts(target *AttemptCounts, success bool) { + target.Total++ + if success { + target.Success++ + } else { + target.Failed++ + } +} + +func addAttemptMap(target map[string]AttemptCounts, key string, success bool) { + if key == "" { + return + } + current := target[key] + recordAttemptCounts(¤t, success) + target[key] = current +} + +func mergeAttemptAggregate(target *AttemptAggregate, source AttemptAggregate) { + target.Total += source.Total + target.Success += source.Success + target.Failed += source.Failed + mergeAttemptMaps(target.Tiers, source.Tiers) + mergeAttemptMaps(target.Channels, source.Channels) + mergeAttemptMaps(target.Keys, source.Keys) +} + +func mergeAttemptMaps(target, source map[string]AttemptCounts) { + for key, value := range source { + current := target[key] + current.Total += value.Total + current.Success += value.Success + current.Failed += value.Failed + target[key] = current + } +} + +func cloneAttemptAggregate(source AttemptAggregate) AttemptAggregate { + result := newAttemptAggregate() + mergeAttemptAggregate(&result, source) + return result +} + +func finalizeAttemptAggregate(aggregate *AttemptAggregate) { + if aggregate.Total > 0 { + aggregate.SuccessRate = float64(aggregate.Success) / float64(aggregate.Total) + } +} + func histogramPercentile(histogram [11]uint64, total uint64, percentile float64) uint64 { target := uint64(float64(total)*percentile + 0.999) var count uint64 @@ -509,6 +780,10 @@ func (w *statusWriter) Unwrap() http.ResponseWriter { return w.ResponseWriter } func monitorMiddleware(monitor *Monitor, logger *slog.Logger, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/healthz" { + next.ServeHTTP(w, r) + return + } started := time.Now() meta := &requestMeta{} r = r.WithContext(context.WithValue(r.Context(), requestMetaKey{}, meta)) @@ -522,7 +797,7 @@ func monitorMiddleware(monitor *Monitor, logger *slog.Logger, next http.Handler) } duration := time.Since(started) monitor.Record(r.URL.Path, status, duration, meta) - logger.Info("request completed", "component", "http", "event", "request_complete", "method", r.Method, + logger.Debug("request completed", "component", "http", "event", "request_complete", "method", r.Method, "path", r.URL.Path, "status", status, "duration_ms", duration.Milliseconds(), "bytes", writer.bytes, "request_id", meta.Request, "model", meta.Model, "tier", meta.Tier, "attempts", meta.Attempts, "stream", meta.Stream) }() diff --git a/stream.go b/stream.go index 423ae45..7e4a4b4 100644 --- a/stream.go +++ b/stream.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "bytes" "encoding/json" "fmt" "io" @@ -35,9 +36,14 @@ type bridgeStreamParser struct { } func transcodeStream(w http.ResponseWriter, reader io.Reader, from, to Protocol, model string) error { + _, _, err := transcodeStreamWithUsage(w, reader, from, to, model) + return err +} + +func transcodeStreamWithUsage(w http.ResponseWriter, reader io.Reader, from, to Protocol, model string) (bridgeUsage, bool, error) { flusher, ok := w.(http.Flusher) if !ok { - return fmt.Errorf("response writer does not support streaming") + return bridgeUsage{}, false, fmt.Errorf("response writer does not support streaming") } parser := &bridgeStreamParser{ protocol: from, @@ -60,9 +66,78 @@ func transcodeStream(w http.ResponseWriter, reader io.Reader, from, to Protocol, } return nil }); err != nil { - return err + return emitter.usage, emitter.usageReported, err } - return emitter.Finish() + return emitter.usage, emitter.usageReported, emitter.Finish() +} + +type streamUsageObserver struct { + parser *bridgeStreamParser + buffer []byte + usage bridgeUsage + reported bool +} + +func newStreamUsageObserver(protocol Protocol) *streamUsageObserver { + return &streamUsageObserver{parser: &bridgeStreamParser{ + protocol: protocol, tools: map[string]bool{}, toolIDs: map[string]string{}, toolNames: map[string]string{}, + responseArgs: map[string]bool{}, responseReasoning: map[string]bool{}, + }} +} + +func (observer *streamUsageObserver) Write(data []byte) (int, error) { + observer.buffer = append(observer.buffer, data...) + for { + index, width := nextSSEBoundary(observer.buffer) + if index < 0 { + break + } + frame := append([]byte(nil), observer.buffer[:index+width]...) + observer.buffer = observer.buffer[index+width:] + observer.consume(frame) + } + return len(data), nil +} + +func (observer *streamUsageObserver) Finish() bridgeUsage { + if len(observer.buffer) > 0 { + observer.consume(observer.buffer) + observer.buffer = nil + } + return observer.usage +} + +func (observer *streamUsageObserver) Reported() bool { return observer.reported } + +func (observer *streamUsageObserver) consume(frame []byte) { + _ = readSSE(strings.NewReader(string(frame)), func(eventName, data string) error { + events, err := observer.parser.Parse(eventName, data) + if err != nil { + return nil + } + for _, event := range events { + if event.Usage != nil { + observer.reported = true + mergeBridgeUsage(&observer.usage, *event.Usage) + } + } + return nil + }) +} + +func nextSSEBoundary(data []byte) (int, int) { + lf := bytes.Index(data, []byte("\n\n")) + crlf := bytes.Index(data, []byte("\r\n\r\n")) + if lf < 0 { + if crlf < 0 { + return -1, 0 + } + return crlf, 4 + } + if crlf >= 0 && crlf < lf { + return crlf, 4 + } + return lf, 2 } func readSSE(reader io.Reader, handler func(eventName, data string) error) error { @@ -380,6 +455,7 @@ type bridgeStreamEmitter struct { done bool stop string usage bridgeUsage + usageReported bool text strings.Builder reasoning strings.Builder reasoningSignature strings.Builder @@ -471,6 +547,7 @@ func (emitter *bridgeStreamEmitter) Emit(event bridgeStreamEvent) error { return emitter.emitPendingToolArguments(tool) case "usage": if event.Usage != nil { + emitter.usageReported = true mergeBridgeUsage(&emitter.usage, *event.Usage) } case "finish": @@ -944,7 +1021,7 @@ func mergeBridgeUsage(destination *bridgeUsage, source bridgeUsage) { destination.Output = source.Output } if source.Total != 0 { - destination.Total = source.Total + destination.Total = max(destination.Total, source.Total) } if source.Cached != 0 { destination.Cached = source.Cached @@ -952,7 +1029,5 @@ func mergeBridgeUsage(destination *bridgeUsage, source bridgeUsage) { if source.Reasoning != 0 { destination.Reasoning = source.Reasoning } - if destination.Total == 0 { - destination.Total = destination.Input + destination.Output - } + destination.Total = max(destination.Total, destination.Input+destination.Output) }