> For the complete documentation index, see [llms.txt](https://docs.uibakery.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.uibakery.io/build-with-ai/usage-credits-monitoring.md).

# Usage credits monitoring

You can monitor usage credits via the Instance API endpoint or by setting up the low-credit email alerts.

### Credits endpoint

Use the Instance API endpoint to read usage credits for a workspace by slug.

```
GET /api/instance/organization/{slug}/credits
Authorization: Bearer <UI_BAKERY_INSTANCE_API_TOKEN>
```

Example:

```bash
curl -i https://<your-instance-host>/api/instance/organization/acme/credits \
  -H "Authorization: Bearer $UI_BAKERY_INSTANCE_API_TOKEN"
```

The endpoint returns the standard Instance API response wrapper. The credits balance is in `result`.

Example response:

```json
{
  "status": "OK",
  "message": "Credits balance fetched",
  "result": {
    "organizationId": "b6b3e804-bf24-11f0-b596-9514a4f9f5de",
    "organizationSlug": "acme",
    "availableCredits": 249.68,
    "lastTopUpTotalCredits": 250.0,
    "percentageRemaining": 99.87,
    "billingDisabled": false
  }
}
```

The endpoint returns customer-facing UI Bakery usage credits, not raw AI/token credits.

Result fields:

<table data-header-hidden data-search="false"><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody><tr><td><code>organizationId</code></td><td>Workspace view ID.</td></tr><tr><td><code>organizationSlug</code></td><td>Workspace slug from the request.</td></tr><tr><td><code>availableCredits</code></td><td>Remaining UI Bakery usage credits.</td></tr><tr><td><code>lastTopUpTotalCredits</code></td><td>Total usage credits from the latest top-up/current balance period.</td></tr><tr><td><code>percentageRemaining</code></td><td>Remaining percentage, calculated as <code>availableCredits / lastTopUpTotalCredits * 100</code>, rounded to 2 decimals. May be <code>null</code> if the total is missing or zero.</td></tr><tr><td><code>billingDisabled</code></td><td><code>true</code> when credits billing is disabled for the chat service/runtime.</td></tr></tbody></table>

Status codes:

| Status | Meaning                                                     | Body                          |
| ------ | ----------------------------------------------------------- | ----------------------------- |
| `200`  | Credits balance returned successfully.                      | `status`, `message`, `result` |
| `403`  | Missing or invalid Instance API token.                      | Instance API auth error body  |
| `404`  | Organization with the given slug was not found.             | `status`, `message`           |
| `502`  | Java backend could not fetch credits from the chat service. | `status`, `message`           |

### Low-credit alerts <a href="#id-28f0588d-53d0-4a56-8dd8-dd0ad0b564d9" id="id-28f0588d-53d0-4a56-8dd8-dd0ad0b564d9"></a>

The backend includes a scheduled job that checks Workspace credit balances and sends alerts when configured thresholds are reached.

Alerts are disabled by default. To deliver alerts, all of the following must be true:

* `UI_BAKERY_CREDITS_ALERT_ENABLED=true`
* at least one threshold is configured,
* at least one delivery channel is configured: email and/or webhook.

Alerts are skipped when `billingDisabled=true`.

#### Alert thresholds <a href="#b0f50ab2-bd5a-4f3d-baa1-5b723251282e" id="b0f50ab2-bd5a-4f3d-baa1-5b723251282e"></a>

Two threshold types are supported:

* absolute usage credits threshold;
* percentage remaining threshold.

An alert is sent when either configured threshold is reached.

Example:

```
UI_BAKERY_CREDITS_ALERT_THRESHOLD_CREDITS=10
UI_BAKERY_CREDITS_ALERT_THRESHOLD_PERCENT=20
```

This sends an alert when either:

* the workspace has 10 usage credits or fewer; or
* the workspace has 20% or less of its latest top-up remaining.

Use `-1` to disable a threshold:

```
UI_BAKERY_CREDITS_ALERT_THRESHOLD_CREDITS=-1
UI_BAKERY_CREDITS_ALERT_THRESHOLD_PERCENT=20
```

With this configuration, only the percentage threshold is used.

#### Alert configuration variables <a href="#b28ebf36-8b09-4186-96a9-e4ee62bd545a" id="b28ebf36-8b09-4186-96a9-e4ee62bd545a"></a>

<table data-header-hidden data-search="false"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td>Variable</td><td>Default</td><td>Description</td></tr><tr><td><code>UI_BAKERY_CREDITS_ALERT_ENABLED</code></td><td><code>false</code></td><td>Enables or disables the scheduled low-credit alert job.</td></tr><tr><td><code>UI_BAKERY_CREDITS_ALERT_THRESHOLD_CREDITS</code></td><td><code>-1</code></td><td>Absolute usage credits threshold. <code>-1</code> disables this threshold.</td></tr><tr><td><code>UI_BAKERY_CREDITS_ALERT_THRESHOLD_PERCENT</code></td><td><code>-1</code></td><td>Percentage remaining threshold. <code>-1</code> disables this threshold.</td></tr><tr><td><code>UI_BAKERY_CREDITS_ALERT_EMAIL_ENABLED</code></td><td><code>true</code></td><td>Enables email delivery to organization admins.</td></tr><tr><td><code>UI_BAKERY_CREDITS_ALERT_WEBHOOK_URL</code></td><td>empty</td><td>Optional webhook URL for alert delivery.</td></tr><tr><td><code>UI_BAKERY_CREDITS_ALERT_COOLDOWN_HOURS</code></td><td><code>24</code></td><td>Per-organization cooldown after a successful alert. Prevents repeated alerts while credits remain low.</td></tr><tr><td><code>UI_BAKERY_CREDITS_ALERT_CRON</code></td><td><code>0 0 * * * *</code></td><td>Spring cron expression for scheduled checks. Spring cron includes seconds.</td></tr></tbody></table>

Important behavior:

* The alert job has a hard minimum check interval of 15 minutes. If the cron runs more often, checks are skipped until the next allowed time.
* Cooldown is tracked in memory per Java process and resets after backend restart.
* Cooldown is recorded only after at least one delivery channel succeeds.
* Email and webhook delivery are attempted independently. If one channel fails, the other can still succeed.

#### Webhook alerts <a href="#b0c47cee-acea-4ea2-8245-b5926bfb3c8d" id="b0c47cee-acea-4ea2-8245-b5926bfb3c8d"></a>

Set `UI_BAKERY_CREDITS_ALERT_WEBHOOK_URL` to send a `POST` request when credits are low.

```
UI_BAKERY_CREDITS_ALERT_ENABLED=true
UI_BAKERY_CREDITS_ALERT_WEBHOOK_URL=https://example.com/credits-alert-webhook
UI_BAKERY_CREDITS_ALERT_THRESHOLD_PERCENT=20
```

Payload example:

```json
{
  "event": "credits.low_balance",
  "organizationId": "b6b3e804-bf24-11f0-b596-9514a4f9f5de",
  "organizationSlug": "acme",
  "availableCredits": 9.5,
  "lastTopUpTotalCredits": 250.0,
  "percentageRemaining": 3.8,
  "billingDisabled": false,
  "text": "UI Bakery usage credits are low for acme: 9.5 usage credits remaining (3.8%)."
}
```

The webhook payload is not the same as the Instance API response. It intentionally includes `event` and `text` for automation/notification tools.

The `text` field is included so the payload can be used with Slack-compatible incoming webhooks or automation tools. The webhook request has a 5-second connection/read timeout.

### Email alerts <a href="#id-9960aeb7-7586-4601-98ef-66ec5392b930" id="id-9960aeb7-7586-4601-98ef-66ec5392b930"></a>

Email alerts are sent to Workspace admins.

Enable email delivery:

```
UI_BAKERY_CREDITS_ALERT_ENABLED=true
UI_BAKERY_CREDITS_ALERT_EMAIL_ENABLED=true
UI_BAKERY_CREDITS_ALERT_THRESHOLD_PERCENT=20
```

For custom HTML templates over SMTP, configure:

```
UI_BAKERY_MAILING_PROVIDER=smtp
UI_BAKERY_MAILING_TEMPLATES_MODE=custom
UI_BAKERY_MAILING_EMAIL_FROM=admin@uibakery.io
UI_BAKERY_MAILING_NAME_FROM=UI Bakery

UI_BAKERY_SMTP_HOST=sandbox.smtp.mailtrap.io
UI_BAKERY_SMTP_PORT=2525
UI_BAKERY_SMTP_USERNAME=<smtp-username>
UI_BAKERY_SMTP_PASSWORD=<smtp-password>
UI_BAKERY_SMTP_ENCRYPTION=tls
```

For local testing, Mailtrap can be used as the SMTP server. In that case, the email recipient is still the Workspace admin email, but the message is captured in the Mailtrap inbox and is not delivered to the real mailbox.

#### Email template customization <a href="#ea11becf-8a6e-4996-8582-198d3fbfe0c9" id="ea11becf-8a6e-4996-8582-198d3fbfe0c9"></a>

The low-credit email subject and template can be customized with:

```
UI_BAKERY_MAILING_CREDITS_LOW_SUBJECT=UI Bakery usage credits are low
UI_BAKERY_MAILING_CREDITS_LOW_TEMPLATE=<h2>Low credits for organizationName</h2><p>Hello userName, organizationSlug has availableCredits usage credits left from lastTopUpTotalCredits. Remaining: percentageRemaining%.</p>
```

Available template variables:

<table data-header-hidden data-search="false"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td>Variable</td><td>Description</td></tr><tr><td><code>userName</code></td><td>Admin display name or email.</td></tr><tr><td><code>userEmail</code></td><td>Admin email address.</td></tr><tr><td><code>organizationName</code></td><td>Workspace display name.</td></tr><tr><td><code>organizationSlug</code></td><td>Workspace slug.</td></tr><tr><td><code>availableCredits</code></td><td>Remaining usage credits.</td></tr><tr><td><code>lastTopUpTotalCredits</code></td><td>Latest top-up/current balance total in usage credits.</td></tr><tr><td><code>percentageRemaining</code></td><td>Remaining percentage.</td></tr></tbody></table>

Template substitution is plain string replacement. Use `userName`, `organizationName`, etc., directly in the template. Do not use `{{ userName }}` syntax for the SMTP custom template.

Example rendered template:

```
<h2>Low credits for Acme</h2>
<p>Hello Jane Admin, acme has 9.5 usage credits left from 250.0. Remaining: 3.8%.</p>
```

### Sending both email and webhook <a href="#id-2ba44d66-3cb2-480c-a874-4083fd9cfc4a" id="id-2ba44d66-3cb2-480c-a874-4083fd9cfc4a"></a>

Email and webhook can be enabled together:

```
UI_BAKERY_CREDITS_ALERT_ENABLED=true
UI_BAKERY_CREDITS_ALERT_EMAIL_ENABLED=true
UI_BAKERY_CREDITS_ALERT_WEBHOOK_URL=https://example.com/credits-alert-webhook
UI_BAKERY_CREDITS_ALERT_THRESHOLD_CREDITS=10
UI_BAKERY_CREDITS_ALERT_THRESHOLD_PERCENT=20
```

When the threshold is reached, UI Bakery sends an email to Workspace admins and also posts the webhook payload.&#x20;


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.uibakery.io/build-with-ai/usage-credits-monitoring.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
