DOCS LLMs

Usage credits

RailsFast ships with a fully working usage credits system, so you can keep track of how many credits each of your users has, and allow them to spend credits on your app.

If you don't need this in your app, you can just ignore it! There's nothing else to do.

If you do want usage credits in your app, everything is ready to go! You just need to define how your users get credits and how much each credit-consuming operation costs in usage_credits.rb.

For example, you define how your users get credits like this:

stripe_config = Rails.configuration.x.railsfast.dig(:stripe, :plans) || {}

subscription_plan :basic_plan do
  stripe_price    month: stripe_config.dig(:basic_plan, :monthly), year: stripe_config.dig(:basic_plan, :yearly)
  gives           10_000.credits.every(:month)
end

And you define the cost of operations like this:

operation :process_image do
  costs 10.credits + 1.credit_per(:mb)
end

And then you can do this anywhere in your app:

@user.spend_credits_on(:process_image) do
  # Perform the actual operation here.
  # No credits will be spent if this block fails.
end

Finally, to make everything work in both development and production, uncomment the credit fulfillment jobs from config/recurring.yml:

# Uncomment this so usage_credits refills credits in production:
refill_credits:
  class: UsageCredits::FulfillmentJob
  queue: default
  schedule: every 5 minutes

RailsFast uses the usage_credits gem for this, please refer to the gem README to learn everything you can do and how to fully define credit-consuming operations and credit-giving plans.