Skip to content

[Agent Loop] Streaming token/cost budgets (maxToolTokens/maxCostUsd on generateStream) #854

Description

@heskew

Part of #612.

Enforce maxToolTokens / maxCostUsd on the streaming auto path (generateStream). Today these throw 501 at stream entry; the sync path enforces them.

Why deferred

GenerateChunk doesn't expose usage in v1, so cumulative spend isn't observable from a stream. Wiring this needs the chunk type extended plus each in-tree backend (openai / anthropic / bedrock) emitting usage on its final chunk.

Acceptance

  • GenerateChunk (or a terminal metadata chunk) carries usage.
  • Backends populate it on the stream's final chunk.
  • Streaming loop tallies cumulative usage and trips BudgetExceededError like the sync path.
  • Tests: streaming token-budget trip and cost-budget trip.

Note: the maxToolIterations cap already applies to streaming; this closes the spend-budget gap only.


🤖 Generated with Claude Code

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

    Fields

    Priority

    P3

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions