For AI agents
Setup prompt for AI coding agents
Paste this into Cursor, Claude, Copilot Chat, or any coding agent. Fill in the product blanks (or let the agent ask). It will design your rules graph, wire the SDK, and connect Stripe — including how billing periods work.
How to use it
- Create a UsageGate workspace and an API key.
- Copy the prompt below into a new agent chat in your product repo.
- Fill the “Product context” section, or leave blanks and answer the agent’s questions.
- Review the proposed rules, then save them in Access rules or via
putRules. - Let the agent add
canAccess/consumeand wire Stripe. Keep the API key server-side.
Copy this prompt
usagegate-ai-setup.md
You are helping me set up UsageGate for my SaaS product.
## What UsageGate is
UsageGate is a usage-billing / feature-gating control plane. I (the SaaS founder) define plans → features → limits. My backend asks UsageGate whether *my* end users can access a feature and how many credits they have left. End users never log into UsageGate.
Stripe is required: I keep Checkout in *my* Stripe account. UsageGate listens to webhooks, refills balances from subscription metadata, and sets resetAt from the subscription's current_period_end. There is no calendar cron that auto-tops balances — the period clock is Stripe's.
## Goals for this session
1. Design a Plan → Feature → Limit rules graph for my product
2. Choose stable feature keys my code will call forever
3. Wire GateClient into my backend (canAccess / consume / grant)
4. Connect Stripe webhooks (required) so paid renewals refill + set period end
5. Leave me with env vars, code snippets, and a smoke-test checklist
## Product context (fill this in before you start)
- Product name:
- What users do that costs credits or should be gated:
- Free tier limits:
- Paid tier(s) and limits:
- Stripe billing interval (monthly / yearly / other):
- End-user id field in my app (e.g. session.user.id):
- Stack (Next.js / Express / other):
- UsageGate base URL (local default http://127.0.0.1:43123):
- I already have an API key? (yes/no):
- Stripe webhook secret ready? (yes/no):
## Rules you must follow
- Never invent a second login system. userId = my existing user id.
- Feature keys: snake_case, stable (ai_credits, export_pdf, seats).
- Prefer METERED for burnable credits, BOOLEAN for on/off, NUMERIC_LIMIT for caps (seats/projects).
- canAccess / consume default fail-open — note that when discussing reliability.
- grant() is absolute set (not increment). Use grant for free signup / trials / admin overrides only.
- Paid refills and period boundaries MUST go through Stripe webhooks — do not treat Stripe as optional.
- Explain resetAt: after current_period_end, effective balance is 0 until the next invoice.paid sets a new balance + new resetAt. Leftover credits do not roll over.
- Do not put the API key in frontend code. Server-side only.
- Package: @usagegate0/sdk → new GateClient(process.env.USAGEGATE_KEY!, { baseUrl: process.env.USAGEGATE_API_BASE_URL })
## Suggested workflow
1. Ask me the product-context blanks if empty.
2. Propose a rules graph (nodes + edges) I can paste into dashboard or gate.putRules().
3. Show grant on signup for free-tier balances (optional resetAt for trials).
4. Show the gate on my expensive code path (canAccess → work → consume).
5. Stripe (required): webhook URL POST {baseUrl}/api/webhooks/stripe, save whsec via dashboard or gate.configureStripe(), subscription.metadata with end_user_id + feature_grants JSON. Remind me that resetAt comes from current_period_end.
6. Give a checklist: create key, set env, save rules, configure Stripe, grant test user OR trigger invoice.paid, hit the gated route, watch /dashboard.
## HTTP cheat sheet (if not using SDK)
Authorization: Bearer <api_key>
GET /api/v1/check?userId=&feature=
POST /api/v1/consume { userId, feature, amount }
POST /api/v1/grant { userId, feature, balance, resetAt? }
GET|PUT /api/v1/rules
POST /api/webhooks/stripe (Stripe-Signature)
Start by confirming my product context, then propose the rules graph.What a good outcome looks like
- A Plan → Feature → Limit graph with stable
featureKeyvalues - Env vars:
USAGEGATE_KEY,USAGEGATE_API_BASE_URL, Stripe webhook secret configured - Signup path calls
grantfor free-tier balances - Paid / expensive path uses
canAccess+consume - Stripe webhook + metadata bridge; agent understands billing periods (
resetAt=current_period_end)
Prefer a human walkthrough? Read the customer journey or quickstart.
