@@ -5,6 +5,7 @@ import type {
55 DeepSeekRequest ,
66 DeepSeekStreamChunk ,
77 DeepSeekToolCall ,
8+ DeepSeekUsage ,
89 StreamCallbacks ,
910} from '../types' ;
1011import { createHttpError , formatRequestError , normalizeRequestError } from './error' ;
@@ -64,6 +65,7 @@ export class DeepSeekClient {
6465 const reader = response . body . getReader ( ) ;
6566 const decoder = new TextDecoder ( ) ;
6667 let buffer = '' ;
68+ let latestUsage : DeepSeekUsage | undefined ;
6769
6870 // Accumulate tool call deltas by index, then emit on finish_reason=stop/tool_calls
6971 const pendingToolCalls = new Map < number , DeepSeekToolCall > ( ) ;
@@ -97,6 +99,7 @@ export class DeepSeekClient {
9799 callbacks . onToolCall ( tc ) ;
98100 }
99101 pendingToolCalls . clear ( ) ;
102+ reportFinalUsage ( callbacks , latestUsage ) ;
100103 callbacks . onDone ( ) ;
101104 return ;
102105 }
@@ -110,9 +113,10 @@ export class DeepSeekClient {
110113 const chunk : DeepSeekStreamChunk = JSON . parse ( jsonStr ) ;
111114 const choice = chunk . choices ?. [ 0 ] ;
112115
113- // Capture usage stats from the API for token-count calibration.
114- if ( chunk . usage && callbacks . onUsage ) {
115- callbacks . onUsage ( chunk . usage ) ;
116+ // Some OpenAI-compatible providers emit usage on every streaming chunk.
117+ // Keep only the latest value and report it once when the stream completes.
118+ if ( chunk . usage ) {
119+ latestUsage = chunk . usage ;
116120 }
117121
118122 if ( ! choice ) {
@@ -166,6 +170,7 @@ export class DeepSeekClient {
166170 }
167171 }
168172
173+ reportFinalUsage ( callbacks , latestUsage ) ;
169174 callbacks . onDone ( ) ;
170175 } catch ( error ) {
171176 if ( isAbortError ( error ) && cancellationToken ?. isCancellationRequested ) {
@@ -180,6 +185,13 @@ export class DeepSeekClient {
180185 }
181186}
182187
188+ function reportFinalUsage ( callbacks : StreamCallbacks , usage : DeepSeekUsage | undefined ) : void {
189+ if ( ! usage || ! callbacks . onUsage ) {
190+ return ;
191+ }
192+ callbacks . onUsage ( usage ) ;
193+ }
194+
183195function isAbortError ( error : unknown ) : boolean {
184196 return error instanceof Error && error . name === 'AbortError' ;
185197}
0 commit comments