You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug: Streaming responses record 0 actual tokens — cost and quota tracking broken for all streams #62
Every streaming response is recorded with zero input and output tokens. Cost and quota tracking are entirely inaccurate for streamed requests.
Location:src/factory.ts:1064
// buildFactoryStream() — called for all streaming responsesconstusage={inputTokens: 0,outputTokens: 0,totalTokens: 0,cost: estimatedCost};
The token fields are hardcoded to 0. Only the pre-request cost estimate (derived from character count) is stored. No post-stream reconciliation happens.
Downstream effects:
CreditLedger spend is based on rough estimates, not actual usage — monthly budgets can drift significantly
CostTracker per-provider totals are wrong for any provider used via streaming
QuotaHook receives actualCost: estimatedCost with no token breakdown (src/factory.ts:1079)
Root cause
generateResponseStream() returns a ReadableStream<string> rather than an LLMResponse, so the token counts in the final SSE event are never extracted.
Providers do surface final usage in their SSE streams:
Anthropic:message_delta event with usage.output_tokens; message_start with usage.input_tokens
OpenAI / Groq / Cerebras: final data: chunk with usage.prompt_tokens / usage.completion_tokens when stream_options: { include_usage: true } is set
Fix
Pass stream_options: { include_usage: true } in OpenAI/Groq/Cerebras streaming requests
Buffer the final SSE chunk in each provider's stream path and extract usage
After the stream drains, call the same recordQuota / costTracker.trackCost path used by non-streaming responses
Acceptance criteria
Streaming responses record actual inputTokens and outputTokens post-stream
CreditLedger and CostTracker reflect real token counts for streamed requests
Existing streaming tests updated; new tests cover token accumulation across providers
OpenAI/Groq/Cerebras/NVIDIA request stream usage and attach final token usage to streams.
Anthropic parses message_start/message_delta stream usage and attaches it to streams.
Factory records attached stream usage to cost tracking, quota hooks, and request-end hooks after stream drain.
Cloudflare streams now attach usage too: provider-reported Workers AI usage when present, otherwise a non-zero estimated fallback from streamed content.
Validation run:
npm test -- src/tests/cloudflare.test.ts src/tests/factory.test.ts
npm run typecheck
npm test (19 files, 420 tests)
npm run build
npm run test:package
Current state: code is committed locally on main; issue should remain open until we push and do the final closeout pass.
Activity
stackbilt-admin commentedon May 31, 2026
Status update: implemented locally, pending push/release.
Follow-up patch added after adversarial review found Cloudflare streaming still lacked attached usage metadata.
Local commits covering this issue:
What changed:
Validation run:
Current state: code is committed locally on main; issue should remain open until we push and do the final closeout pass.
stackbilt-admin commentedon May 31, 2026
Resolved on main.
Pushed commits:
Acceptance coverage:
Validation:
No release/tag yet; this closes only the tracker item.