Skip to content

Add mode, auto model and market cost to microdollar usage#681

Open
chrarnoldus wants to merge 7 commits intomainfrom
session/agent_ca6302aa-4424-40a2-97db-741dc107c41d
Open

Add mode, auto model and market cost to microdollar usage#681
chrarnoldus wants to merge 7 commits intomainfrom
session/agent_ca6302aa-4424-40a2-97db-741dc107c41d

Conversation

@chrarnoldus
Copy link
Contributor

No description provided.

@kilo-code-bot
Copy link
Contributor

kilo-code-bot bot commented Feb 27, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Overview

This PR adds three new fields to the usage tracking pipeline:

  • mode — the client mode header (x-kilocode-mode), now stored for all requests (not just auto-model requests)
  • auto_model — the original kilo/auto* model ID before resolution
  • market_cost — the real cost before free/BYOK/promo zeroing

The implementation is clean and consistent:

  • New mode and auto_model lookup tables follow the existing normalized pattern (with createUpsertCTE)
  • The modeHeader extraction was correctly refactored to use extractHeaderAndLimitLength (500 char limit + trim), addressing the previous review comment about length limits
  • The migration file correctly creates the tables, adds FK columns, and recreates the view
  • All callers of MicrodollarUsageContext have been updated with the new fields
  • Test helpers and benchmark scripts are updated
  • The market_cost is set at the right point in processTokenData — after abuse cost reporting but before free/BYOK/promo zeroing

Previous review comments about missing length limits and missing migration are now addressed by this revision.

Files Reviewed (11 files)
  • packages/db/src/migrations/0034_brave_amphibian.sql - Migration: new tables, columns, view
  • packages/db/src/migrations/meta/0034_snapshot.json - Generated snapshot
  • packages/db/src/migrations/meta/_journal.json - Journal entry
  • packages/db/src/schema.ts - Schema: new tables, view columns, FK references
  • src/app/api/openrouter/[...path]/route.ts - Mode/auto_model extraction and storage
  • src/app/api/dev/consume-credits/route.ts - New fields on usage context
  • src/app/api/fim/completions/route.ts - New fields on usage context
  • src/app/admin/api/users/[id]/dev/insert-usage-record/route.ts - New fields on metadata
  • src/lib/processUsage.ts - Core: types, extraction, DB insert, market_cost logic
  • src/lib/processUsage.test.ts - Test updates
  • src/tests/helpers/microdollar-usage.helper.ts - Test helper updates
  • src/scripts/usage/benchmark-insert-usage.ts - Benchmark updates

@chrarnoldus chrarnoldus changed the title Store mode and auto model in microdollar usage Add mode, auto model and market cost to microdollar usage Feb 28, 2026
kilo-code-bot bot and others added 7 commits February 28, 2026 18:30
- add mode and kilo_auto_model in MicrodollarUsageContext
- plumb fields through insertUsageRecord and metadata
- extend microdollar_usage_metadata and microdollar_usage_view schemas
- propagate through admin/dev routes and API routes (consume-credits, fim completions, openrouter)
- update tests, helpers, and benchmarks to include new fields with null defaults
- ensure backward compatibility by allowing null for new columns
update database schema and views to use auto_model
instead of kilo_auto_model
align metadata fields accordingly

propagate changes through API routes, processing logic, tests, and
scripts to reference auto_model
introduce new mode and auto_model tables with primary keys and
unique constraints on their value fields
update microdollar_usage_metadata to reference mode_id and
auto_model_id instead of text fields
update microdollar_usage_view to join new tables and expose
mode and auto_model values via aliases md and am
update processUsage to upsert mode and auto_model and store IDs
in mode_id and auto_model_id via CTEs
…ader

Use shared helper to extract and bound the header value, replacing direct header access and trim
Stores the real cost before free/BYOK/promo zeroing, so we can track
the actual market value of requests even when the user isn't charged.
@chrarnoldus chrarnoldus force-pushed the session/agent_ca6302aa-4424-40a2-97db-741dc107c41d branch from 9a1a666 to c6a34ce Compare February 28, 2026 17:31
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.

2 participants