Skip to content

bug: Streaming responses record 0 actual tokens — cost and quota tracking broken for all streams #62

Closed

Description

@stackbilt-admin

Bug

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 responses
const usage = { 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

  1. Pass stream_options: { include_usage: true } in OpenAI/Groq/Cerebras streaming requests
  2. Buffer the final SSE chunk in each provider's stream path and extract usage
  3. 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

Found by

Codebase audit (automated) — src/factory.ts:1047–1090

Activity

  1. stackbilt-admin commented on May 31, 2026

    @stackbilt-admin
    MemberAuthor

    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:

    • 745507c fix: record streaming token usage
    • eb3c472 fix: attach cloudflare stream usage

    What changed:

    • 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.

  2. stackbilt-admin commented on May 31, 2026

    @stackbilt-admin
    MemberAuthor

    Resolved on main.

    Pushed commits:

    • 745507c fix: record streaming token usage
    • eb3c472 fix: attach cloudflare stream usage

    Acceptance coverage:

    • Streaming usage is attached by OpenAI, Groq, Cerebras, NVIDIA, Anthropic, and Cloudflare stream paths.
    • Factory records attached stream usage to cost tracking, quota hooks, and request-end hooks after stream drain.
    • Cloudflare now avoids zero-token accounting by using Workers AI usage when present and non-zero estimated usage from streamed content otherwise.

    Validation:

    • 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

    No release/tag yet; this closes only the tracker item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions