Repository navigation
Description
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.gomapsResponse.Usage.InputTokens,OutputTokens, andInputTokensDetails.CachedTokens. - Anthropic Messages:
internal/adapter/anthropic/stream.gomapsUsage.InputTokens,OutputTokens, andCacheReadInputTokens. - Server events distinguish:
turn.end.turn_end.usage: per-turn usageresult.usage/event.usage: cumulative run total
- mecatui currently:
- accumulates footer session usage from terminal
ResultMsgonly (cmd/mecatui/ui/update.goapplyResult) - sets
contextTokens = msg.Usage.InputTokensfrom 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.Usageinto the mainm.usage
- accumulates footer session usage from terminal
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.InputTokensper response/turn, including cache-served tokens, and doesInputTokensDetails.CachedTokensmean cached subset of those tokens? - For Anthropic Messages streaming, are
input_tokensandcache_read_input_tokenssimilarly 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/ContextWindowvalue 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.contextTokensfrom eachTurnEndMsg.Usage.InputTokens. - Accumulate
m.usagefrom 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 cumulative2.2K - terminal result cumulative usage does not overwrite context occupancy with cumulative input
- cache hit rate uses cumulative cache-read / cumulative input as intended
- two turns of 1000 + 1200 input show cumulative
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.gointernal/agent/loop.gousage handling andturn.end/resultemissioninternal/adapter/openai/stream.gomapUsageinternal/adapter/anthropic/stream.gousage mappingcontracts/proto/mecatl/v1/harness.protoUsage,Event,TurnEndcmd/mecatui/ui/update.goapplyResult,TurnEndMsghandlingcmd/mecatui/ui/footer.gorenderUsageFacets,cacheHitRate
Activity