feat(billing): grant configurable AI gateway signup credit to new orgs - #6942
Open
nicacioliveira wants to merge 2 commits into
Open
feat(billing): grant configurable AI gateway signup credit to new orgs#6942nicacioliveira wants to merge 2 commits into
nicacioliveira wants to merge 2 commits into
Conversation
seedOrgDb now grants a one-time signup credit to every new org's Deco AI Gateway ledger via POST /api/admin/credits, idempotent at the gateway per `signup-credit:<orgId>` so a re-run never double-grants. Amount is configurable (DECO_AI_GATEWAY_SIGNUP_GRANT_CENTS, default 2500 = $25, 0 disables). Gated on gatewayAdminConfigured() (hosted only) and applied fail-soft — a grant error never fails org creation, mirroring the AI-key auto-provision path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…tion The control plane creates orgs via ORGANIZATION_CREATE; it can now pass `initialCreditCents` to set that org's welcome credit, overriding the deployment default. The value rides in org metadata so the afterCreate seed hook (the canonical creation boundary, covering every entry point) reads it via readInitialCreditCents — a defensive parser that accepts metadata as object or JSON string and ignores out-of-range/garbage, falling back to the default. Default grant lowered 2500 -> 2300 so it nets ~$25 with the gateway's own $2 provision credit. Env DECO_AI_GATEWAY_SIGNUP_GRANT_CENTS still sets the deployment default (0 disables), capped at $1,000 (MAX_SIGNUP_GRANT_CENTS). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Every new org is granted a one-time Deco AI Gateway credit on creation, netting ~$25 with the gateway's existing $2 provision credit. The amount is a deployment default and can be overridden per-org by the control plane at org creation.
How the credit is granted
provisionKeyonly mints a key (+ a small $2 auto-grant); balance is separate. The server-to-server grant isPOST /api/admin/credits(already used by the Stripe top-up webhook), idempotent perreferenceId.grantGatewaySignupCredit(billing/gateway-admin.ts) usesreferenceId = signup-credit:<orgId>→ a re-run ofseedOrgDbor any retry is a no-op at the gateway, so no local "already granted" state is needed.seedOrgDb(own try/catch, only logs) — a grant error never fails org creation, mirroring the AI-key auto-provision path. Independent of the provision block.Configurability
DECO_AI_GATEWAY_SIGNUP_GRANT_CENTS(default 2300 = nets ~$25;0disables; capped at $1,000 viaMAX_SIGNUP_GRANT_CENTS).ORGANIZATION_CREATEaccepts optionalinitialCreditCents. It rides in org metadata; theafterCreateseed hook (the canonical creation boundary, covering the MCP tool and direct Better Auth calls) reads it viareadInitialCreditCents, a defensive parser that handles metadata as object or JSON string and ignores out-of-range/garbage (→ falls back to the default).Scope
Applies to every org only when the gateway admin is configured (
gatewayAdminConfigured()=aiGatewayEnabled && aiGatewayAdminToken) — hosted deployments. Self-hosted can't reach the admin API and skip it.Testing
resolve-config.test.ts: default/override/0/non-numeric/cap for the new setting.initial-credit.test.ts: object vs JSON-string metadata, 0, out-of-range/garbage, cap boundary.bun run check(all workspaces) andbun run lintpass;bun run fmtapplied;generate:tool-contractsre-run (tool-io.ts).billing-checkout.integration.test.ts) is a pre-existing Postgres-dependent integration test (no local DB), untouched by this PR.Caveats
/api/admin/creditscontract (field names,referenceIddedupe) is inferred from the existingcreditGatewayTopUpconsumer — the ai-site gateway repo isn't in this workspace. Worth a confirm from the gateway side before relying on exact idempotency semantics.🤖 Generated with Claude Code
Summary by cubic
Grants new organizations a one-time Deco AI Gateway credit on creation, replacing the previous no-credit behavior. The amount is configurable via deployment default and per-org override.
Configuration
DECO_AI_GATEWAY_SIGNUP_GRANT_CENTSsets the default (2300 cents, 0 disables, capped at $1,000).ORGANIZATION_CREATEaccepts optionalinitialCreditCentsto override per org.Notes
referenceId = signup-credit:<orgId>, so retries never double-grant.Written for commit 20c81de. Summary will update on new commits.