> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tuturuuu.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Workspace AI credit grants

> Audited platform-admin grants of AI credits to any workspace or a free-tier member.

Infrastructure owns the AI credit grants page at `/admin/<root>/credits`.
Operators need a stored root workspace membership row of type `MEMBER` plus
`manage_workspace_roles`; being the root workspace's creator is not enough.
`private.grant_workspace_ai_credits` re-checks the same row, and the
navigation shows the page only to operators who pass the same check. Any workspace can receive a grant, including the root workspace and
personal workspaces. The legacy `POST /api/v1/admin/ai-credits/balances` bonus
endpoint uses the same function, permission, cap and audit. It requires a
`request_id` idempotency key and takes the amount as a decimal string
(`"12.5"`) so precision is checked before any number conversion. The balances
tab keeps each balance's attempt and key above its rows, so filtering,
paging or closing a row never drops it and re-opening the balance resumes it.
While a write is in flight the tab locks its filters, paging and rows, and it
asks for the typed amount on large grants.

## Target balance

Credits are spent from the balance the workspace's tier routes usage to, so a
grant lands where it can be spent:

| Workspace tier | Receiving balance |
| - | - |
| Paid (PLUS, PRO, ENTERPRISE) | The workspace balance of the current period. |
| FREE | The chosen `MEMBER`-type member's own user-scoped balance ("grant to member"). Guests cannot receive grants. The picker defaults to the workspace creator and searches members on the server. |

A workspace-scoped grant to a FREE workspace and a member grant to a paid
workspace are refused rather than recorded as credits nobody can spend. A FREE
member balance is the member's own FREE balance, so the member can spend it in
any FREE workspace they use. A member the operator names explicitly must be a
`MEMBER` of the workspace; otherwise the page refuses the grant and asks for a
new choice instead of falling back to the default. A member named for a
paid-tier workspace is refused the same way, with a prompt to clear it, rather
than silently becoming a workspace grant. The page shows the tier and
the receiving balance before the confirmation, and in the confirmation itself.

## Rules

* A grant adds to the receiving balance's `bonus_credits`, appends one `bonus`
  row to `ai_credit_transactions` and writes one audit row in
  `private.workspace_ai_credit_grants`, all in one transaction. Remaining credits
  are `total_allocated + bonus_credits - total_used`.
* Bonus credits belong to the billing period and expire with it. Grants to a
  balance outside its current period are refused.
* Amounts have at most 4 decimal places, the precision the ledger stores;
  finer amounts are refused rather than rounded. A missing or invalid cap
  refuses every grant.
* Each grant has a request UUID. A retry with the same UUID, target (kind and
  ids), amount, reason and operator returns the recorded grant without adding
  credits again; any difference conflicts.
* One grant may add at most `ADMIN_AI_CREDIT_GRANT_MAX_AMOUNT` credits (default
  100,000). Grants above `ADMIN_AI_CREDIT_GRANT_CONFIRM_AMOUNT` (default
  10,000) need the amount typed again in the confirmation. Only unset
  variables use the defaults. A set value that is empty or blank, not a
  positive amount with at most 4 decimal places, a cap above the 1,000,000 database ceiling, or a threshold above the
  cap turns grants off until it is corrected; the database also refuses a
  missing, NaN or infinite cap.
* The private audit row records the operator, reason (3–500 characters), the
  requested target, workspace context, receiving member, balance, ledger
  transaction and period end. The ledger row the recipient can read carries
  only "Admin credit grant" and the grant id.
* Receipts are durable and written only by the grant function. They keep plain
  ids without foreign keys, so deleting a referenced row never removes a
  receipt or reopens its request id. No API role can read or write the table
  directly; history comes from the history function. The rollback keeps the
  table, so a retry after rollback and re-apply still replays. Grants cannot be
  revoked from this page.

Regression coverage: `apps/admin/src/lib/ai-credit-grants/*.test.ts`, the
credits page tests and `apps/admin/src/app/api/v1/admin/ai-credits/balances`
route tests (non-admin refusal, replay, cap and confirmation, root and FREE
member targets, ledger math, concurrent legacy grants).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.