Skip to content

providerAsSandboxClient prompt() sums usage across events without reading usageMode, so cumulative receipts are counted once per event #1216

Description

@drewstone

Observation

providerAsSandboxClient(provider).create() returns an instance whose prompt() folds every streamed event with mergeTokenUsage, which adds the counters (src/runtime/environment-provider.ts:1538 and :2501-2532 on origin/main 3b33fe9f).
It never reads AgentEnvironmentEvent.usageMode.
The interface defines cumulative as "the total so far for this turn" (agent-interface environment-runtime.ts:274-278), so each cumulative event restates earlier usage.

Reproduction against agent-runtime 0.219.0, with a stub provider whose stream is a cumulative step total, then result and done carrying the same cumulative receipt:

events: raw {60,6} cumulative, result {100,10} cumulative, done {100,10} cumulative
prompt().usage: { inputTokens: 260, outputTokens: 26 }   // receipt is { 100, 10 }

The executor path is correct: streamProviderExecutor credits the same events through createSandboxUsageLedger, which honors usageMode and marks a falling cumulative total unknown.

Reach today

@tangle-network/agent-provider-tangle cannot reach this path yet, because providerAsSandboxClient always sends providerOptions.sandboxCreateOptions and that provider refuses non-empty create providerOptions.
It becomes reachable for Tangle as soon as that composition works.
The Tangle provider already marks both result and done cumulative, and agent-sdk#327 adds cumulative step totals for OpenCode executions (agent-sdk#324).

Ask

Fold prompt() usage by mode: replace the running total with a cumulative event's usage, add a delta event's usage, and treat usage with no declared mode as it is treated in the ledger.

Activity

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