xonecas.io
62159
Sy

symb / commit 3afe694

feat: token count per message

xonecas2026-02-15 3afe69407b5a3a74411120f26c14de1657177355 parent 7ac3e0d

12 files changed

internal/llm/loop.go+85−58
@@ -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
internal/provider/anthropic.go+51−1
@@ -71,6 +71,23 @@ type anthropicTool struct {
7171
7272 // Anthropic SSE streaming response types.
7373
74+// anthropicMessageStart wraps the message_start event payload.
75+type anthropicMessageStart struct {
76+ Message struct {
77+ Usage struct {
78+ InputTokens int `json:"input_tokens"`
79+ OutputTokens int `json:"output_tokens"`
80+ } `json:"usage"`
81+ } `json:"message"`
82+}
83+
84+// anthropicMessageDelta wraps the message_delta event payload.
85+type anthropicMessageDelta struct {
86+ Usage struct {
87+ OutputTokens int `json:"output_tokens"`
88+ } `json:"usage"`
89+}
90+
7491 type anthropicContentBlockStart struct {
7592 Type string `json:"type"`
7693 Index int `json:"index"`
@@ -250,7 +267,11 @@ func parseAnthropicSSEStream(ctx context.Context, reader io.Reader, ch chan<- St
250267 if !bt.handleBlockDelta(ctx, ch, data) {
251268 return
252269 }
253- case "ping", "message_start", "message_delta", "content_block_stop":
270+ case "message_start":
271+ handleAnthropicMessageStart(ctx, ch, data)
272+ case "message_delta":
273+ handleAnthropicMessageDelta(ctx, ch, data)
274+ case "ping", "content_block_stop":
254275 // Ignored
255276 }
256277
@@ -313,3 +334,32 @@ func (bt *anthropicBlockTracker) handleBlockDelta(ctx context.Context, ch chan<-
313334 }
314335 return true
315336 }
337+
338+// handleAnthropicMessageStart extracts input token usage from message_start events.
339+func handleAnthropicMessageStart(ctx context.Context, ch chan<- StreamEvent, data string) {
340+ var ms anthropicMessageStart
341+ if err := json.Unmarshal([]byte(data), &ms); err != nil {
342+ return
343+ }
344+ if ms.Message.Usage.InputTokens > 0 || ms.Message.Usage.OutputTokens > 0 {
345+ trySend(ctx, ch, StreamEvent{
346+ Type: EventUsage,
347+ InputTokens: ms.Message.Usage.InputTokens,
348+ OutputTokens: ms.Message.Usage.OutputTokens,
349+ })
350+ }
351+}
352+
353+// handleAnthropicMessageDelta extracts output token usage from message_delta events.
354+func handleAnthropicMessageDelta(ctx context.Context, ch chan<- StreamEvent, data string) {
355+ var md anthropicMessageDelta
356+ if err := json.Unmarshal([]byte(data), &md); err != nil {
357+ return
358+ }
359+ if md.Usage.OutputTokens > 0 {
360+ trySend(ctx, ch, StreamEvent{
361+ Type: EventUsage,
362+ OutputTokens: md.Usage.OutputTokens,
363+ })
364+ }
365+}
internal/provider/ollama.go+12−10
@@ -44,11 +44,12 @@ func (p *OllamaProvider) Name() string {
4444 // ChatStream sends messages with optional tools and returns a channel of streaming events.
4545 func (p *OllamaProvider) ChatStream(ctx context.Context, messages []Message, tools []Tool) (<-chan StreamEvent, error) {
4646 req := ollamaChatRequest{
47- Model: p.model,
48- Messages: mergeConsecutiveSystemMessagesOllama(toOllamaMessages(messages)),
49- Tools: toOllamaTools(tools),
50- Temperature: float32(p.temperature),
51- Stream: true,
47+ Model: p.model,
48+ Messages: mergeConsecutiveSystemMessagesOllama(toOllamaMessages(messages)),
49+ Tools: toOllamaTools(tools),
50+ Temperature: float32(p.temperature),
51+ Stream: true,
52+ StreamOptions: &chatStreamOptions{IncludeUsage: true},
5253 }
5354 body, err := json.Marshal(req)
5455 if err != nil {
@@ -81,11 +82,12 @@ func (p *OllamaProvider) ChatStream(ctx context.Context, messages []Message, too
8182 // They differ from OpenAI standard in structure and field names.
8283
8384 type ollamaChatRequest struct {
84- Model string `json:"model"`
85- Messages []ollamaReqMessage `json:"messages"`
86- Tools []ollamaReqTool `json:"tools,omitempty"`
87- Temperature float32 `json:"temperature,omitempty"`
88- Stream bool `json:"stream"`
85+ Model string `json:"model"`
86+ Messages []ollamaReqMessage `json:"messages"`
87+ Tools []ollamaReqTool `json:"tools,omitempty"`
88+ Temperature float32 `json:"temperature,omitempty"`
89+ Stream bool `json:"stream"`
90+ StreamOptions *chatStreamOptions `json:"stream_options,omitempty"`
8991 }
9092
9193 type ollamaReqMessage struct {
internal/provider/openai_common.go+19−0
@@ -20,6 +20,18 @@ import (
2020
2121 type chatCompletionStreamResponse struct {
2222 Choices []chatCompletionStreamChoice `json:"choices"`
23+ Usage *chatCompletionUsage `json:"usage,omitempty"`
24+}
25+
26+// chatCompletionUsage carries token counts from the final streaming chunk.
27+type chatCompletionUsage struct {
28+ PromptTokens int `json:"prompt_tokens"`
29+ CompletionTokens int `json:"completion_tokens"`
30+}
31+
32+// chatStreamOptions requests usage info in the streaming response.
33+type chatStreamOptions struct {
34+ IncludeUsage bool `json:"include_usage"`
2335 }
2436
2537 type chatCompletionStreamChoice struct {
@@ -168,6 +180,13 @@ func parseSSEStream(ctx context.Context, reader io.Reader, ch chan<- StreamEvent
168180 log.Warn().Err(err).Str("data", data).Msg("Failed to parse SSE chunk")
169181 continue
170182 }
183+ if chunk.Usage != nil {
184+ trySend(ctx, ch, StreamEvent{
185+ Type: EventUsage,
186+ InputTokens: chunk.Usage.PromptTokens,
187+ OutputTokens: chunk.Usage.CompletionTokens,
188+ })
189+ }
171190 if len(chunk.Choices) == 0 {
172191 continue
173192 }
internal/provider/opencode.go+12−10
@@ -13,11 +13,12 @@ import (
1313 // openCodeRequest is a custom request struct to ensure stream:false is serialized
1414 // The openai.ChatCompletionRequest has omitempty on Stream, which omits false values
1515 type openCodeRequest struct {
16- Model string `json:"model"`
17- Messages []openai.ChatCompletionMessage `json:"messages"`
18- Tools []openai.Tool `json:"tools,omitempty"`
19- Temperature float32 `json:"temperature,omitempty"`
20- Stream bool `json:"stream"` // NO omitempty - always serialize
16+ Model string `json:"model"`
17+ Messages []openai.ChatCompletionMessage `json:"messages"`
18+ Tools []openai.Tool `json:"tools,omitempty"`
19+ Temperature float32 `json:"temperature,omitempty"`
20+ Stream bool `json:"stream"` // NO omitempty - always serialize
21+ StreamOptions *chatStreamOptions `json:"stream_options,omitempty"`
2122 }
2223
2324 // OpenCodeProvider implements the Provider interface for OpenCode Zen.
@@ -85,11 +86,12 @@ func (p *OpenCodeProvider) chatStreamOpenAI(ctx context.Context, messages []Mess
8586 openaiTools := toOpenAITools(tools)
8687
8788 customReq := openCodeRequest{
88- Model: p.model,
89- Messages: mergeSystemMessagesOpenAI(toOpenAIMessages(messages)),
90- Tools: openaiTools,
91- Temperature: float32(p.temperature),
92- Stream: true,
89+ Model: p.model,
90+ Messages: mergeSystemMessagesOpenAI(toOpenAIMessages(messages)),
91+ Tools: openaiTools,
92+ Temperature: float32(p.temperature),
93+ Stream: true,
94+ StreamOptions: &chatStreamOptions{IncludeUsage: true},
9395 }
9496 body, err := json.Marshal(customReq)
9597 if err != nil {
internal/provider/provider.go+19−9
@@ -13,12 +13,14 @@ var ErrProviderNotFound = errors.New("provider not found")
1313
1414 // Message represents a chat message.
1515 type Message struct {
16- Role string
17- Content string
18- Reasoning string // Model reasoning/thinking content (optional)
19- ToolCalls []ToolCall // For assistant messages with tool calls
20- ToolCallID string // For tool result messages
21- CreatedAt time.Time // Message timestamp
16+ Role string
17+ Content string
18+ Reasoning string // Model reasoning/thinking content (optional)
19+ ToolCalls []ToolCall // For assistant messages with tool calls
20+ ToolCallID string // For tool result messages
21+ CreatedAt time.Time // Message timestamp
22+ InputTokens int // Token usage for this LLM call (assistant messages only)
23+ OutputTokens int // Token usage for this LLM call (assistant messages only)
2224 }
2325
2426 // Tool represents a tool/function definition for the LLM.
@@ -37,9 +39,11 @@ type ToolCall struct {
3739
3840 // ChatResponse represents the response from a chat completion.
3941 type ChatResponse struct {
40- Content string // Text content (may be empty if tool calls)
41- ToolCalls []ToolCall // Tool calls (may be empty if text response)
42- Reasoning string // Model reasoning content (optional)
42+ Content string // Text content (may be empty if tool calls)
43+ ToolCalls []ToolCall // Tool calls (may be empty if text response)
44+ Reasoning string // Model reasoning content (optional)
45+ InputTokens int // Input/prompt token count (0 if unavailable)
46+ OutputTokens int // Output/completion token count (0 if unavailable)
4347 }
4448
4549 // StreamEventType identifies the kind of streaming event.
@@ -54,6 +58,8 @@ const (
5458 EventToolCallBegin
5559 // EventToolCallDelta carries a chunk of tool call arguments.
5660 EventToolCallDelta
61+ // EventUsage carries token usage statistics.
62+ EventUsage
5763 // EventDone signals the stream is complete.
5864 EventDone
5965 // EventError signals a stream error.
@@ -73,6 +79,10 @@ type StreamEvent struct {
7379 ToolCallName string // Set on EventToolCallBegin
7480 ToolCallArgs string // Argument fragment on EventToolCallDelta
7581
82+ // Token usage (for EventUsage).
83+ InputTokens int
84+ OutputTokens int
85+
7686 // Error (for EventError).
7787 Err error
7888 }
internal/store/session.go+16−12
@@ -17,12 +17,14 @@ type Session struct {
1717
1818 // SessionMessage is a persisted chat message.
1919 type SessionMessage struct {
20- Role string
21- Content string
22- Reasoning string
23- ToolCalls json.RawMessage // JSON array
24- ToolCallID string
25- CreatedAt time.Time
20+ Role string
21+ Content string
22+ Reasoning string
23+ ToolCalls json.RawMessage // JSON array
24+ ToolCallID string
25+ CreatedAt time.Time
26+ InputTokens int
27+ OutputTokens int
2628 }
2729
2830 // CreateSession inserts a new session and returns its ID.
@@ -79,9 +81,10 @@ func (c *Cache) writeMessage(sessionID string, msg SessionMessage) {
7981 }
8082
8183 _, err := c.db.Exec(
82- `INSERT INTO messages (session_id, role, content, reasoning, tool_calls, tool_call_id, created)
83- VALUES (?, ?, ?, ?, ?, ?, ?)`,
84+ `INSERT INTO messages (session_id, role, content, reasoning, tool_calls, tool_call_id, created, input_tokens, output_tokens)
85+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
8486 sessionID, msg.Role, msg.Content, msg.Reasoning, string(tc), msg.ToolCallID, msg.CreatedAt.Unix(),
87+ msg.InputTokens, msg.OutputTokens,
8588 )
8689 if err != nil {
8790 log.Warn().Err(err).Str("session", sessionID).Msg("failed to save message")
@@ -106,9 +109,10 @@ func (c *Cache) SaveMessageSync(sessionID string, msg SessionMessage) (int64, er
106109 }
107110
108111 res, err := c.db.Exec(
109- `INSERT INTO messages (session_id, role, content, reasoning, tool_calls, tool_call_id, created)
110- VALUES (?, ?, ?, ?, ?, ?, ?)`,
112+ `INSERT INTO messages (session_id, role, content, reasoning, tool_calls, tool_call_id, created, input_tokens, output_tokens)
113+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
111114 sessionID, msg.Role, msg.Content, msg.Reasoning, string(tc), msg.ToolCallID, msg.CreatedAt.Unix(),
115+ msg.InputTokens, msg.OutputTokens,
112116 )
113117 if err != nil {
114118 return 0, err
@@ -158,7 +162,7 @@ func (c *Cache) LoadMessages(sessionID string) ([]SessionMessage, error) {
158162 defer c.mu.Unlock()
159163
160164 rows, err := c.db.Query(
161- `SELECT role, content, reasoning, tool_calls, tool_call_id, created
165+ `SELECT role, content, reasoning, tool_calls, tool_call_id, created, input_tokens, output_tokens
162166 FROM messages WHERE session_id = ? ORDER BY id`, sessionID,
163167 )
164168 if err != nil {
@@ -171,7 +175,7 @@ func (c *Cache) LoadMessages(sessionID string) ([]SessionMessage, error) {
171175 var m SessionMessage
172176 var tc string
173177 var created int64
174- if err := rows.Scan(&m.Role, &m.Content, &m.Reasoning, &tc, &m.ToolCallID, &created); err != nil {
178+ if err := rows.Scan(&m.Role, &m.Content, &m.Reasoning, &tc, &m.ToolCallID, &created, &m.InputTokens, &m.OutputTokens); err != nil {
175179 continue
176180 }
177181 m.ToolCalls = json.RawMessage(tc)
internal/store/store.go+6−0
@@ -109,6 +109,12 @@ func Open(dbPath string, ttl time.Duration) (*Cache, error) {
109109 return nil, fmt.Errorf("create schema: %w", err)
110110 }
111111
112+ // Migrate: add token count columns to messages table.
113+ if !hasColumn(db, "messages", "input_tokens") {
114+ db.Exec("ALTER TABLE messages ADD COLUMN input_tokens INTEGER NOT NULL DEFAULT 0") //nolint:errcheck
115+ db.Exec("ALTER TABLE messages ADD COLUMN output_tokens INTEGER NOT NULL DEFAULT 0") //nolint:errcheck
116+ }
117+
112118 c := &Cache{
113119 db: db,
114120 ttl: ttl,
internal/tui/conv.go+37−11
@@ -1,6 +1,7 @@
11 package tui
22
33 import (
4+ "fmt"
45 "strings"
56
67 "charm.land/lipgloss/v2"
@@ -109,20 +110,45 @@ func (m *Model) wrappedConvLines() []string {
109110 return lines
110111 }
111112
112-// makeSeparator builds a timestamp separator line.
113-func (m Model) makeSeparator(dur string, ts string) string {
114- label := dur + " " + ts + " "
115- fill := m.convWidth() - lipgloss.Width(label)
116- if fill < 0 {
117- fill = 0
113+// formatTokens formats a token count for display (e.g. 1234 -> "1.2k").
114+func formatTokens(n int) string {
115+ if n < 1000 {
116+ return fmt.Sprintf("%d", n)
118117 }
119- return m.styles.Dim.Render(label + strings.Repeat("─", fill))
118+ return fmt.Sprintf("%.1fk", float64(n)/1000)
120119 }
121120
122-// makeUndoEntry creates a convEntry for the undo control.
123-// sepDisplay is the styled separator text to restore if this entry is demoted.
124-func (m Model) makeUndoEntry(sepDisplay string) convEntry {
125- return convEntry{display: m.styles.Dim.Render("undo"), kind: entryUndo, full: sepDisplay}
121+// makeSeparator builds a right-aligned timestamp/token separator line.
122+func (m Model) makeSeparator(dur string, ts string, tokIn, tokOut, totalTok int) string {
123+ var label string
124+ if tokIn > 0 || tokOut > 0 {
125+ label = fmt.Sprintf("%s %s %sin/%sout (%s)", dur, ts, formatTokens(tokIn), formatTokens(tokOut), formatTokens(totalTok))
126+ } else {
127+ label = dur + " " + ts
128+ }
129+ w := m.convWidth()
130+ pad := w - lipgloss.Width(label)
131+ if pad < 0 {
132+ pad = 0
133+ }
134+ return m.styles.Dim.Render(strings.Repeat(" ", pad) + label)
135+}
136+
137+// makeUndoEntry creates convEntries for the undo control: the separator line
138+// plus a right-aligned "undo" line below it.
139+// sepDisplay is the styled separator text to restore if the undo entry is demoted.
140+func (m Model) makeUndoEntry(sepDisplay string) []convEntry {
141+ undoLabel := "undo"
142+ w := m.convWidth()
143+ pad := w - lipgloss.Width(undoLabel)
144+ if pad < 0 {
145+ pad = 0
146+ }
147+ undoLine := m.styles.Dim.Render(strings.Repeat(" ", pad) + undoLabel)
148+ return []convEntry{
149+ {display: sepDisplay, kind: entrySeparator, full: sepDisplay},
150+ {display: undoLine, kind: entryUndo, full: sepDisplay},
151+ }
126152 }
127153
128154 // visibleStartLine returns the index of the first visible wrapped conversation line.
internal/tui/messages.go+27−10
@@ -31,8 +31,15 @@ type llmToolResultMsg struct {
3131 }
3232
3333 type llmDoneMsg struct {
34- duration time.Duration
35- timestamp string
34+ duration time.Duration
35+ timestamp string
36+ inputTokens int
37+ outputTokens int
38+}
39+
40+type llmUsageMsg struct {
41+ inputTokens int
42+ outputTokens int
3643 }
3744
3845 type llmHistoryMsg struct{ msg provider.Message }
@@ -124,6 +131,7 @@ func (m Model) processLLM() tea.Cmd {
124131 }
125132
126133 start := time.Now()
134+ var turnIn, turnOut int
127135 err := llm.ProcessTurn(ctx, llm.ProcessTurnOptions{
128136 Provider: prov,
129137 Proxy: proxy,
@@ -138,6 +146,11 @@ func (m Model) processLLM() tea.Cmd {
138146 ch <- llmReasoningDeltaMsg{content: evt.Content}
139147 }
140148 },
149+ OnUsage: func(inputTokens, outputTokens int) {
150+ turnIn += inputTokens
151+ turnOut += outputTokens
152+ ch <- llmUsageMsg{inputTokens: inputTokens, outputTokens: outputTokens}
153+ },
141154 OnMessage: func(msg provider.Message) {
142155 ch <- llmHistoryMsg{msg: msg}
143156 switch msg.Role {
@@ -167,8 +180,10 @@ func (m Model) processLLM() tea.Cmd {
167180 return
168181 }
169182 ch <- llmDoneMsg{
170- duration: time.Since(start),
171- timestamp: start.Format("15:04"),
183+ duration: time.Since(start),
184+ timestamp: start.Format("15:04"),
185+ inputTokens: turnIn,
186+ outputTokens: turnOut,
172187 }
173188 }()
174189 return nil
@@ -182,12 +197,14 @@ func messageToStore(msg provider.Message) store.SessionMessage {
182197 tc, _ = json.Marshal(msg.ToolCalls) //nolint:errcheck
183198 }
184199 return store.SessionMessage{
185- Role: msg.Role,
186- Content: msg.Content,
187- Reasoning: msg.Reasoning,
188- ToolCalls: tc,
189- ToolCallID: msg.ToolCallID,
190- CreatedAt: msg.CreatedAt,
200+ Role: msg.Role,
201+ Content: msg.Content,
202+ Reasoning: msg.Reasoning,
203+ ToolCalls: tc,
204+ ToolCallID: msg.ToolCallID,
205+ CreatedAt: msg.CreatedAt,
206+ InputTokens: msg.InputTokens,
207+ OutputTokens: msg.OutputTokens,
191208 }
192209 }
193210
internal/tui/tui.go+11−3
@@ -167,9 +167,11 @@ type FileReadResetter interface {
167167
168168 // turnBoundary marks the start of a user turn in both history and convEntries.
169169 type turnBoundary struct {
170- historyIdx int // index in m.history where the user message is
171- convIdx int // index in m.convEntries where this turn's display starts
172- dbMsgID int64 // messages.id of the user message (for DB cleanup)
170+ historyIdx int // index in m.history where the user message is
171+ convIdx int // index in m.convEntries where this turn's display starts
172+ dbMsgID int64 // messages.id of the user message (for DB cleanup)
173+ inputTokens int // total input tokens at start of this turn
174+ outputTokens int // total output tokens at start of this turn
173175 }
174176
175177 // ---------------------------------------------------------------------------
@@ -216,6 +218,12 @@ type Model struct {
216218 streaming bool // Whether we're currently streaming
217219 streamEntryStart int // Index in convEntries where streaming entries begin (-1 = none)
218220
221+ // Token usage tracking
222+ turnInputTokens int // accumulated input tokens for current turn
223+ turnOutputTokens int // accumulated output tokens for current turn
224+ totalInputTokens int // session-wide total input tokens
225+ totalOutputTokens int // session-wide total output tokens
226+
219227 // Undo
220228 deltaTracker *delta.Tracker
221229 turnBoundaries []turnBoundary
internal/tui/update.go+32−15
@@ -185,16 +185,20 @@ func (m *Model) handleUserMsg(msg llmUserMsg) Model {
185185 }
186186
187187 m.turnBoundaries = append(m.turnBoundaries, turnBoundary{
188- historyIdx: historyIdx,
189- convIdx: convIdx,
190- dbMsgID: dbMsgID,
188+ historyIdx: historyIdx,
189+ convIdx: convIdx,
190+ dbMsgID: dbMsgID,
191+ inputTokens: m.totalInputTokens,
192+ outputTokens: m.totalOutputTokens,
191193 })
192194
193195 m.appendText(highlightMarkdown(msg.content, m.styles.Text)...)
194196 m.appendText("")
195- sep := m.makeSeparator("0s", now.Format("15:04:05"))
197+ sep := m.makeSeparator("0s", now.Format("15:04:05"), 0, 0, 0)
196198 wasBottom := m.appendText(sep)
197199 m.appendText("")
200+ m.turnInputTokens = 0
201+ m.turnOutputTokens = 0
198202 if wasBottom {
199203 m.scrollOffset = 0
200204 }
@@ -232,11 +236,18 @@ func (m Model) handleLLMBatch(batch llmBatchMsg) (tea.Model, tea.Cmd) {
232236 m.appendText("", m.styles.Error.Render("Error: "+msg.err.Error()), "")
233237 return m, nil
234238
239+ case llmUsageMsg:
240+ m.turnInputTokens += msg.inputTokens
241+ m.turnOutputTokens += msg.outputTokens
242+ m.totalInputTokens += msg.inputTokens
243+ m.totalOutputTokens += msg.outputTokens
244+
235245 case llmDoneMsg:
236246 m.demoteOldUndo()
237247 m.appendText("")
238- sep := m.makeSeparator(msg.duration.Round(time.Second).String(), msg.timestamp)
239- m.appendConv(m.makeUndoEntry(sep))
248+ sep := m.makeSeparator(msg.duration.Round(time.Second).String(), msg.timestamp,
249+ msg.inputTokens, msg.outputTokens, m.totalInputTokens+m.totalOutputTokens)
250+ m.appendConv(m.makeUndoEntry(sep)...)
240251 m.trimOldTurns()
241252 return m, nil
242253 }
@@ -474,16 +485,13 @@ func (m *Model) updateComponentSizes() {
474485 m.agentInput.SetHeight(inputRows)
475486 }
476487
477-// demoteOldUndo finds the existing entryUndo in convEntries and demotes it
478-// to an entrySeparator (preserving the original display for potential
479-// re-promotion). Called before appending a new undo entry so only the
480-// latest turn shows the undo control.
488+// demoteOldUndo finds the existing entryUndo in convEntries and removes it.
489+// The entrySeparator line above it is already present and stays.
490+// Called before appending a new undo entry so only the latest turn shows the undo control.
481491 func (m *Model) demoteOldUndo() {
482492 for i := len(m.convEntries) - 1; i >= 0; i-- {
483493 if m.convEntries[i].kind == entryUndo {
484- m.convEntries[i].kind = entrySeparator
485- // full field carries the original separator display text.
486- m.convEntries[i].display = m.convEntries[i].full
494+ m.convEntries = append(m.convEntries[:i], m.convEntries[i+1:]...)
487495 return
488496 }
489497 }
@@ -528,6 +536,12 @@ func (m *Model) handleUndo() Model {
528536 tb := m.turnBoundaries[len(m.turnBoundaries)-1]
529537 m.turnBoundaries = m.turnBoundaries[:len(m.turnBoundaries)-1]
530538
539+ // Restore token totals to the snapshot at turn start.
540+ m.totalInputTokens = tb.inputTokens
541+ m.totalOutputTokens = tb.outputTokens
542+ m.turnInputTokens = 0
543+ m.turnOutputTokens = 0
544+
531545 // 1. Reverse filesystem changes.
532546 var undoErr error
533547 var restoredFiles []string
@@ -569,8 +583,11 @@ func (m *Model) handleUndo() Model {
569583 if len(m.turnBoundaries) > 0 {
570584 for i := len(m.convEntries) - 1; i >= 0; i-- {
571585 if m.convEntries[i].kind == entrySeparator {
572- // full carries the original separator display text.
573- m.convEntries[i] = m.makeUndoEntry(m.convEntries[i].full)
586+ // Replace separator with separator+undo pair.
587+ entries := m.makeUndoEntry(m.convEntries[i].full)
588+ // Replace the separator entry in-place and insert undo after it.
589+ m.convEntries[i] = entries[0]
590+ m.convEntries = append(m.convEntries[:i+1], append([]convEntry{entries[1]}, m.convEntries[i+1:]...)...)
574591 break
575592 }
576593 }