# Set Up Alert Rules

These instructions apply to the current Admin Console.

{% hint style="info" %}
Alerts are in early access and may not be available in your account. [Contact Contextual support](mailto:help@contextual.io) to request that Alerts be enabled for your tenant.
{% endhint %}

Start with a template to obtain a supported configuration. A template supplies defaults and records its ID and version; it does not keep saved rules synchronized with later catalog changes.

You can set up a rule in the Admin Console or through the Alerts API.

* [Create an alert from a chart](https://docs.contextual.io/operate/alerts/create-alerts-from-charts.md) when you already have the agent or AI Gateway metric open.
* [Monitor Native Object values and activity](https://docs.contextual.io/operate/alerts/solution-metrics-alerts.md) with a state or audit-history rule.
* [Use a template with the Alerts API](#with-the-api) to automate rule setup.

## Create a Threshold Rule from a Template

### In the Admin Console

In the Admin Console, open **Operate → Alerts → Templates**, choose a threshold template, set its parameters and destination, and select **Review configuration**. Before selecting **Enable alert**, review the condition, resources it monitors, notification destination, and recovery preference. The Admin Console saves the rule and then creates its subscription.

**Notify on recovery** is checked by default. For Native Object and attachment templates, use the [API template catalog](https://docs.contextual.io/tenants/tenant-apis/alerts-api/configuration.md#templates).

To receive notifications when a stop, restart, or deletion is requested for an Agent or Worker, [set up a lifecycle subscription](https://docs.contextual.io/operate/alerts/agent-lifecycle-notifications.md#configure-a-subscription). By default, the subscription covers all current and future Agents and Workers. Choose **Specific deployments** to limit the subscription to the deployments you select.

### With the API

Confirm the target tenant and use its [Alerts API base URL and credentials](https://docs.contextual.io/tenants/tenant-apis/alerts-api.md#base-url-and-authentication). For direct requests, send the tenant’s organization ID in `x-org-id`. Have a [destination ID](https://docs.contextual.io/operate/alerts/destinations-and-subscriptions.md#create-a-destination) ready. Use a test destination to verify delivery with test traffic.

You need `contextual:alert-rules:read`, `contextual:alert-rules:create`, `contextual:alert-subscriptions:create`, and read access to the metric source. Check the [source permissions for AI metrics](https://docs.contextual.io/operate/alerts/metrics-and-queries.md#ai-metrics) if you’re configuring an AI Gateway rule. Before enabling a metrics rule, confirm [background metrics access](https://docs.contextual.io/operate/alerts/background-metrics-access.md) is ready. You can save a disabled draft before enrollment.

1. List templates with `GET /templates`. Render a template with `POST /templates/{id}/render`, passing the inputs in `parameters`. Record the returned template ID and version.
2. Check the returned `kind`. For a subscription, [set up a lifecycle subscription](https://docs.contextual.io/operate/alerts/agent-lifecycle-notifications.md#configure-a-subscription). For a rule, send `config` to `POST /rules/validate`. Confirm `valid` and every `guardRails[].ok` are true before saving. Review the condition and scope. If supported, send `config` to `POST /rules/preview` and [review its coverage](https://docs.contextual.io/operate/alerts/validate-and-preview.md#read-coverage-before-results).
3. Review the destination and recovery preference. A rule can fire before its subscription exists. If receiving the first firing notification matters, set `enabled` to `false` in `config`. Send `config` as the body of `POST /rules` and omit `id`.
4. Use the saved rule’s `_metaData.id` to [create its destination subscription](https://docs.contextual.io/operate/alerts/destinations-and-subscriptions.md#route-one-rule). If you saved the rule disabled, [enable it using its current hash](https://docs.contextual.io/tenants/tenant-apis/alerts-api/configuration.md#safe-writes). This requires `contextual:alert-rules:update`.
5. Check that the rule evaluates. Use controlled test traffic to confirm firing in alert history and receipt at the destination. Verify recovery where supported. To [check history and delivery details through the API](https://docs.contextual.io/tenants/tenant-apis/alerts-api/alert-operations.md#inspect-delivery-details), you need `contextual:alert-instances:read`. Disable or delete the test rule and remove unused test routes and destinations when done.

#### Example Template Parameters

* **Repeated agent startup failures.** Send `{ "parameters": { "agentScope": "selected", "agentIds": ["TEST_AGENT_ID"], "threshold": 3, "window": "15m" } }` to `POST /templates/agent-termination-threshold/render`. This detects more than three starting-to-terminated transitions in 15 minutes for each selected agent. Without selected-agent coverage, the rule covers current and future reported agents. Historical preview is unavailable for this metric. Verify the rule with live test data.
* **AI Gateway attempt failure rate above 5%.** Send `{ "parameters": { "threshold": 0.05, "window": "5m" } }` to `POST /templates/ai-error-rate-high/render`. The threshold is a ratio. See the [full rule body](https://docs.contextual.io/tenants/tenant-apis/alerts-api/configuration.md#ai-gateway-attempt-failure-rate-rule) for the metric and evaluation settings.

## Edit a Rule

The Admin Console supports [creating and editing alerts from supported charts](https://docs.contextual.io/operate/alerts/create-alerts-from-charts.md). The **Rules** list provides **Enable**, **Disable**, **Delete**, and **Edit notifications** actions; use the chart editor for a matching condition or the API for other rule configurations.

For an API update, read the current rule, edit and validate its complete writable configuration, then submit it with the current hash. See [safe writes](https://docs.contextual.io/tenants/tenant-apis/alerts-api/configuration.md#safe-writes) for the request and how to handle conflicts.

Changing the metric, grouping, target, query, window, or threshold can change which series are tracked and restart active evaluation. Review existing subscriptions and silences after such changes. See [How alerts fire and notifications are delivered](https://docs.contextual.io/operate/alerts/concepts-and-lifecycle.md).

## Disable or Delete

The Admin Console's **Rules** actions include **Disable**, **Enable**, and **Delete**. Disabling stops evaluation while retaining configuration. Re-enabling a metrics rule checks current authoring requirements, including enrollment.

Deleting a rule also deletes its rule-bound subscriptions. Label-policy subscriptions remain because they can cover other rules. Do not delete a rule merely to mute notifications during maintenance; use a [silence](https://docs.contextual.io/operate/alerts/silences-and-alert-history.md).

For multiple resources, use [declarative apply](https://docs.contextual.io/tenants/tenant-apis/alerts-api/configuration.md#apply-a-configuration-bundle). Inspect the plan first and check the per-item results afterward for partial changes.
