Skip to content

Sanitize token usage, charge remaining credits, and add DB/accounting error handling in generateAnswer#56

Merged
Abdulmuiz44 merged 2 commits into
codex/ui-redesignfrom
codex/fix-upgrade-modal-for-credit-check-pi5paz
Apr 6, 2026
Merged

Sanitize token usage, charge remaining credits, and add DB/accounting error handling in generateAnswer#56
Abdulmuiz44 merged 2 commits into
codex/ui-redesignfrom
codex/fix-upgrade-modal-for-credit-check-pi5paz

Conversation

@Abdulmuiz44
Copy link
Copy Markdown
Collaborator

Motivation

  • Prevent crashes or integer overflows from malformed token usage values and avoid failing requests when users have partial credits left.
  • Ensure we still save generated responses even if the user doesn't have enough credits to cover the full token cost.
  • Surface friendly error messages to the client instead of throwing on DB failures and improve server-side logging for debugging.

Description

  • Sanitize and clamp generationResult.usage.totalTokens into a finite integer and compute tokenCost with a safe 32-bit upper bound.
  • Compute creditsToCharge as min(tokenCost, creditsRemaining) and remove the early rejection that returned an error when tokenCost exceeded remaining credits.
  • Use creditsToCharge when calling incrementUserCredits and retain the retry loop, logging a usageAccountingWarning if accounting ultimately fails.
  • Replace thrown errors on DB update/insert failures with console.error logging and a friendly error response so generated content is not lost.

Testing

  • Ran the project's unit test suite with pnpm test and all tests passed.
  • Ran a TypeScript type check/build with pnpm build and the build completed successfully.

Codex Task

@Abdulmuiz44 Abdulmuiz44 merged commit 796d810 into codex/ui-redesign Apr 6, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant