Skip to content

Calculate and deduct credit usage from token consumption #98

@afrinxnahar

Description

@afrinxnahar

🎯 Problem Statement
Credits are not currently properly calculated/charged based on actual token usage (recently implemented), causing inaccurate accounting for usage-based features.

💡 Proposed Solution

  • Define 1 credit = 1000 tokens.
  • Instrument server-side request flows to capture token usage.
  • Calculate credits_used = ceil(tokens_used / tokens_per_credit).
  • Deduct credits_used from the user’s balance atomically.
  • Store usage records in credit_usage table.
  • Pre-check: if insufficient credits, reject request.

📊 Impact Assessment
User Impact: High (accurate billing).
Development Effort: Medium.
Priority: High.

🧪 Acceptance Criteria

  • Token usage captured for all LLM requests.
  • Credits deducted based on conversion rule.
  • Users see per-request credit deduction on each feature before generating response
  • Safe behavior when credits insufficient.

🔗 Related Features
Billing, invoices, credit awarding/referrals, usage dashboard.

📚 Additional Context
Ensure the model's API returns token counts or compute tokens manually.

📋 Checklist

  • Token capture for all LLM calls
  • Credit conversion and deduction logic
  • DB table for usage records
  • UX for insufficient credits
  • Tests for calculations and concurrency

Metadata

Metadata

Assignees

No one assigned

    Labels

    high priorityTop most priority based task

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions