Repository navigation
fix: count every model call once, with all four token parts and per-model prices - #166
Merged
Merged
Conversation
… prices, a token limit that can count cache tokens, and fewer cache rewrites
nambok
force-pushed
the
feat/usage-accounting
branch
from
October 19, 2026 02:19
88a78a8 to
731c2b1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Every model call is now counted once, with all four token parts, priced at the model that was actually used.
common::usagetakes the team, the feature, the model and the four token counts (new input, cache read, cache write 5m/1h, output) and adds them in one transaction to the team's month, all-time and day counters, with per-model and per-feature breakdowns and a per-model cost. The worker's Anthropic client cannot be built without a usage meter, so every worker call goes through it. Plan chat (API and streaming), memory extraction and the log analyzer record through the same fields.services/common/model_prices.json, from Anthropic's pricing page) with input, output, cache read and 5-minute and 1-hour cache write rates per model. A model missing from the table is logged once and costed at the most expensive rate, never zero. Run costs are now the sum of each call at its own model's price instead of one flat rate.counts_cache = trueon the team'sSETTINGS#TOKEN_LIMITitem, or for everyone withTOKEN_LIMIT_COUNTS_CACHE=true.GET /api/usagereturns the total, the four parts, the cache hit rate, the per-feature split and the number the limit compares. No money in it. Saving the limit no longer wipes the item's other fields.usage_driftwhen they differ by more than 2%. It stays off until thecoderhelm/<stage>/anthropic-admin-keyssecret exists.scripts/usage_backfill.pyrecomputes this month's and last month's counters. It prints by default and writes only with--apply.Changes
services/common/src/usage.rs,services/common/model_prices.json: accounting, prices, limit math, the exactly-once source check.AnthropicClient, per-modelTokenUsage, feature scope per queue message, team agent asks underagent_ask,passes/usage_reconcile.rs, per-pass cost on traces, cost kept on checkpoints, per-run cost kept after feedback rounds./api/usagecount all four parts behind the flag; plan chat counts per call.usage.py(the same fields and price table; a Rust test keeps the two tables identical).usage_reconcileschedule, worker read access to the admin-key secret, log analyzer write access to analytics, gateway write access to analytics. The gateway was read-only before, so non-streaming plan chat usage had never been saved.Verification
cargo fmt --all -- --check,cargo clippy --workspace --all-targets -- -D warnings,cargo test --workspace(220 + 108 + 246 passed),tsc --noEmitin infra,python3 -m unittest discover -s lambda/tests(14 passed).