Skip to content

Bill capture and cache: per-session token accounting the plugin can report itself #387

Closed

Description

@abogartz

Part of #386.

Goal

The plugin can say, for one session, what its own memory work cost — capture included,
cache accounted for, in the plugin's own status output rather than only in a bench harness.

Findings

  • Capture does not appear in the session's event stream. It is a separate model call made
    at session end, so anything reading the session's usage sees zero.
  • Its reply can land after opencode run has exited. The bench read the meter at that
    instant and reported capture as free for its entire history. Fixed in the bench by
    draining until capture requests and replies balance.
  • Measured cost: roughly 5,000 completion tokens per session, about a third of what the
    session itself generates, with cached_tokens: 0 because the capture prompt is the
    prefix of nothing.
  • 94% of capture's completion tokens are reasoning.
  • The provider reports input as only the uncached remainder, so an event-stream total is
    not a cost. Measured: 13,393 reported against 187,856 actually moved.

Acceptance criteria

  1. status (or the ledger) reports, per session: generation tokens, capture tokens, cache
    read tokens, and the number of capture calls, sourced from provider usage rather than
    from an internal estimate.
  2. A session that triggers no capture reports capture cost 0 and says why (no signal, gate
    refused, model disabled), so 0 is never ambiguous between "free" and "never ran".
  3. A capture call whose reply arrives after the session ends is still attributed to that
    session, proven by a test that delays the reply.
  4. The bench's billedFrom and the plugin's own number agree within 2% on the same wire
    log, so the harness is not the only thing that can see this.
  5. Nothing in the repo quotes a session cost that excludes capture.

Not in scope

Making capture cheaper. That is the sibling ticket, and it should be decided with this
number visible rather than guessed at without it.

Activity

  1. added
    capture
    bench
    severity:medium
    on Nov 11, 2026
  2. abogartz commented on Nov 11, 2026

    @abogartz
    OwnerAuthor

    PR #394 ships the part that is buildable. Criterion 1's capture half cannot be met with the current host API, and criterion 4 depends on it.

    Probed against a running host with a throwaway plugin, 2026-11-11:

    • ctx.generate exposes exactly one member: text.
    • generate.text({ model, prompt }) resolves to { text } — no usage, no tokens.
    • Capture emits no usage event either: every bench store holds one status/<session>.json, and its measured covers only the session's own turns.
    • ctx.session does expose create / prompt / wait; session.prompt accepted the call and blocked without returning in the probe window.

    So the choice was an estimate that says it is an estimate, or a zero. #394 ships chars/4 with receipt=none on the line and "estimate, no provider receipt" in the render.

    Met: criterion 2 (zero with a reason; an unread cost renders nothing), criterion 3 (tested with a parked reply — the receipt carries session= and appears when the reply lands, which is the race that billed capture as free), criterion 5 (README updated; no other repo claim quotes a session token figure).

    Also fixed, and it was a live one: Number("") is 0, so a capture_cost line whose character count failed to render would have been billed as a free capture — the exact failure this ticket exists to remove. Blank counts are now skipped, not read as zero.

    Session side is genuinely provider-sourced: Measured now carries output and reasoning, split, because 94% of a capture call's completion tokens were reasoning and folding them hides the cost. Optional on the persisted payload so a newer panel reading an older file shows nothing rather than "generated nothing".

    Criteria 1 (capture) and 4 moved to #395, which names both routes and their costs. Criterion 3 of #395 re-verifies the base4 transport result, because moving capture onto ctx.session.* changes the one component whose output that benchmark measures.

    Verification: bun run gate — 1204 unit tests, 30 e2e against a real opencode host, typecheck and lint clean.

  3. added a commit that references this issue on Nov 12, 2026
  4. abogartz-opencode-self-improvement commented on Nov 12, 2026

    @abogartz-opencode-self-improvement

    🎉 This issue has been resolved in version 0.24.0 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

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

    benchcapturereleasedseverity:medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions