Skip to content

Conversation

@icecrasher321
Copy link
Collaborator

Summary

Copilot update cost route directly deducting the credit balance.

Should be in line with regular execution cost tracking.

Type of Change

  • Bug fix

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link

vercel bot commented Dec 11, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
docs Skipped Skipped Dec 11, 2025 2:24am

@icecrasher321 icecrasher321 merged commit 1d62ece into staging Dec 11, 2025
5 checks passed
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 11, 2025

Greptile Overview

Greptile Summary

This PR removes the real-time credit deduction logic from the copilot billing route, which contradicts the PR title and description that state copilot "should directly deduct credit balance."

Key Changes:

  • Removed deductFromCredits() call that was previously applying credits immediately
  • Changed from storing overflow (cost after credits) to storing full cost in user stats
  • Now matches the deferred credit application pattern used in regular execution tracking

Critical Issue:
The PR description claims this change makes copilot "directly deduct the credit balance" and aligns with regular execution cost tracking. However, the regular ExecutionLogger also does NOT deduct credits in real-time. Both systems now defer credit application until threshold billing occurs via checkAndBillOverageThreshold, which only applies credits when creating invoices for overage amounts that exceed the billing threshold.

This means credits are NOT deducted immediately for copilot usage - they're only applied during invoice generation. If the goal is real-time credit deduction, this PR moves in the opposite direction.

Confidence Score: 1/5

  • This PR contains a critical logic issue where the implementation contradicts the stated intent
  • The PR description states copilot should "directly deduct credit balance" but the code removes the only real-time credit deduction mechanism. The change defers credit application to threshold billing, which contradicts the PR's stated goal. This fundamental misalignment between intent and implementation requires clarification before merging.
  • apps/sim/app/api/billing/update-cost/route.ts requires immediate attention due to logic contradiction with PR description

Important Files Changed

File Analysis

Filename Score Overview
apps/sim/app/api/billing/update-cost/route.ts 1/5 Removed credit deduction logic from copilot billing - credits will no longer be applied to copilot costs

Sequence Diagram

sequenceDiagram
    participant Copilot
    participant UpdateCostAPI
    participant DB as Database (userStats)
    participant ThresholdBilling
    participant Credits as Credit Balance
    participant Stripe

    Copilot->>UpdateCostAPI: POST /api/billing/update-cost<br/>{userId, cost}
    
    UpdateCostAPI->>UpdateCostAPI: Validate request & auth
    
    UpdateCostAPI->>DB: Check user stats exists
    DB-->>UpdateCostAPI: User stats record
    
    Note over UpdateCostAPI,DB: REMOVED: deductFromCredits(userId, cost)
    Note over UpdateCostAPI,DB: Now stores FULL cost without credit deduction
    
    UpdateCostAPI->>DB: UPDATE userStats SET<br/>totalCost += cost<br/>currentPeriodCost += cost<br/>totalCopilotCost += cost<br/>currentPeriodCopilotCost += cost
    
    UpdateCostAPI->>ThresholdBilling: checkAndBillOverageThreshold(userId)
    
    ThresholdBilling->>DB: Get subscription & user stats
    ThresholdBilling->>ThresholdBilling: Calculate unbilled overage
    
    alt Overage >= threshold AND credits available
        ThresholdBilling->>Credits: Deduct credits from balance
        Credits-->>ThresholdBilling: Credits applied amount
        ThresholdBilling->>Stripe: Create invoice (overage - credits)
        Stripe-->>ThresholdBilling: Invoice created
        ThresholdBilling->>DB: Update billedOverageThisPeriod
    else Overage < threshold OR no credits
        Note over ThresholdBilling: No action taken<br/>Credits remain unchanged
    end
    
    ThresholdBilling-->>UpdateCostAPI: Complete
    UpdateCostAPI-->>Copilot: Success response
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Additional Comments (1)

  1. apps/sim/app/api/billing/update-cost/route.ts, line 94-103 (link)

    logic: This change removes credit deduction from copilot usage tracking, which contradicts both the PR description ("copilot should directly deduct credit balance") and the stated goal of aligning with regular execution cost tracking.

    The regular ExecutionLogger (line 548-563 in logger.ts) also does NOT call deductFromCredits - it stores the full cost and defers credit application until threshold billing via checkAndBillOverageThreshold.

    However, checkAndBillOverageThreshold only applies credits when creating invoices for overage (see threshold-billing.ts lines 164-186 and 406-427). This means:

    • Credits are NOT deducted in real-time for copilot usage
    • Credits are only applied when billing thresholds trigger invoice creation
    • The credit balance field won't reflect copilot usage until invoice time

    If the intent is to have copilot "directly deduct credit balance" as stated in the PR title and description, this PR actually does the opposite - it removes the only place where credits were being deducted in real-time.

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@waleedlatif1 waleedlatif1 deleted the fix/billing-copilot branch December 11, 2025 06:08
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