xonecas.io
6312
Ga

go-opencode-ai-zen-sdk / commit a03b10e

feat: add DeltaUsage token counts for all streaming endpoints

xonecas2026-02-19 a03b10e6803902cfe9e3a5e1e0112e2d2c1b519c parent bb394f9

4 files changed

cmd/test_models/main.go+59−33
@@ -15,14 +15,16 @@ import (
1515 var probeModels = []string{"gpt-5.1", "claude-sonnet-4-6", "gemini-3-flash", "kimi-k2-thinking"}
1616
1717 type testResult struct {
18- Model string
19- Endpoint string
20- Success bool
21- Error string
22- Latency time.Duration
23- Request string
24- Response string
25- Stream bool
18+ Model string
19+ Endpoint string
20+ Success bool
21+ Error string
22+ Latency time.Duration
23+ Request string
24+ Response string
25+ Stream bool
26+ InputTokens int
27+ OutputTokens int
2628 }
2729
2830 func main() {
@@ -101,11 +103,11 @@ func testStreamParsed(ctx context.Context, client *zen.Client, modelID string) t
101103
102104 deltaCh, errCh, err := client.Stream(ctx, req)
103105 if err != nil {
104- return makeResult(modelID, endpoint, true, string(reqBody), "", err, time.Since(start))
106+ return makeResult(modelID, endpoint, true, string(reqBody), "", err, time.Since(start), 0, 0)
105107 }
106108
107109 var textBuf, reasoningBuf strings.Builder
108- var count int
110+ var count, inTok, outTok int
109111 for d := range deltaCh {
110112 count++
111113 switch d.Type {
@@ -113,14 +115,19 @@ func testStreamParsed(ctx context.Context, client *zen.Client, modelID string) t
113115 textBuf.WriteString(d.Content)
114116 case zen.DeltaReasoning:
115117 reasoningBuf.WriteString(d.Content)
118+ case zen.DeltaUsage:
119+ if d.InputTokens > inTok {
120+ inTok = d.InputTokens
121+ }
122+ outTok += d.OutputTokens
116123 }
117124 }
118125 if streamErr := <-errCh; streamErr != nil {
119- return makeResult(modelID, endpoint, true, string(reqBody), "", streamErr, time.Since(start))
126+ return makeResult(modelID, endpoint, true, string(reqBody), "", streamErr, time.Since(start), 0, 0)
120127 }
121128
122129 resp := fmt.Sprintf("deltas=%d text=%s reasoning=%s", count, truncate(textBuf.String(), 40), truncate(reasoningBuf.String(), 40))
123- return makeResult(modelID, endpoint, true, string(reqBody), resp, nil, time.Since(start))
130+ return makeResult(modelID, endpoint, true, string(reqBody), resp, nil, time.Since(start), inTok, outTok)
124131 }
125132
126133 // toolHistory returns a pre-built two-turn tool-use conversation:
@@ -159,23 +166,29 @@ func testToolHistoryStream(ctx context.Context, client *zen.Client, modelID stri
159166
160167 deltaCh, errCh, err := client.Stream(ctx, req)
161168 if err != nil {
162- return makeResult(modelID, endpoint, true, string(reqBody), "", err, time.Since(start))
169+ return makeResult(modelID, endpoint, true, string(reqBody), "", err, time.Since(start), 0, 0)
163170 }
164171
165172 var textBuf strings.Builder
166- var count int
173+ var count, inTok, outTok int
167174 for d := range deltaCh {
168175 count++
169- if d.Type == zen.DeltaText {
176+ switch d.Type {
177+ case zen.DeltaText:
170178 textBuf.WriteString(d.Content)
179+ case zen.DeltaUsage:
180+ if d.InputTokens > inTok {
181+ inTok = d.InputTokens
182+ }
183+ outTok += d.OutputTokens
171184 }
172185 }
173186 if streamErr := <-errCh; streamErr != nil {
174- return makeResult(modelID, endpoint, true, string(reqBody), "", streamErr, time.Since(start))
187+ return makeResult(modelID, endpoint, true, string(reqBody), "", streamErr, time.Since(start), 0, 0)
175188 }
176189
177190 resp := fmt.Sprintf("deltas=%d text=%s", count, truncate(textBuf.String(), 60))
178- return makeResult(modelID, endpoint, true, string(reqBody), resp, nil, time.Since(start))
191+ return makeResult(modelID, endpoint, true, string(reqBody), resp, nil, time.Since(start), inTok, outTok)
179192 }
180193
181194 func testReasoningStream(ctx context.Context, client *zen.Client, modelID string) testResult {
@@ -190,11 +203,11 @@ func testReasoningStream(ctx context.Context, client *zen.Client, modelID string
190203
191204 deltaCh, errCh, err := client.Stream(ctx, req)
192205 if err != nil {
193- return makeResult(modelID, endpoint, true, string(reqBody), "", err, time.Since(start))
206+ return makeResult(modelID, endpoint, true, string(reqBody), "", err, time.Since(start), 0, 0)
194207 }
195208
196209 var textBuf, reasoningBuf strings.Builder
197- var deltaCount int
210+ var deltaCount, inTok, outTok int
198211 for d := range deltaCh {
199212 deltaCount++
200213 switch d.Type {
@@ -202,30 +215,37 @@ func testReasoningStream(ctx context.Context, client *zen.Client, modelID string
202215 textBuf.WriteString(d.Content)
203216 case zen.DeltaReasoning:
204217 reasoningBuf.WriteString(d.Content)
218+ case zen.DeltaUsage:
219+ if d.InputTokens > inTok {
220+ inTok = d.InputTokens
221+ }
222+ outTok += d.OutputTokens
205223 }
206224 }
207225 if streamErr := <-errCh; streamErr != nil {
208- return makeResult(modelID, endpoint, true, string(reqBody), "", streamErr, time.Since(start))
226+ return makeResult(modelID, endpoint, true, string(reqBody), "", streamErr, time.Since(start), 0, 0)
209227 }
210228
211229 if reasoningBuf.Len() == 0 {
212230 return makeResult(modelID, endpoint, true, string(reqBody),
213231 fmt.Sprintf("deltas=%d text=%s", deltaCount, truncate(textBuf.String(), 60)),
214232 fmt.Errorf("model %s: stream contained no reasoning/thinking deltas", modelID),
215- time.Since(start))
233+ time.Since(start), inTok, outTok)
216234 }
217235 return makeResult(modelID, endpoint, true, string(reqBody),
218236 fmt.Sprintf("deltas=%d reasoning=%s text=%s",
219237 deltaCount,
220238 truncate(reasoningBuf.String(), 40),
221239 truncate(textBuf.String(), 40)),
222- nil, time.Since(start))
240+ nil, time.Since(start), inTok, outTok)
223241 }
224242
225243 // drainUnifiedStream consumes a UnifiedEvent channel and builds a testResult.
244+// StreamEvents operates at the raw event level and does not parse DeltaUsage,
245+// so token counts are not available here.
226246 func drainUnifiedStream(modelID, endpoint, reqBody string, eventCh <-chan zen.UnifiedEvent, errCh <-chan error, initErr error, start time.Time) testResult {
227247 if initErr != nil {
228- return makeResult(modelID, endpoint, true, reqBody, "", initErr, time.Since(start))
248+ return makeResult(modelID, endpoint, true, reqBody, "", initErr, time.Since(start), 0, 0)
229249 }
230250 var count int
231251 var last string
@@ -238,22 +258,24 @@ func drainUnifiedStream(modelID, endpoint, reqBody string, eventCh <-chan zen.Un
238258 }
239259 }
240260 if err := <-errCh; err != nil {
241- return makeResult(modelID, endpoint, true, reqBody, "", err, time.Since(start))
261+ return makeResult(modelID, endpoint, true, reqBody, "", err, time.Since(start), 0, 0)
242262 }
243263 if resolved != "" {
244264 endpoint = resolved
245265 }
246- return makeResult(modelID, endpoint, true, reqBody, fmt.Sprintf("events=%d last=%s", count, truncate(last, 80)), nil, time.Since(start))
266+ return makeResult(modelID, endpoint, true, reqBody, fmt.Sprintf("events=%d last=%s", count, truncate(last, 80)), nil, time.Since(start), 0, 0)
247267 }
248268
249-func makeResult(modelID, endpoint string, stream bool, req, resp string, err error, latency time.Duration) testResult {
269+func makeResult(modelID, endpoint string, stream bool, req, resp string, err error, latency time.Duration, inTok, outTok int) testResult {
250270 r := testResult{
251- Model: modelID,
252- Endpoint: endpoint,
253- Stream: stream,
254- Latency: latency,
255- Request: req,
256- Response: resp,
271+ Model: modelID,
272+ Endpoint: endpoint,
273+ Stream: stream,
274+ Latency: latency,
275+ Request: req,
276+ Response: resp,
277+ InputTokens: inTok,
278+ OutputTokens: outTok,
257279 }
258280 if err != nil {
259281 r.Error = err.Error()
@@ -286,7 +308,11 @@ func printResult(r testResult) {
286308 status = "✗"
287309 }
288310 mode := "stream"
289- fmt.Printf(" %s %-25s [%-15s] [%-10s] %v\n", status, r.Model, r.Endpoint, mode, r.Latency)
311+ usage := "-"
312+ if r.InputTokens > 0 || r.OutputTokens > 0 {
313+ usage = fmt.Sprintf("in=%d out=%d", r.InputTokens, r.OutputTokens)
314+ }
315+ fmt.Printf(" %s %-25s [%-15s] [%-10s] %-20s %v\n", status, r.Model, r.Endpoint, mode, usage, r.Latency)
290316 }
291317
292318 func truncate(s string, max int) string {
examples/agent_loop/main.go+13−0
@@ -57,6 +57,7 @@ func runAgentLoop(client *zen.Client, model string, debugSSE bool, tools map[str
5757 }}
5858
5959 maxSteps := 6
60+ var totalIn, totalOut int
6061 for step := 0; step < maxSteps; step++ {
6162 req := zen.NormalizedRequest{
6263 Model: model,
@@ -82,6 +83,7 @@ func runAgentLoop(client *zen.Client, model string, debugSSE bool, tools map[str
8283
8384 var text strings.Builder
8485 accumulator := zen.NewToolCallAccumulator()
86+ var stepIn, stepOut int
8587
8688 for d := range deltas {
8789 switch d.Type {
@@ -91,6 +93,11 @@ func runAgentLoop(client *zen.Client, model string, debugSSE bool, tools map[str
9193 fmt.Printf("[reasoning] %s", d.Content)
9294 case zen.DeltaToolCallBegin, zen.DeltaToolCallArgumentsDelta, zen.DeltaToolCallDone:
9395 accumulator.Apply(d)
96+ case zen.DeltaUsage:
97+ if d.InputTokens > stepIn {
98+ stepIn = d.InputTokens
99+ }
100+ stepOut += d.OutputTokens
94101 }
95102 }
96103 if err := <-errs; err != nil {
@@ -99,6 +106,10 @@ func runAgentLoop(client *zen.Client, model string, debugSSE bool, tools map[str
99106 }
100107 cancel()
101108
109+ totalIn += stepIn
110+ totalOut += stepOut
111+ fmt.Printf("[usage:step%d] in=%d out=%d\n", step+1, stepIn, stepOut)
112+
102113 calls := accumulator.CompleteCalls()
103114 if len(calls) == 0 {
104115 messages = append(messages, zen.NormalizedMessage{
@@ -106,6 +117,7 @@ func runAgentLoop(client *zen.Client, model string, debugSSE bool, tools map[str
106117 Content: text.String(),
107118 })
108119 fmt.Printf("\n[assistant] %s\n", text.String())
120+ fmt.Printf("[usage:total] in=%d out=%d\n", totalIn, totalOut)
109121 return nil
110122 }
111123
@@ -144,6 +156,7 @@ func runAgentLoop(client *zen.Client, model string, debugSSE bool, tools map[str
144156 })
145157 }
146158 }
159+ fmt.Printf("[usage:total] in=%d out=%d\n", totalIn, totalOut)
147160 return fmt.Errorf("max steps reached without final response")
148161 }
149162
parse.go+94−4
@@ -22,6 +22,8 @@ const (
2222 DeltaToolCallDone NormalizedDeltaType = "tool_call_done"
2323 // DeltaDone signals that the stream has finished (no content fields are set).
2424 DeltaDone NormalizedDeltaType = "done"
25+ // DeltaUsage carries token-count information (InputTokens / OutputTokens).
26+ DeltaUsage NormalizedDeltaType = "usage"
2527 // DeltaUnknown is emitted for events that carry no recognized content.
2628 DeltaUnknown NormalizedDeltaType = "unknown"
2729 )
@@ -40,6 +42,10 @@ type NormalizedDelta struct {
4042 ToolCallSignature string // set on DeltaToolCallBegin / DeltaToolCallDone when provided by provider
4143 ArgumentsDelta string // set on DeltaToolCallArgumentsDelta
4244 ArgumentsFull string // set on DeltaToolCallDone (fully accumulated)
45+
46+ // Usage fields (set for DeltaUsage).
47+ InputTokens int
48+ OutputTokens int
4349 }
4450
4551 // ParseNormalizedEvent parses a single UnifiedEvent into zero or more NormalizedDelta values.
@@ -93,6 +99,10 @@ type chatCompletionChunk struct {
9399 } `json:"delta"`
94100 FinishReason string `json:"finish_reason"`
95101 } `json:"choices"`
102+ Usage *struct {
103+ PromptTokens int `json:"prompt_tokens"`
104+ CompletionTokens int `json:"completion_tokens"`
105+ } `json:"usage"`
96106 }
97107
98108 func parseChatCompletionsDelta(ev UnifiedEvent) []NormalizedDelta {
@@ -100,12 +110,23 @@ func parseChatCompletionsDelta(ev UnifiedEvent) []NormalizedDelta {
100110 if err := json.Unmarshal(ev.Data, &chunk); err != nil {
101111 return nil
102112 }
113+
114+ // Some providers (e.g. those using stream_options: {include_usage: true})
115+ // send a final chunk with an empty choices array that carries only usage.
116+ // Check usage before the choices guard so it is not silently dropped.
117+ var out []NormalizedDelta
103118 if len(chunk.Choices) == 0 {
119+ if chunk.Usage != nil && (chunk.Usage.PromptTokens > 0 || chunk.Usage.CompletionTokens > 0) {
120+ return []NormalizedDelta{{
121+ Type: DeltaUsage,
122+ InputTokens: chunk.Usage.PromptTokens,
123+ OutputTokens: chunk.Usage.CompletionTokens,
124+ }}
125+ }
104126 return nil
105127 }
106128
107129 delta := chunk.Choices[0].Delta
108- var out []NormalizedDelta
109130
110131 if delta.ReasoningContent != "" {
111132 out = append(out, NormalizedDelta{Type: DeltaReasoning, Content: delta.ReasoningContent})
@@ -140,6 +161,13 @@ func parseChatCompletionsDelta(ev UnifiedEvent) []NormalizedDelta {
140161 }
141162
142163 if chunk.Choices[0].FinishReason != "" {
164+ if chunk.Usage != nil && (chunk.Usage.PromptTokens > 0 || chunk.Usage.CompletionTokens > 0) {
165+ out = append(out, NormalizedDelta{
166+ Type: DeltaUsage,
167+ InputTokens: chunk.Usage.PromptTokens,
168+ OutputTokens: chunk.Usage.CompletionTokens,
169+ })
170+ }
143171 out = append(out, NormalizedDelta{Type: DeltaDone})
144172 }
145173
@@ -166,6 +194,13 @@ type responsesEvent struct {
166194 CallID string `json:"call_id"`
167195 ItemID string `json:"item_id"`
168196 Arguments string `json:"arguments"`
197+ // For response.completed / response.done events.
198+ Response *struct {
199+ Usage *struct {
200+ InputTokens int `json:"input_tokens"`
201+ OutputTokens int `json:"output_tokens"`
202+ } `json:"usage"`
203+ } `json:"response"`
169204 }
170205
171206 func parseResponsesDelta(ev UnifiedEvent) []NormalizedDelta {
@@ -230,7 +265,19 @@ func parseResponsesDelta(ev UnifiedEvent) []NormalizedDelta {
230265 }}
231266 }
232267 case "response.completed", "response.done":
233- return []NormalizedDelta{{Type: DeltaDone}}
268+ var out []NormalizedDelta
269+ if e.Response != nil && e.Response.Usage != nil {
270+ u := e.Response.Usage
271+ if u.InputTokens > 0 || u.OutputTokens > 0 {
272+ out = append(out, NormalizedDelta{
273+ Type: DeltaUsage,
274+ InputTokens: u.InputTokens,
275+ OutputTokens: u.OutputTokens,
276+ })
277+ }
278+ }
279+ out = append(out, NormalizedDelta{Type: DeltaDone})
280+ return out
234281 }
235282
236283 return nil
@@ -287,6 +334,16 @@ type anthropicStreamEvent struct {
287334 Name string `json:"name"`
288335 Input string `json:"input"`
289336 } `json:"content_block"`
337+ // For message_start usage.
338+ Message *struct {
339+ Usage *struct {
340+ InputTokens int `json:"input_tokens"`
341+ } `json:"usage"`
342+ } `json:"message"`
343+ // For message_delta usage (top-level usage field).
344+ Usage *struct {
345+ OutputTokens int `json:"output_tokens"`
346+ } `json:"usage"`
290347 }
291348
292349 func parseMessagesDelta(ev UnifiedEvent) []NormalizedDelta {
@@ -304,6 +361,13 @@ func parseMessagesDelta(ev UnifiedEvent) []NormalizedDelta {
304361 }
305362
306363 switch evType {
364+ case "message_start":
365+ if e.Message != nil && e.Message.Usage != nil && e.Message.Usage.InputTokens > 0 {
366+ return []NormalizedDelta{{
367+ Type: DeltaUsage,
368+ InputTokens: e.Message.Usage.InputTokens,
369+ }}
370+ }
307371 case "content_block_start":
308372 if e.ContentBlock.Type == "tool_use" {
309373 return []NormalizedDelta{{
@@ -313,6 +377,13 @@ func parseMessagesDelta(ev UnifiedEvent) []NormalizedDelta {
313377 ToolCallName: e.ContentBlock.Name,
314378 }}
315379 }
380+ case "message_delta":
381+ if e.Usage != nil && e.Usage.OutputTokens > 0 {
382+ return []NormalizedDelta{{
383+ Type: DeltaUsage,
384+ OutputTokens: e.Usage.OutputTokens,
385+ }}
386+ }
316387 case "content_block_delta":
317388 switch e.Delta.Type {
318389 case "text_delta":
@@ -360,6 +431,10 @@ type geminiChunk struct {
360431 } `json:"content"`
361432 FinishReason string `json:"finishReason"`
362433 } `json:"candidates"`
434+ UsageMetadata *struct {
435+ PromptTokenCount int `json:"promptTokenCount"`
436+ CandidatesTokenCount int `json:"candidatesTokenCount"`
437+ } `json:"usageMetadata"`
363438 }
364439
365440 type geminiFC struct {
@@ -372,12 +447,27 @@ func parseGeminiDelta(ev UnifiedEvent) []NormalizedDelta {
372447 if err := json.Unmarshal(ev.Data, &chunk); err != nil {
373448 return nil
374449 }
450+
451+ // Emit usage first; some providers send a final candidate-less chunk that
452+ // only carries usageMetadata.
453+ var out []NormalizedDelta
454+ if chunk.UsageMetadata != nil {
455+ in := chunk.UsageMetadata.PromptTokenCount
456+ outToks := chunk.UsageMetadata.CandidatesTokenCount
457+ if in > 0 || outToks > 0 {
458+ out = append(out, NormalizedDelta{
459+ Type: DeltaUsage,
460+ InputTokens: in,
461+ OutputTokens: outToks,
462+ })
463+ }
464+ }
465+
375466 if len(chunk.Candidates) == 0 {
376- return nil
467+ return out
377468 }
378469
379470 cand := chunk.Candidates[0]
380- var out []NormalizedDelta
381471
382472 for i, part := range cand.Content.Parts {
383473 if part.FunctionCall != nil {
parse_test.go+69−0
@@ -122,6 +122,26 @@ func TestParseChatCompletionsDone(t *testing.T) {
122122 }
123123 }
124124
125+func TestParseChatCompletionsUsageOnFinishChunk(t *testing.T) {
126+ // Usage included on the same chunk as finish_reason (OpenAI default).
127+ ev := makeEvent(EndpointChatCompletions, `{"choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":120,"completion_tokens":45}}`)
128+ deltas := ParseNormalizedEvent(ev)
129+ assertDeltaSequence(t, deltas, DeltaUsage, DeltaDone)
130+ if deltas[0].InputTokens != 120 || deltas[0].OutputTokens != 45 {
131+ t.Fatalf("usage tokens wrong: %+v", deltas[0])
132+ }
133+}
134+
135+func TestParseChatCompletionsUsageOnSplitChunk(t *testing.T) {
136+ // Usage on a separate chunk with empty choices (stream_options providers).
137+ ev := makeEvent(EndpointChatCompletions, `{"choices":[],"usage":{"prompt_tokens":80,"completion_tokens":30}}`)
138+ deltas := ParseNormalizedEvent(ev)
139+ assertDeltaSequence(t, deltas, DeltaUsage)
140+ if deltas[0].InputTokens != 80 || deltas[0].OutputTokens != 30 {
141+ t.Fatalf("usage tokens wrong: %+v", deltas[0])
142+ }
143+}
144+
125145 // ---------------------------------------------------------------------------
126146 // responses (OpenAI Responses API)
127147 // ---------------------------------------------------------------------------
@@ -184,6 +204,15 @@ func TestParseResponsesDone(t *testing.T) {
184204 }
185205 }
186206
207+func TestParseResponsesUsage(t *testing.T) {
208+ ev := makeEvent(EndpointResponses, `{"type":"response.completed","response":{"usage":{"input_tokens":120,"output_tokens":45}}}`)
209+ deltas := ParseNormalizedEvent(ev)
210+ assertDeltaSequence(t, deltas, DeltaUsage, DeltaDone)
211+ if deltas[0].InputTokens != 120 || deltas[0].OutputTokens != 45 {
212+ t.Fatalf("usage tokens wrong: %+v", deltas[0])
213+ }
214+}
215+
187216 // ---------------------------------------------------------------------------
188217 // messages (Anthropic)
189218 // ---------------------------------------------------------------------------
@@ -237,6 +266,26 @@ func TestParseMessagesStop(t *testing.T) {
237266 }
238267 }
239268
269+func TestParseMessagesUsageStart(t *testing.T) {
270+ // message_start carries input tokens.
271+ ev := makeEventNamed(EndpointMessages, "message_start", `{"type":"message_start","message":{"usage":{"input_tokens":120}}}`)
272+ deltas := ParseNormalizedEvent(ev)
273+ assertDeltaSequence(t, deltas, DeltaUsage)
274+ if deltas[0].InputTokens != 120 || deltas[0].OutputTokens != 0 {
275+ t.Fatalf("usage tokens wrong: %+v", deltas[0])
276+ }
277+}
278+
279+func TestParseMessagesUsageDelta(t *testing.T) {
280+ // message_delta carries output tokens at the top-level usage field.
281+ ev := makeEventNamed(EndpointMessages, "message_delta", `{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":45}}`)
282+ deltas := ParseNormalizedEvent(ev)
283+ assertDeltaSequence(t, deltas, DeltaUsage)
284+ if deltas[0].OutputTokens != 45 || deltas[0].InputTokens != 0 {
285+ t.Fatalf("usage tokens wrong: %+v", deltas[0])
286+ }
287+}
288+
240289 // ---------------------------------------------------------------------------
241290 // models (Gemini)
242291 // ---------------------------------------------------------------------------
@@ -301,6 +350,26 @@ func TestParseGeminiDone(t *testing.T) {
301350 }
302351 }
303352
353+func TestParseGeminiUsageOnFinalChunk(t *testing.T) {
354+ // Usage alongside candidates (most common case).
355+ ev := makeEvent(EndpointModels, `{"candidates":[{"content":{"parts":[{"text":"hi"}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":120,"candidatesTokenCount":45}}`)
356+ deltas := ParseNormalizedEvent(ev)
357+ assertDeltaSequence(t, deltas, DeltaUsage, DeltaText, DeltaDone)
358+ if deltas[0].InputTokens != 120 || deltas[0].OutputTokens != 45 {
359+ t.Fatalf("usage tokens wrong: %+v", deltas[0])
360+ }
361+}
362+
363+func TestParseGeminiUsageOnCandidatelessChunk(t *testing.T) {
364+ // Trailing chunk with no candidates, only usageMetadata.
365+ ev := makeEvent(EndpointModels, `{"usageMetadata":{"promptTokenCount":120,"candidatesTokenCount":45}}`)
366+ deltas := ParseNormalizedEvent(ev)
367+ assertDeltaSequence(t, deltas, DeltaUsage)
368+ if deltas[0].InputTokens != 120 || deltas[0].OutputTokens != 45 {
369+ t.Fatalf("usage tokens wrong: %+v", deltas[0])
370+ }
371+}
372+
304373 // ---------------------------------------------------------------------------
305374 // Integration: Stream end-to-end with a mock server
306375 // ---------------------------------------------------------------------------