Skip to content

fix: keep chat token usage for streams ending with usage-less chunks - #28068

Open
ibetitsmike wants to merge 1 commit into
mainfrom
mike/fantasy-stream-usage-bump
Open

fix: keep chat token usage for streams ending with usage-less chunks#28068
ibetitsmike wants to merge 1 commit into
mainfrom
mike/fantasy-stream-usage-bump

Conversation

@ibetitsmike

Copy link
Copy Markdown
Collaborator

Bumps the coder/fantasy fork pin to pick up coder/fantasy#52.

Problem

Chats on poolside/laguna-xs-2.1 showed no context usage: every assistant message persisted NULL token columns, and automatic compaction never triggered, so chats ran to context overflow. AIBridge recorded correct usage for the same requests, so the loss was client-side in fantasy.

When tools are declared, laguna-xs reports cumulative usage on every delta chunk and ends the stream with a finish_reason chunk whose usage is null, with no trailing usage-only chunk. Fantasy's chat-completions stream loops reassigned usage from every chunk, and the default stream usage hook returns zero usage for usage-less chunks, so the trailing finish chunk wiped the real usage one chunk earlier. The Finish part then reported Usage{0,0,0}, which chatd persists as NULL (nullInt64IfNonZero).

Fix

coder/fantasy#52 adopts the stream usage hook's result only when the chunk actually carries usage, in both the chat-completions stream loop and the JSON-mode object stream loop. This mirrors the aibridge fix in #27967, which is why the gateway recorded usage correctly while fantasy lost it. Spec-compliant backends that emit usage once on the final chunk are unaffected.

This PR pins the fork at the merged commit and documents the fork-only patch in the go.mod comment block.

Validation

Mux acted on Mike's behalf to create this PR.

…chunks

Picks up coder/fantasy#52: OpenAI-compatible backends that report
cumulative usage on delta chunks and end with a usage-less finish chunk
(observed with Poolside laguna-xs when tools are declared) no longer
have their usage zeroed at Finish. Restores per-message token
accounting, the context gauge, and automatic compaction for such
models.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant