Skip to content

Report the price back onto the bus - #21

Merged
fylorn merged 1 commit into
mainfrom
feat/priced-event
Sep 16, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/priced-event

Conversation

@fylorn

@fylorn fylorn commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Why

The store is the only place in the chain that knows what a request cost: the gateway knows how many tokens went by, and the per-token rate is looked up here when the row is written. So the price is known a moment after the request finishes — and nothing announced it.

Anything that wants to display a figure has to go back and query for it. In the desktop client that means re-reading the last two hundred rows a couple of seconds after every batch of requests, a mechanism that exists purely because this one number is missing from the stream. A live chart cannot use it at all.

What

Event::RequestPriced { id, cost_micros, cost_estimated, cache_saved_micros, at_ms }.

A separate event rather than a field on RequestFinished, deliberately: putting it there would make the gateway depend on the price table, filing "forwarding" under "billing". The recorder takes an optional bus handle (Recorder::reporting_to) and reports what it computed; twcore does the wiring, being the one place that sees both sides. The recorder ignores the variant it emits, so there is no loop.

All three values stay Option. Upstream returned no usage, the model is absent from the table, the provider bills by subscription — none of those is zero, and a zero gets drawn as "free".

Gates

cargo fmt --all -- --check, cargo clippy --workspace --all-targets -- -D warnings, cargo test --workspace (61 binaries green, 2 new), ./scripts/smoke.sh (46 passed, 0 failed).

🤖 Generated with Claude Code

The store is the only place in the chain that knows what a request
cost — the gateway knows how many tokens went by, the per-token rate is
looked up here when the row is written. So the price arrives a moment
after the request finishes, and nothing announced it.

Anything wanting to show a figure had to go back and query for it after
the fact. In the desktop client that is a re-read of the last two
hundred rows a couple of seconds after every batch of requests, kept
alive purely because this one number was missing from the stream. A
live chart cannot use that at all.

`RequestPriced` is a separate event rather than a field on
`RequestFinished` on purpose: putting it there would make the gateway
depend on the price table, which files "forwarding" under "billing".
The recorder gets an optional bus handle and reports what it computed;
twcore does the wiring, being the one place that sees both sides.

All three values stay optional. Upstream returned no usage, the model
is absent from the table, the provider bills by subscription — none of
those is zero, and a zero would be drawn as "free".

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Thanks for the PR. One thing before review: this targets main, and it should target dev.

main is a release-only line — branch-protected, linear history, and every commit on it is a tagged release snapshot. Routine work lands on dev and reaches main with the next release. GitHub pre-filled main here because it is the default branch, so this is our defaults being unhelpful rather than anything you did wrong.

No need to close and reopen: click Edit next to the PR title and change the base branch to dev. The commits and the discussion carry over.

See CONTRIBUTING.md and CONTRIBUTING.md for the branch contract.

@fylorn
fylorn merged commit 512ddb9 into main Sep 16, 2026
2 checks passed
@fylorn
fylorn deleted the feat/priced-event branch September 16, 2026 05:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant