Skip to content

token accounting: investigate inflated input/context counts and fix footer semantics #25

Closed

Description

@JAORMX

Context

The mecatui footer can show implausibly high upload/input-token totals for short conversations. In one short session it showed roughly:

ctx 773.4K · ↑2.9M ↓7.6K cache 65%

That does not look right for the amount of visible conversation. We should research whether this is a real provider-reported cost, a UI aggregation bug, a context-meter bug, or a mismatch between "billed input", "current context occupancy", and "cumulative uploaded tokens".

Current implementation notes

Provider usage is already available from the LLM APIs and mapped into session.Usage:

  • OpenAI Responses: internal/adapter/openai/stream.go maps Response.Usage.InputTokens, OutputTokens, and InputTokensDetails.CachedTokens.
  • Anthropic Messages: internal/adapter/anthropic/stream.go maps Usage.InputTokens, OutputTokens, and CacheReadInputTokens.
  • Server events distinguish:
    • turn.end.turn_end.usage: per-turn usage
    • result.usage / event.usage: cumulative run total
  • mecatui currently:
    • accumulates footer session usage from terminal ResultMsg only (cmd/mecatui/ui/update.go applyResult)
    • sets contextTokens = msg.Usage.InputTokens from the terminal result, whose usage is cumulative run usage, despite the comment saying it is the latest turn's prompt size
    • does not fold per-turn TurnEndMsg.Usage into the main m.usage

That looks suspicious: the footer may be using cumulative run input tokens as the current context numerator, and the semantics of ↑ may be unclear (cumulative billed input vs current context vs upload bytes).

Questions to answer

  • Are the high numbers actually coming from provider-reported usage, or from our aggregation/mapping?
  • For OpenAI Responses streaming, is Response.Usage.InputTokens per response/turn, including cache-served tokens, and does InputTokensDetails.CachedTokens mean cached subset of those tokens?
  • For Anthropic Messages streaming, are input_tokens and cache_read_input_tokens similarly per request?
  • Does either provider expose a more reliable current-context / prompt-token count separate from billed per-call input?
  • Should mecatui display:
    • current context occupancy (latest turn input tokens)
    • cumulative session input tokens (sum of per-turn input tokens)
    • cache read/write split
    • billed estimate (input minus cache discount, if pricing known) separately?
  • Should the loop/server emit a dedicated ContextUsage/ContextWindow value instead of clients inferring context occupancy from cumulative usage?

Likely bug hypothesis

ResultMsg.Usage is cumulative run usage, so using msg.Usage.InputTokens as m.contextTokens makes the context meter show cumulative input uploaded over the run, not the latest model-call input/context size.

TurnEndMsg.Usage.InputTokens is the per-turn prompt size and is the better approximation for current context occupancy. If there is no turn.end, the result usage should not be treated as current context unless it is known to be per-turn.

Scope

Research and fix token/context accounting semantics across provider → agent → server → mecatui.

Minimum likely fix:

  • In mecatui, update m.contextTokens from each TurnEndMsg.Usage.InputTokens.
  • Accumulate m.usage from per-turn usage or from final result usage, but not both.
  • Rename/clarify footer labels if ↑ means cumulative provider input, not current upload.
  • Add tests that prove:
    • two turns of 1000 + 1200 input show cumulative ↑2.2K
    • context meter uses latest turn 1.2K, not cumulative 2.2K
    • terminal result cumulative usage does not overwrite context occupancy with cumulative input
    • cache hit rate uses cumulative cache-read / cumulative input as intended

Follow-up/optional:

  • Add an explicit server/client field for current context occupancy so UI does not infer it from token usage.
  • Add provider diagnostics that log raw provider usage for a turn when debug/perf telemetry is enabled.
  • Consider exposing cache write tokens where providers support it.

References

  • internal/session/usage.go
  • internal/agent/loop.go usage handling and turn.end / result emission
  • internal/adapter/openai/stream.go mapUsage
  • internal/adapter/anthropic/stream.go usage mapping
  • contracts/proto/mecatl/v1/harness.proto Usage, Event, TurnEnd
  • cmd/mecatui/ui/update.go applyResult, TurnEndMsg handling
  • cmd/mecatui/ui/footer.go renderUsageFacets, cacheHitRate

Activity

  1. added
    bug
    on Jun 9, 2026
  2. added
    tui
    fit-and-finish
    and removed
    tui
    fit-and-finish
    on Aug 13, 2026
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

    bugresearchux

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions