Skip to content

feat(usage): add api_kind field to microdollar_usage_metadata#1126

Merged
chrarnoldus merged 3 commits intomainfrom
feat/api-kind-usage-metadata
Mar 16, 2026
Merged

feat(usage): add api_kind field to microdollar_usage_metadata#1126
chrarnoldus merged 3 commits intomainfrom
feat/api-kind-usage-metadata

Conversation

@kilo-code-bot
Copy link
Contributor

@kilo-code-bot kilo-code-bot bot commented Mar 16, 2026

Summary

Adds an api_kind field (e.g. chat_completions, responses, fim_completions, embeddings, messages) to microdollar_usage_metadata, using the same normalized lookup-table pattern as editor_name.

  • New api_kind lookup table with a unique index and serial PK
  • New api_kind_id FK column on microdollar_usage_metadata
  • microdollar_usage_view extended with api_kind via a LEFT JOIN
  • UsageMetaData type updated with api_kind: string | null
  • extractUsageContextInfo now forwards api_kind from MicrodollarUsageContext (already present there)
  • insertUsageAndMetadataWithBalanceUpdate CTE extended with createUpsertCTE for api_kind
  • Benchmark script and admin dev-insert route updated to satisfy the updated type
  • No migration included — will be auto-generated

Verification

  • pnpm typecheck passes (all packages)
  • pnpm format:check passes

Visual Changes

N/A

Reviewer Notes

api_kind was already present on MicrodollarUsageContext (used for branching logic) but was never persisted to the database. This PR closes that gap using the established normalized storage pattern.

@kilo-code-bot
Copy link
Contributor Author

kilo-code-bot bot commented Mar 16, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (9 files)
  • packages/db/src/migrations/0051_chemical_ultimatum.sql
  • packages/db/src/migrations/meta/0051_snapshot.json
  • packages/db/src/migrations/meta/_journal.json
  • packages/db/src/schema.ts
  • src/app/admin/api/users/[id]/dev/insert-usage-record/route.ts
  • src/lib/processUsage.ts
  • src/lib/processUsage.types.ts
  • src/scripts/usage/benchmark-insert-usage.ts
  • src/tests/helpers/microdollar-usage.helper.ts

Reviewed by gpt-5.4-20260305 · 2,154,116 tokens

'api_kind',
{
api_kind_id: serial().notNull().primaryKey(),
api_kind: text().notNull(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a known enum, we can express it via typescript:

Suggested change
api_kind: text().notNull(),
api_kind: text().$type<'chat_completions' | 'responses' | 'fim_completions' | 'embeddings' | 'messages'>().notNull(),

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright, I'll do it in a follow-up because I'll probably need to move the type

@chrarnoldus chrarnoldus force-pushed the feat/api-kind-usage-metadata branch from b3425cb to 4fdb052 Compare March 16, 2026 20:48
@chrarnoldus chrarnoldus enabled auto-merge March 16, 2026 20:49
@chrarnoldus chrarnoldus merged commit dc09e85 into main Mar 16, 2026
18 checks passed
@chrarnoldus chrarnoldus deleted the feat/api-kind-usage-metadata branch March 16, 2026 20:53
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.

3 participants