Documentation

Guides

Custom User Billing

Implementing billing for your application's end-users when using A4F.

Introduction

A4F provides a unified API for developers to access various LLMs. While A4F handles billing for your (the developer's) usage of our platform based on your chosen A4F plan, you might want to bill your own end-users for their consumption of AI features within your application.

This guide outlines strategies and considerations for implementing a custom billing system for your end-users on top of A4F.

A4F's Billing Model

It's important to understand that A4F bills you, the developer or organization, for the API requests made using your A4F API key. This is based on token consumption, rate limits, and features associated with your A4F subscription tier.

A4F does not directly provide a mechanism to bill your application's individual end-users. You are responsible for implementing this layer if required.

Strategies for End-User Billing

To bill your end-users, you'll typically need to:

  1. Track Usage Per User: Monitor how much each of your end-users consumes through A4F.
  2. Define Your Pricing Model: Decide how you want to charge your users (e.g., per token, per request, subscription tiers, credit packs).
  3. Integrate a Billing System: Use a third-party payment processor or subscription management platform (e.g., Stripe, Chargebee, Paddle) to handle payments and subscriptions for your users.

Usage Tracking for Your Users

This is a critical step. Here are a few approaches:

  • Your Backend as a Proxy:
    • Route all end-user requests through your own backend server.
    • Your backend authenticates your user, then makes the request to A4F using your A4F API key.
    • Before or after the A4F call, log the request details (user ID, model, prompt tokens, completion tokens, timestamp) in your own database.
    • The usage object in A4F's response (compatible with OpenAI's) provides token counts.
    • You can pass your end-user's unique ID in the user parameter of the A4F API request. While A4F forwards this for potential abuse monitoring by providers, it's primarily for your own logging if providers expose it back or if A4F uses it for detailed dashboard breakdowns in the future. A4F's `/v1/usage` endpoint currently provides aggregate usage for *your* API key.
  • User-Specific A4F API Keys (Advanced & Potentially Complex):
    • If A4F's platform evolves to allow programmatic creation and management of multiple API keys under your account (this is not the current state of one key per account), you could consider assigning a unique A4F sub-key to each of your end-users or user groups.
    • This would allow A4F's `/v1/usage` endpoint to potentially provide a direct breakdown if it supports per-key statistics. However, managing a large number of keys can be complex. (Note: This is speculative based on future A4F enhancements; currently, it's one key per account.)

The first approach (backend proxy and self-logging) is generally more common and flexible.

from flask import Flask, request, jsonify
# Assume a4f_client is your configured OpenAI client pointing to A4F
# from my_a4f_client import a4f_client
# Assume db is your database utility
# from my_database_utils import record_user_usage, get_user_api_key
app = Flask(__name__)
@app.route('/api/my-app/chat', methods=['POST'])
def handle_chat_request():
# 1. Authenticate your end-user (e.g., via JWT in request.headers)
# user_id = authenticate_my_user(request.headers.get('Authorization'))
# if not user_id:
# return jsonify({"error": "Unauthorized"}), 401
user_id = "example_user_123" # Placeholder
user_request_data = request.json
# 2. (Optional) Retrieve user-specific A4F key or use a shared one
# a4f_key_for_user = get_user_api_key(user_id) # If you map users to keys
try:
# 3. Make the call to A4F
# For this example, assume a4f_client is globally configured or per request
# completion = a4f_client.chat.completions.create(
# model=user_request_data.get("model", "provider-1/default-model"),
# messages=user_request_data.get("messages"),
# user=user_id # Pass your end-user's ID to A4F if supported for logging
# )
# Mocked response for conceptual example
completion = {
"choices": [{"message": {"content": "This is a response from A4F."}}],
"usage": {"prompt_tokens": 50, "completion_tokens": 100, "total_tokens": 150}
}
# 4. Record usage for your end-user in your database
# record_user_usage(
# user_id=user_id,
# model_requested=user_request_data.get("model"),
# prompt_tokens=completion.usage['prompt_tokens'],
# completion_tokens=completion.usage['completion_tokens'],
# total_tokens=completion.usage['total_tokens'],
# # Potentially also cost if you calculate it here based on A4F rates
# )
# return jsonify(completion.choices[0].message.content)
return jsonify({"response": completion["choices"][0]["message"]["content"]})
except Exception as e:
# Log error and handle appropriately
# print(f"Error processing request for user {user_id}: {e}")
# return jsonify({"error": "Failed to process request"}), 500
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
# This is conceptual. Do not run Flask dev server in production.
# app.run(debug=True)
pass

Integrating with Billing Systems

Once you have usage data per user, you can integrate with a billing provider:

  • Stripe: Excellent for metered billing (pay-as-you-go based on usage), subscriptions, and invoicing.
  • Chargebee, Paddle, Recurly: Robust subscription management platforms.
  • Custom Solution: For very specific needs, though this adds significant development overhead.

Your backend would periodically (e.g., end of billing cycle) or in real-time (for pre-paid credits) push usage data to your chosen billing system to generate invoices or deduct from credit balances.

Pricing Your Service

When setting prices for your end-users, consider:

  • Your A4F Costs: The rates A4F charges you for the models your users consume.
  • Markup/Margin: Your desired profit margin.
  • Value Provided: The unique value your application adds on top of the raw LLM capabilities.
  • Market Rates: What competitors or similar services charge.
  • Simplicity: Complex pricing can deter users. Consider flat subscriptions, tiered usage, or simple per-token/per-request rates.

Key Considerations

  • Transparency: Clearly communicate your pricing and usage policies to your end-users.
  • Usage Limits & Quotas: Implement your own limits for your users to prevent abuse and manage your A4F costs.
  • Real-time Monitoring: Provide your users with a way to see their own usage and remaining credits/quota.
  • Security: Ensure your usage tracking and billing integration are secure.
  • A4F API Key Security: Your primary A4F API key should remain confidential on your backend. Do not expose it directly to your end-user's client-side applications.

Was this page helpful?