AI monetization
Build an AI product and charge for the tokens it burns.
class=kw>import larz.ai as ai
ai.enable(app)
app.ai.price("gpt-4o", input="$2.50/1M", output="$10/1M")
@app.ai_metered("gpt-4o", per_minute=20) class=com># rate-limit + require credit
@app.post("/api/chat")
class=kw>def chat(req):
reply, usage = call_llm(req.data["prompt"])
app.ai.charge(req, "gpt-4o", usage["in"], usage["out"]) class=com># bill the tokens
class=kw>return {"reply": reply}
Sub-cent accuracy
charge() accumulates cost in micro-dollars and debits whole cents from the
customer's credit only as they accrue โ tiny per-call costs aren't lost to rounding.
BYOK โ bring your own key
app.ai.set_byok(req.subject, "sk-customers-own-key") class=com># encrypted at rest
class=com># when a customer has a BYOK key, @app.ai_metered and charge() skip metering โ
class=com># they pay their provider directly. app.ai.has_byok(subject) / get_byok(subject).
Keys are encrypted with larz.crypto. Gate an
"unlimited AI" plan with a {"ai_unlimited": True} limit to skip the credit check.
An optional app.ai.proxy(...) fronts any OpenAI-compatible endpoint and auto-charges.
Guide: Monetize an AI API in Python.