From aec65e3be33c5b33ac39ea4fba02abd909ed94e4 Mon Sep 17 00:00:00 2001 From: Zhenyu Qi Date: Fri, 13 Mar 2026 00:48:17 -0700 Subject: [PATCH] fix(openai_compat): add stream_options.include_usage for streaming usage tracking --- internal/runtime/executor/openai_compat_executor.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/runtime/executor/openai_compat_executor.go b/internal/runtime/executor/openai_compat_executor.go index d28b3625..623c6620 100644 --- a/internal/runtime/executor/openai_compat_executor.go +++ b/internal/runtime/executor/openai_compat_executor.go @@ -205,6 +205,10 @@ func (e *OpenAICompatExecutor) ExecuteStream(ctx context.Context, auth *cliproxy return nil, err } + // Request usage data in the final streaming chunk so that token statistics + // are captured even when the upstream is an OpenAI-compatible provider. + translated, _ = sjson.SetBytes(translated, "stream_options.include_usage", true) + url := strings.TrimSuffix(baseURL, "/") + "/chat/completions" httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(translated)) if err != nil {