Skip to main content

Overview

TalkifAI uses a credit-based billing system. Organizations purchase credits via Lemon Squeezy, which are consumed as voice sessions run. The billing system is a separate FastAPI microservice running on Google Cloud Run. Key features:
  • Pay-as-you-go credit system (1 credit = $1 = ~1 minute of voice conversation)
  • BYOC (Bring Your Own Carrier) — no telephony markup
  • Grace period of 5(callsallowedevenwithnegativebalanceupto5 (calls allowed even with negative balance up to -5)
  • Redis caching for fast quota checks (30s TTL)
  • Automatic cost calculation via background worker
  • Low balance email notifications

Architecture


Credit System

How Credits Work

Credits are:
  • Purchased via Lemon Squeezy (payment processor)
  • Stored per organization in the database
  • Consumed at session end (not during the call)
  • Cached in Redis (30-second TTL) for fast quota checks

Credit Flow

Grace Period Logic

TalkifAI uses a grace period system to prevent sudden service interruption: Purpose: The $5 grace period prevents sudden service interruption due to timing issues or unexpected usage spikes.

Pricing Model

BYOC Architecture (Bring Your Own Carrier)

Customers provide their own SIP credentials (Twilio, Telnyx, etc.) and pay carriers directly. Platform only charges for:
  • Base platform fee (infrastructure)
  • AI usage (STT/TTS/LLM if using platform keys)
No telephony markup — carrier costs are NOT charged by TalkifAI.

Cost Breakdown

The total cost of a session is calculated as:

Example Rates (via PricingRule table)

Example Calculation:
Actual rates are stored in the PricingRule database table and can be updated dynamically without code changes.

Payment Processing

TalkifAI uses Lemon Squeezy for payment processing:

Purchase Flow

Supported Events

Webhook payload includes:
  • custom_data.organization_id — Links payment to org
  • custom_data.user_id — User who made purchase
  • data.attributes.total — Amount in cents ($19.99 = 1999)

Credit Packages

Credits are purchased as one-time payments (no subscriptions):
Credits never expire and remain in the organization’s balance indefinitely.

Session Tracking

Start Session

Response:

End Session

Response:
Costs are calculated asynchronously by the background worker (usually within 10-20 seconds).

Concurrency Safety

Optimistic Locking

To prevent race conditions when multiple calls end simultaneously:

Redis Caching

Cache Flow

Cache Invalidation

Cache is invalidated when:
  • Credits are consumed (session ends)
  • Credits are added (payment successful)
  • Manual balance refresh requested
Performance Impact:
  • Without cache: 100 requests = 100 DB queries
  • With cache: 100 requests = ~3-4 DB queries (96% reduction)

Notifications

The billing service sends email notifications for: Notifications are sent to the organization owner’s email address.

API Reference

Check Credit Quota

Response:

Consume Credits

Response:

Get Balance

Response:

Get Transaction History

Response:

Troubleshooting

Check:
  1. Current balance: GET /billing/credits/balance/{org_id}
  2. Grace period status (balance may be negative but still allowed)
  3. Recent transactions for unexpected charges
Fix: Add credits via Lemon Squeezy checkout.
Check:
  1. Lemon Squeezy order status (check email receipt)
  2. Webhook logs: GET /lemon-squeezy/webhook-events
  3. LemonSqueezyWebhookEvent.processed flag in database
Fix: Contact support with order ID if webhook failed.
Check:
  1. Transaction history for all debits/credits
  2. Recent session costs (may still be calculating)
  3. Redis cache staleness (wait 30s or force refresh)
Fix: Force cache refresh: DELETE /billing/credits/cache/{org_id}
Expected behavior: Costs are calculated within 10-20 seconds by the background worker.Check:
  1. Worker logs for errors
  2. BillingSession.sessionStatus = “completed”
  3. BillingSession.costsCalculatedAt timestamp
Fix: Manual cost calculation via admin tools if worker stuck.