AI monetisation

How to charge credits for AI features

Price a named AI tool on the server. Gemmein reserves the credits before the call, runs it on your provider key, and settles at what the provider actually used. If the provider fails before it answers, the credits are refunded. The browser never sees the price, the model, or the prompt.

01 / OVERVIEW

Price the tool once. Every call spends credits.

A credit is the unit your customer spends on one AI call. You define the operation once as a named tool and give it a price. From then on, every call to that tool spends that many credits, recorded on the customer's ledger. You never write the metering, and the price is not something the browser can change.

Define and price the tool on the server, then call it by name from the app. The app sends a name and inputs. It never sends a prompt, a model, or a price.
02 / DEFINE THE TOOL

Define and price it on the server.

A tool is a file in your project. You write the implementation: provider, model, instructions, template, and inputs. You set its commerce, the price and access gate, in the dashboard. Credits run from 1 to 10,000 per tool.

gemmein/ai/tools/deep-research.jsonTool definition
{
  "label": "Deep Research",
  "provider": "openai",
  "model": "gpt-4o",
  "credits": 20,
  "requires": "access:pro-max",
  "instructions": "You are a careful research assistant. Answer with sources.",
  "promptTemplate": "Research this question for a {{audience}} reader:\n\n{{question}}",
  "inputs": [
    { "name": "question", "type": "text", "required": true, "maxLength": 2000 },
    { "name": "audience", "type": "text" }
  ],
  "bounds": { "maxOutputTokens": 4000 }
}

Carry it with npx gemmein sync (--live into production, with a sync key). The file owns the implementation. The dashboard owns the commerce: credits, requires, and whether the tool is enabled.

03 / BILLING

Credits are reserved, then settled at real usage.

Your app calls the tool by name. Gemmein reserves the price, runs the request on your provider key with the pinned model, and settles the charge when the answer ends.

Your app calls a named toolJavaScript / Expo
// A name and inputs. The server composes the rest.
const answer = await g.ai.runText("deep-research", {
  question: "How can I improve client onboarding?",
  audience: "beginner"
});

// The customer's own balance, in the browser, when signed in.
const { balance, reserved, expiring } = await g.credits.balance();

Priced per call

The price is spent before the call and refunded in full if the provider fails before it answers. A call that dies mid-stream, or one the caller abandons after the first byte, is not refunded.

Priced per token

A ceiling is reserved before the call, covering the request size plus the output cap. The charge settles at the provider's reported usage, up to that ceiling, when the stream ends cleanly.

Every call is recorded: who ran it, the tool, the tokens, the credits, and the outcome. A customer who is short on credits sees the price and their balance: "Deep Research costs 20 credits. You have 7."
04 / TOP-UPS

Top-ups are granted on the payment webhook.

Customers buy more credits with a pack. The credits are granted when the signed payment webhook arrives, not when the browser returns from checkout.

This is the step AI-written checkout code usually gets wrong: it grants access on the success redirect. A redirect can be faked by anyone who knows the URL. Gate the top-up on the payment's signed webhook, the record that Stripe signs, and the grant lands only on a real payment. In Gemmein that path is built in. The signed event is verified and written to the customer's record, and the credit grant follows it, with no founder code holding it together.
05 / FOR YOUR CODING AGENT

Give your agent the full task.

Building with Cursor, Claude, Bolt, or Lovable? Paste this so your agent works against the real contract instead of guessing.

Prompt for your coding agentCopy and paste

Read https://docs.gemmein.com/llms.txt and https://docs.gemmein.com/ai. I want to charge credits for an AI feature. Show me how to define a priced AI tool on the server (a tool file with provider, model, instructions and inputs), set its credit price and access gate, and call it by name from my app. Explain how credits are reserved and settled, and how a customer buys more credits so the grant lands on the signed payment webhook, not the checkout redirect.

06 / FAQ

Frequently asked questions.

What happens to the credits if the provider fails?

A per-call price is spent before the call and refunded if the provider fails before answering. A per-token price reserves a ceiling before the call and settles at the provider's reported usage, up to that ceiling, when the answer ends cleanly. A stream that does not end cleanly, or a call abandoned mid-answer, costs the ceiling.

Do the credits expire?

Credits sit on a grant, such as a credit pack. Credit packs never expire.

Can the browser set the price or pick the model?

No. The price, the model, and the access gate live on the server. The app sends a tool name and inputs. It never sends a price, a model, or a prompt. Raw provider calls from the browser are off by default.

How do customers get more credits?

They buy a pack. The grant is added when the signed payment webhook arrives, not on the checkout redirect. A redirect can be faked; the webhook cannot.

Can I meter something that is not an AI call?

Yes. Spend credits from your own server for anything you want to meter that is not an AI tool call.

07 / RELATED

Related.

AI tools and credits on the product page · Where your OpenAI key should live · Gemmein vs Supabase for AI apps · AI docs

See if Gemmein fits your app.

Point your coding agent at docs.gemmein.com/llms.txt and ask it to confirm the fit before it builds.

Read the builder guide