---
title: The implemented example
description: One indicator, one objective, one alert in two forms, and the measured local run.
sidebar:
  icon: bell-ring
---

This is one alert on AI suggestion failures. It exists because that failure was invisible: no error reached the load balancer, and the health check stayed green.

## The indicator

The WebSocket handler used to catch every exception, `print` it and continue. When the AI call breaks, users get no suggestions, the health check passes and the ALB sees no 5xx. Nothing fires. Now every suggestion request ends with exactly one `outcome`.

$$
\text{failure ratio} = \frac{\texttt{ai\_error} + \texttt{invalid\_response}}{\texttt{ok} + \texttt{ai\_error} + \texttt{invalid\_response}}
$$

`bad_request` (a malformed client message) is in neither part: it is not a service failure.

## The objectives

| Path | Objective | Alert |
|---|---|---|
| AI suggestions | **99.5%** of requests succeed over 30 days. A starting value, to tune from real traffic. | `SuggestionFailureBudgetBurn` (this page) |
| Document load and save | **99.9%** of requests are not 5xx over 30 days, matching the 99.9% availability target | No burn-rate alert. The existing ALB 5xx-ratio alarm covers it coarsely. |

The error budget for suggestions is 0.5%. The alert uses the multi-window burn-rate pattern. At a burn rate of 14.4, 2% of the 30-day budget is spent in one hour and the whole budget in about two days. The threshold is $14.4 \times 0.5\% = 7.2\%$.

## The alert, in two forms

Both are built on the same counter, `suggestions.requests`.

**Prometheus (local, run)**

`observability/prometheus/rules.yml`: the ratio is above `14.4 * 0.005` over **1 hour and** over **5 minutes**, for 1 minute. The 1-hour window shows the burn is significant. The 5-minute window lets the alert clear soon after recovery. Labels `severity: page`; annotation `runbook_url` points at [the runbook](/incident-response#runbook-suggestionfailurebudgetburn).

`observability/prometheus/rules_test.yml` has five `promtool test rules` cases:
- 10% failures fire.
- 5% failures stay silent.
- A 20-minute burst after a healthy hour stays silent (the 1-hour window holds).
- A recovered incident clears (the 5-minute window clears).
- `bad_request` is ignored.

CI runs them in the `observability` job.

**CloudWatch (AWS, not run)**

`infrastructure/modules/service/alarms.tf`, `<name>-suggestion-failure-ratio`. Metric math over `Patent/Server` `suggestions.requests` by `outcome`, period 5 minutes, one datapoint, threshold `7.2` percent. It is evaluated only when there are 10 or more requests, and a missing metric is not breaching. It notifies the alarm topic.

It is also in the ECS service's deployment `alarms` block, next to the 5xx-ratio alarm, so **a release that breaks suggestions rolls back by itself**. CloudWatch evaluates one period per alarm, so this is the 5-minute window only.

The metric reaches CloudWatch through the collector sidecar: the `awsemf` exporter in `observability/collector/aws.yaml`, with dimension `outcome` only and no rollups.

## Run it

```bash
export COMPOSE_PROFILES=observability
export OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
docker compose up --build -d

# Break the suggestion path: a model that does not exist
printf 'services:\n  server:\n    environment:\n      OPENAI_MODEL: no-such-model\n' > /tmp/break-model.yml
docker compose -f docker-compose.yml -f /tmp/break-model.yml up -d --force-recreate server

pip install -r scripts/ci/requirements.txt
while true; do python scripts/ci/smoke.py http://localhost:8000 --timeout 3; done
# Watch http://localhost:9090/alerts, then restore:
docker compose up -d --force-recreate server
```

`observability/README.md` has the same steps and the validation commands.

## Evidence

Run on 2026-11-23 against the real OpenAI API, using the model in `server/.env`. Steps:

1. Five smoke tests succeeded.
2. The model was set to `no-such-model`, and failing requests were sent every 3 seconds.
3. After the alert fired, the model was restored and one more smoke test succeeded.

| Check | Result |
|---|---|
| `suggestions_requests_total{outcome="ok"}` in Prometheus after 5 smoke tests | `5`, with `job="patent-server"`; `target_info` shows `deployment_environment_name="local"`, `service_version="local"` |
| Jaeger | Each `suggestions.review` is a root span with a child `POST https://api.openai.com/v1/chat/completions` from the httpx instrumentation |
| Logs | `suggestion.completed` with `trace_id` `163c647a…5230` and `span_id` `30bff2c7…023c`, the same IDs as the Jaeger span |
| Failure log line | `"event": "suggestion.failed", "outcome": "ai_error", "error.type": "NotFoundError"`. No message: the model name from OpenAI's error text appears 0 times in the log |
| Alert `pending` | **15 s** after the first failed request |
| Alert `firing` | **76 s** after the first failed request (25 failed requests); `for: 1m` is most of it |
| Alert resolved | **287 s** after the model was restored, as the failures aged out of the 5-minute window |
| Smoke test document text in telemetry | 0 hits in 95 exported spans, the server logs, the collector log and the Prometheus series. 0 `Authorization`, 0 `exception.message` |
| Without the two variables | `docker compose up --build` starts only `server`, `client` and `docs`; the server serves and logs no exporter errors |

An earlier run with a 99% objective (threshold 14.4%) measured 79 s to fire and 287 s to resolve.

:::note[What the local run does not prove]
The 1-hour window had only the data since start-up, so it behaved like the 5-minute window. The rule tests cover the two windows separately. The CloudWatch alarm, EMF export and X-Ray were not run; see [Limits and next steps](/observability-limits#not-verified-against-a-live-account).
:::
