@@ -20,6 +20,9 @@ type DeltaCallback func(evt provider.StreamEvent)
2020 // ToolCallCallback is called when tool calls are about to be executed.
2121 type ToolCallCallback func()
2222
23+// UsageCallback is called with accumulated token usage after each LLM call.
24+type UsageCallback func(inputTokens, outputTokens int)
25+
2326 // ProcessTurnOptions holds configuration for processing a turn.
2427 type ProcessTurnOptions struct {
2528 Provider provider.Provider
@@ -29,9 +32,44 @@ type ProcessTurnOptions struct {
2932 OnMessage MessageCallback
3033 OnDelta DeltaCallback // Optional: called for each stream event
3134 OnToolCall ToolCallCallback // Optional: called before executing tool calls
35+ OnUsage UsageCallback // Optional: called with token usage after each LLM call
3236 MaxToolRounds int
3337 }
3438
39+// streamAndCollect runs one LLM call: streams events, collects the response,
40+// reports usage, and returns the ChatResponse.
41+func streamAndCollect(ctx context.Context, opts *ProcessTurnOptions, tools []provider.Tool) (*provider.ChatResponse, error) {
42+ stream, err := opts.Provider.ChatStream(ctx, opts.History, tools)
43+ if err != nil {
44+ return nil, err
45+ }
46+ resp, err := collectWithDeltas(stream, opts.OnDelta)
47+ if err != nil {
48+ return nil, err
49+ }
50+ if opts.OnUsage != nil && (resp.InputTokens > 0 || resp.OutputTokens > 0) {
51+ opts.OnUsage(resp.InputTokens, resp.OutputTokens)
52+ }
53+ return resp, nil
54+}
55+
56+// emitAssistant builds an assistant message from a ChatResponse, emits it, and appends to history.
57+func emitAssistant(opts *ProcessTurnOptions, resp *provider.ChatResponse) {
58+ msg := provider.Message{
59+ Role: "assistant",
60+ Content: resp.Content,
61+ Reasoning: resp.Reasoning,
62+ ToolCalls: resp.ToolCalls,
63+ CreatedAt: time.Now(),
64+ InputTokens: resp.InputTokens,
65+ OutputTokens: resp.OutputTokens,
66+ }
67+ if opts.OnMessage != nil {
68+ opts.OnMessage(msg)
69+ }
70+ opts.History = append(opts.History, msg)
71+}
72+
3573 // ProcessTurn handles one conversation turn, which may involve tool calls.
3674 // It streams events via OnDelta and emits complete messages via OnMessage.
3775 func ProcessTurn(ctx context.Context, opts ProcessTurnOptions) error {
@@ -50,30 +88,12 @@ func ProcessTurn(ctx context.Context, opts ProcessTurnOptions) error {
5088 }
5189
5290 for round := 0; round < opts.MaxToolRounds; round++ {
53- // Start streaming from LLM
54- stream, err := opts.Provider.ChatStream(ctx, opts.History, providerTools)
55- if err != nil {
56- return fmt.Errorf("LLM call failed: %w", err)
57- }
58-
59- // Read stream, forwarding deltas and accumulating the full response
60- resp, err := collectWithDeltas(stream, opts.OnDelta)
91+ resp, err := streamAndCollect(ctx, &opts, providerTools)
6192 if err != nil {
6293 return fmt.Errorf("LLM stream failed: %w", err)
6394 }
6495
65- // Build assistant message from accumulated response
66- assistantMsg := provider.Message{
67- Role: "assistant",
68- Content: resp.Content,
69- Reasoning: resp.Reasoning,
70- ToolCalls: resp.ToolCalls,
71- CreatedAt: time.Now(),
72- }
73- if opts.OnMessage != nil {
74- opts.OnMessage(assistantMsg)
75- }
76- opts.History = append(opts.History, assistantMsg)
96+ emitAssistant(&opts, resp)
7797
7898 // If no tool calls, we're done
7999 if len(resp.ToolCalls) == 0 {
@@ -108,40 +128,55 @@ func ProcessTurn(ctx context.Context, opts ProcessTurnOptions) error {
108128 }
109129 opts.History = append(opts.History, limitMsg)
110130
111- stream, err := opts.Provider.ChatStream(ctx, opts.History, nil)
112- if err != nil {
113- return fmt.Errorf("final text-only LLM call failed: %w", err)
114- }
115-
116- resp, err := collectWithDeltas(stream, opts.OnDelta)
131+ resp, err := streamAndCollect(ctx, &opts, nil)
117132 if err != nil {
118133 return fmt.Errorf("final text-only LLM stream failed: %w", err)
119134 }
120135
121- assistantMsg := provider.Message{
122- Role: "assistant",
123- Content: resp.Content,
124- Reasoning: resp.Reasoning,
125- CreatedAt: time.Now(),
126- }
127- if opts.OnMessage != nil {
128- opts.OnMessage(assistantMsg)
136+ emitAssistant(&opts, resp)
137+ return nil
138+}
139+
140+// toolCallAccumulator tracks tool calls as they stream in.
141+type toolCallAccumulator struct {
142+ byIndex map[int]int
143+ calls []provider.ToolCall
144+ argBuilders []string
145+}
146+
147+func newToolCallAccumulator() *toolCallAccumulator {
148+ return &toolCallAccumulator{byIndex: make(map[int]int)}
149+}
150+
151+func (a *toolCallAccumulator) begin(evt provider.StreamEvent) {
152+ pos := len(a.calls)
153+ a.byIndex[evt.ToolCallIndex] = pos
154+ a.calls = append(a.calls, provider.ToolCall{ID: evt.ToolCallID, Name: evt.ToolCallName})
155+ a.argBuilders = append(a.argBuilders, "")
156+}
157+
158+func (a *toolCallAccumulator) delta(evt provider.StreamEvent) {
159+ if pos, ok := a.byIndex[evt.ToolCallIndex]; ok {
160+ a.argBuilders[pos] += evt.ToolCallArgs
129161 }
162+}
130163
131- return nil
164+func (a *toolCallAccumulator) finalize() []provider.ToolCall {
165+ for i := range a.calls {
166+ if i < len(a.argBuilders) {
167+ a.calls[i].Arguments = json.RawMessage(a.argBuilders[i])
168+ }
169+ }
170+ return a.calls
132171 }
133172
134173 // collectWithDeltas reads all events from a stream, forwarding each to onDelta,
135174 // and assembles them into a ChatResponse.
136175 func collectWithDeltas(ch <-chan provider.StreamEvent, onDelta DeltaCallback) (*provider.ChatResponse, error) {
137176 var result provider.ChatResponse
138- // Map from tool call index to position in our slice
139- toolCallsByIndex := make(map[int]int)
140- var toolCalls []provider.ToolCall
141- var toolArgBuilders []string
177+ tca := newToolCallAccumulator()
142178
143179 for evt := range ch {
144- // Forward delta to callback
145180 if onDelta != nil {
146181 onDelta(evt)
147182 }
@@ -152,16 +187,15 @@ func collectWithDeltas(ch <-chan provider.StreamEvent, onDelta DeltaCallback) (*
152187 case provider.EventReasoningDelta:
153188 result.Reasoning += evt.Content
154189 case provider.EventToolCallBegin:
155- pos := len(toolCalls)
156- toolCallsByIndex[evt.ToolCallIndex] = pos
157- toolCalls = append(toolCalls, provider.ToolCall{
158- ID: evt.ToolCallID,
159- Name: evt.ToolCallName,
160- })
161- toolArgBuilders = append(toolArgBuilders, "")
190+ tca.begin(evt)
162191 case provider.EventToolCallDelta:
163- if pos, ok := toolCallsByIndex[evt.ToolCallIndex]; ok {
164- toolArgBuilders[pos] += evt.ToolCallArgs
192+ tca.delta(evt)
193+ case provider.EventUsage:
194+ if evt.InputTokens > result.InputTokens {
195+ result.InputTokens = evt.InputTokens
196+ }
197+ if evt.OutputTokens > result.OutputTokens {
198+ result.OutputTokens = evt.OutputTokens
165199 }
166200 case provider.EventError:
167201 return nil, evt.Err
@@ -170,16 +204,9 @@ func collectWithDeltas(ch <-chan provider.StreamEvent, onDelta DeltaCallback) (*
170204 }
171205 }
172206
173- // Finalize tool call arguments
174- for i := range toolCalls {
175- if i < len(toolArgBuilders) {
176- toolCalls[i].Arguments = json.RawMessage(toolArgBuilders[i])
177- }
207+ if calls := tca.finalize(); len(calls) > 0 {
208+ result.ToolCalls = calls
178209 }
179- if len(toolCalls) > 0 {
180- result.ToolCalls = toolCalls
181- }
182-
183210 return &result, nil
184211 }
185212