The Cursor plugin collects a tool step's late usage event with the
conversation's next step. It used to add it to that step's prompt_tokens,
which agents read as how full their context is: pi saw 366940 tokens
(181270 + 185670) on a 300k window where 185670 were in use, and compacted.
The plugin now reports it apart, as usage.magpie_deferred (yetone/magpie#1053).
The gateway parses the field into Usage.Deferred and adds it only where it
bills: the usage records (6 sites in serve), the OTel attempt span and route
pricing, as a tier before the request's own, so promptCounted still reads the
request's own prompt. What a client is told (Usage.chat, the Anthropic
encoding) stays the request's own. A reply without the field is handled as
before. docs/subsystems/provider-plugins.md describes the field.
Verified: go test -tags nogui ./... (50 packages ok, sandbox HOME, caches
pinned); gofmt; go vet on darwin, linux (nogui) and windows; go build on the
same three. The new tests were broken by hand and each went red: billed()
ignoring Deferred, cUsage dropping the field, routeUsage without the tier,
Usage.add forgetting it, the decoder ignoring a usage with only a deferred
part, the usage records and the OTel span not using billed(). Not tried: a
real Cursor account on this checkout; the plugin side was run against real
Cursor (composer-2.5, forced late events), where the deferred totals matched
the dashboard's events token for token.
Picked from geoqiao/magpie cursor-deferred-usage (595d6384c), as PRs here
are collaborators-only. Rebased over the service-tier logging (#1784),
keeping its TierAsked/TierSent/TierServed beside billed() at the three
usage.Record sites, and Usage's Tier beside Deferred. Run on this tree:
go test -tags nogui -timeout 40m ./internal/gateway and ./internal/provider
(sandbox HOME); gofmt -l; go vet darwin, GOOS=linux -tags nogui,
GOOS=windows. billed() forced to ignore Deferred: 3 tests and 5 subtests
of deferred_usage_test.go go red.