Claude Code reads a reply's input, cache_creation and cache_read as how
full its conversation is. A reply more than one upstream request made
summed all three across them. In the bridge: Claude Code's own
ToolSearch/WebSearch going on into the answering message, a TUI message
written after its call was told (lateUsage), a cut stream's retry
(b42ff722). Each later request re-sends what the earlier one had, so the
reporter's TaskStop reply after a ToolSearch said cache_read 1,154,857
(577,009 + 577,848) and cache_creation 829 (253 + 576) for a ~577k
conversation, and Claude Code compacted early. 12 of their 30 replies
after ToolSearch were about 2x.
The translated route summed the same way across its tries (#5115): the
empty reply asked again (#667), a loop's re-ask (#1359), a cut reply's
continuation (#853), and search.go's web search rounds. The Anthropic
passthrough has no in-reply fold; each try is its own call.
Usage.after (claude_subscription.go) replaces plus at these joins: the
client is told the last request's input, cache writes and cache reads,
and every request's output and reasoning (all of it was streamed). The
earlier requests' prompts go in Deferred, which the ledger bills
(billed()) and no encoder tells. Usage.restart makes a later request's
prompt counts replace the earlier's, a zero too, so a later request that
reads nothing from the cache isn't told, or billed again, the earlier's
reads. Cache writes are last-only for the client as well: the later
request reads what the earlier wrote, so summed they count it twice.
ggbdpq's #5115 analysis named the translated route's case; no code of
theirs was used.
Tests (usage_last_prompt_test.go), on the reporter's numbers:
- TestClaudeOwnCallReplyTellsLastPrompt: bridge, told 6/576/577848,
billed 8/829/1154857; and a second request that missed the cache.
- TestClaudeTUILateMessagePromptNotTold: interactive mode's late message.
- TestRetriedReplyTellsLastPrompt: Gemini three reasoning-only replies
then an answer, Chat and Anthropic clients; ledger has all four.
- TestLaterTryZeroCacheReadNotTold: a continuation and a search round
whose later request reads 0 from the cache.
Tests that pinned the summed client numbers now pin the last request's
and check the billed sum (CachePartialUpdateMatrix, Probe1717 zero split,
cut stream retry, own WebSearch, search rounds, StreamCutUsage renamed
TestStreamCutUsageTellsLastTry).
Run (mote, Go 1.26.8, -tags nogui):
- The new tests on origin/main ca77a9ea8: red, the client told
8/829/1154857 (the report's numbers), prompt_tokens 2,000,000 for
four 500k tries.
- Broken by hand: restart dropped -> TestClaudeCutStreamGoesOnInRetry
and the cache-missed case red (reads billed twice); after() put back
to plus -> 12 tests red; restart on the translated tries dropped ->
TestLaterTryZeroCacheReadNotTold red (cache reads 200 billed, 111
told). The last one was green before that test was added.
- go test ./internal/gateway/ whole: ok except
TestClaudeInternalMessagesKeepCacheWrites, which pinned the sum and
was updated after; that test with the usage, cache, search,
continuation and TUI tests then ok (61.8s).
- go vet ./internal/gateway for darwin, linux and windows; gofmt clean.
- The full suite runs after the push (mote's suite lock held 3h+ by
another session).
Not tried: a real Claude Code account or the real TUI, the reporter's
session, a real Gemini/translated provider, Windows or Linux runs of
the package.
Reported-by: sleepy_shibe (Discord)
Keep reported Anthropic cache_creation splits, including pure-5m replies,
through API and subscription parsing and client output. Only positive
counters replace known usage. Completed sums retain all counters and
split presence.
Across continuation tries, search rounds and internal bridge messages,
partial updates add earlier total writes only with a positive total. A
split with counts and zero 1h keeps the current message's known 1h before
adding earlier messages' usage. A zero-filled or absent split keeps the
per-counter rule. Omit a zero-filled partial split with a positive total
from output; retain a reported zero-total split. Split presence and the
parsed update's own count evidence remain separate.
Yetone's zero-filled bridge probe and matching continuation/search probes
failed at 0c9298d with 10 instead of 15 1h writes. Fable's partial-5m and
pure-5m cases failed at e1de9bc7 in bridge, continuation and both search
modes with the same loss. All pass after the repair. The 12-case matrix
matches main's client totals and 1h counts and continuation/search ledger
counts. A renderer test rejects a fabricated 50/0 split on a sparse delta.
Verified under sandbox HOME, pinned Go caches and nogui: focused race,
Darwin vet ./..., Linux/Windows gateway vet, gofmt and diff checks. Archive
mutations removing current-message carry, bypassing each of three callers,
and restoring the fabricated split each fail behavior assertions. Restored
archive passes. Earlier zero-total, unreported-split and pure-5m cases stay
green. No live-provider check in this repair.
Review (yetone's session, landed from the PR's head c4cf224 and 90cdb2f):
the partial-cache probes go red when carryUpdate is reverted by hand,
and the 12-case matrix gives the same client totals and 1h counts as
main a15e57656. Rebased on 8b84dc902; gofmt clean; GOOS=darwin/linux/windows
go vet -tags nogui ./... clean.
Full suite: set -o pipefail; go test -timeout 45m ./... at the rebased head -> 65 packages ok, internal/gui red only on TestPanelJoinsFullScreenSpaces ("the panel shown doesn't take the keyboard"), red the same without this change, filed as #2450.