Skip to content

fix/database-issue#307

Draft
MmagdyHafezZ wants to merge 2 commits intomasterfrom
fix/database-issue
Draft

fix/database-issue#307
MmagdyHafezZ wants to merge 2 commits intomasterfrom
fix/database-issue

Conversation

@MmagdyHafezZ
Copy link
Copy Markdown
Collaborator

@MmagdyHafezZ MmagdyHafezZ commented Apr 15, 2026

PR Description

Overview:

This PR fixes AI usage tracking issues caused by growing token counters by migrating AIUsage.tokensIn, tokensOut, and usageCount from INT to BIGINT. It also updates usage tracking writes to persist bigint-safe values, hardens prompt processing so successful provider responses are not blocked by usage-tracking failures, and adds targeted unit tests for the new counter-conversion behavior.

Type of Issue:

  • Feature (feat): New functionality or feature added.
  • Bug Fix (bug): Issue or bug resolved.
  • Chore (chore): Maintenance, refactoring, or non-functional changes.
  • Documentation Update (doc): Documentation improvements or additions.

Change Type:

  • Major: Significant changes that introduce new features, large refactoring, or breaking changes. Requires thorough review and testing.
  • Minor: Small to medium changes, such as adding new functionality that is backward-compatible or minor refactoring. Moderate review needed.
  • Patch: Bug fixes, small tweaks, or documentation updates. Light review is sufficient.

Test Coverage

  • Unit tests updated
  • Manual verification done

Evidence:

  • Added unit tests for PromptProcessorService, UsageTrackerService, and ai-usage-counter.util.
  • Covers bigint persistence, bigint-to-number conversion, and prompt-processing behavior when usage tracking fails after a successful provider response.

Impact / Risk

  • Touches Prisma schema and adds a DB migration for AIUsage counters.
  • Updates admin-side AI usage/cost reporting to handle bigint-backed counters.
  • Changes prompt-processing behavior so usage-tracking failures are logged instead of failing the user request.

Rollback

  • Revert the application change with git revert <sha>.
  • If database rollback is required, only convert AIUsage counters back to INT after confirming all existing values still fit within the 32-bit integer range.

Reviewer Focus

  • apps/api/prisma/migrations/20260414101500_ai_usage_bigint_counters/migration.sql
  • apps/api/src/api/llm/core/services/usage-tracking.service.ts
  • apps/api/src/api/llm/core/services/prompt-processor.service.ts
  • apps/api/src/api/admin/admin.service.ts

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