# Health patterns developer guide

> Build on the health-pattern engine: on-request and stored findings, the person's consent, the display rule, method maturity, feedback, research on a finding, the safety header, the no-diagnosis rule and rate limits.

Source: https://developers.anpheros.com/guides/health-patterns-api

Anpheros finds patterns in a person's own record by fixed statistical rules: trends, deviations from the person's
usual level, recurrence, and things recorded around the same time. This guide covers what you need to build on it:
the two ways to read findings, consent, the display rule, method maturity, feedback, research, the safety header, the
no-diagnosis rule and the rate limits. For the methods themselves (what each test does, validation, confidence axes,
next steps), see [Health patterns for AI agents](https://developers.anpheros.com/guides/health-patterns).

## Concepts

| Term | Meaning |
|---|---|
| finding | One description of the record: a type (`trend`, `baseline_deviation`, `recurrence`, `association`, `convergence`), the measurement or symptom, the period, the resources behind it, the method and its limitations. |
| on-request run | `GET /v1/patients/{id}/patterns`: computed now, under your credential's access. Finding ids (`pf_…`) change when the record changes. |
| stored finding | Kept up to date in the background for the trusted first-party project the person gave the `anpheros/patterns` feature consent to. Its `finding_key` (`fk_…`) stays the same as new data arrive. |
| display rule | When a stored finding may be shown to the person (`shown`). |
| maturity | How far a method has been validated: `experimental`, `internally_validated`, `clinically_reviewed`, `production_approved`. |
| `statistically_supported` | `true` only when the finding passed its statistical test. Anything else is a description, not evidence. |

## The no-diagnosis rule

Findings describe what the records show. They never name a condition, never say what caused something, and never
recommend a medicine, a dose or a change of treatment. Every finding carries `safety.not_a_diagnosis = true` and
`safety.causal_claim = false`. An association means two things were recorded around the same time, nothing more.

What you build on top must keep this:

- Show the finding's own `summary_text`, its period, its method maturity and its limitations. Do not turn a finding into
  a statement about a disease, a cause or a treatment.
- The next step you offer a person is to keep recording or to bring the pattern up with their doctor, never to start,
  stop or change a medicine.
- When you pass findings to a language model, tell it the same in the system prompt, and give it only the findings the
  person may see (below).

## Two ways to read findings

| | On-request run | Stored findings |
|---|---|---|
| Routes | `GET /v1/patients/{id}/patterns`, `…/patterns/{finding_id}`, `…/patterns/{finding_id}/evidence`, `GET /v1/patients/{id}/series`, `POST /v1/patients/{id}/patterns/compare` | `GET /v1/patients/{id}/patterns/current`, `…/patterns/{finding_key}/history`, `…/patterns/{finding_key}/feedback`, `…/patterns/{finding_key}/research` |
| Who | Any sandbox credential; on production records an OAuth grant that includes the `anpheros/patterns` scope | Only the trusted first-party project the person consented to (its API key, or its app's token for this person with read access to everything the engine reads) |
| Ids | `pf_…`, new when the record changes | `fk_…`, stable per project and record |
| `shown` | `true` only when the stored finding with the same key is shown | the display rule |
| Use | developer tools, one-off analysis, agents with the person's OAuth consent | anything shown to the person, notifications, feedback, research |

Every other credential gets `403` on the stored routes, with a pointer to the on-request run.

## Consent

- **The person decides.** Pattern analysis is a separate permission: on an OAuth consent screen the `anpheros/patterns`
  scope has its own line; in a first-party app it is a feature consent the person turns on and can turn off at any time.
- **Revocable.** Withdrawing the feature consent stops the background refresh at once and deletes the stored findings,
  their history, the feedback and the research assessments for that project. Withdrawing an OAuth grant ends access
  at once.
- **Never widened.** The engine reads only what your credential may read; what it could not read is listed in
  `omitted`, without content. A stored finding is never served to a credential narrower than the view it was computed
  with.
- **Population data are separate.** Patterns are about one person's own data. Population signals, similar cases and
  community features have their own consents and never read a person's findings without them.

## The display rule

A stored finding is **shown** only after it stayed statistically supported over consecutive evaluations spanning at
least 14 days, and **hidden** only after it stayed unsupported for 28 days, or at once when it is no longer computed.
The rule is in `display_rule` of `GET /v1/patients/{id}/patterns/current`.

Show a finding to a person only when all three hold:

1. `status` is `current`;
2. `shown` is `true`;
3. `method.patient_facing` is `true` (from the method registry, see below).

Findings with `shown_only_if_supported = true` (day-of-week, time-of-day, month-of-year and step changes) pick what they
name among many tries: show them only when statistically supported. Next steps carry `patient_facing` too.

## Method maturity

`GET /v1/patterns/methods` lists every method with its version and maturity. Maturity comes from the committed
validation report and the registry, never from a flag you set:

| Maturity | What it means | Show to the person? |
|---|---|---|
| `experimental` | Runs and is returned to developers | No. If you show it in a developer tool, label it "experimental, not validated". |
| `internally_validated` | Passed the internal statistical validation on synthetic data | Yes, with `disclosure` ("Method set with AI help; not reviewed by a clinician.") |
| `clinically_reviewed` | A named clinician reviewed the method and its thresholds | Yes |
| `production_approved` | Clinical review plus product sign-off | Yes |

Internal validation is not clinical validation: never present a finding as clinically validated unless its maturity
says so. `method.notify` is `true` only for validated methods that also pass the repeated-looks test; notify a person
only about findings of those methods (see Notifications).

## Reading a stored finding

```bash
curl -s https://platform.anpheros.com/v1/patients/$PATIENT/patterns/current \
  -H "Authorization: Bearer $ANPHEROS_KEY"
```

The response has `last_run`, `as_of`, `stale`, `refresh`, `findings`, `next_steps`, `display_rule` and
`interpretation_guidance`. Each finding carries, among others: `finding_key`, `type`, `subtype`, `summary_text`,
`window`, `statistically_supported`, `method` (`name`, `version`, `maturity`, `patient_facing`, `notify`), `disclosure`,
`confidence` (data quality, statistics, extraction: separate axes, never one score), `limitations`, `evidence`,
`status`, `shown`, `shown_since`, `first_seen_at`, `last_changed_at`.

Until the first background run finishes after consent, the route answers `409` with `error.type = not_computed_yet`
and `queued`; try again in a few minutes. `GET …/patterns/{finding_key}/history` lists when the finding appeared, gained
or lost support, and went.

## Feedback

```bash
curl -s -X POST https://platform.anpheros.com/v1/patients/$PATIENT/patterns/$FINDING_KEY/feedback \
  -H "Authorization: Bearer $ANPHEROS_KEY" -H "Content-Type: application/json" \
  -d '{"response": "does_not_match", "note": "I was travelling that week"}'
```

`response` is `matches`, `does_not_match`, `unsure` or `more_info`; `note` is optional, at most 500 characters. The
feedback is kept apart from the record: it is never written to FHIR, never sent to a language model and never used to
change thresholds. It needs the `write` scope and is deleted with the consent.

## Research on a finding

`GET /v1/patients/{id}/patterns/{finding_key}/research` says whether published research in the Anpheros research
library has looked at the relationship a stored finding describes:

- `available: true` with `research.verdict` = `investigated` (counts per study design, the strongest design, numbered
  citations) or `not_adequately_investigated` (with what is missing);
- `available: false` with a `reason`: `finding.library_not_populated` while the library holds nothing for the finding's
  concepts (it is still being built), `finding.not_shown` or `finding.not_mapped`.

Show exactly what the platform returns. When `available` is false, show the reason, never a verdict. Research
describes groups of people, not this person; "supports" and "contradicts" appear only in `research.curation`, from human
reviewers. Built from concept codes over the local library: nothing about the person is sent anywhere. See
[Research library API](https://developers.anpheros.com/guides/research-api).

## Safety header obligation

When a write records a PHQ-9 `QuestionnaireResponse` whose item 9 (LOINC `44260-8`) is answered above 0, the platform
answers the write (create, update or a Bundle) with:

```
X-Anpheros-Safety: crisis-resources
Link: </v1/safety/crisis-resources>; rel="help"
```

**An app that receives this header must show crisis lines to the person at once**, from
`GET /v1/safety/crisis-resources?country=XX` (no authentication, no patient data): the emergency number, a national
crisis line where one is listed, the Find A Helpline directory and a short plain-English text. The platform contacts
nobody, stores no flag and adds no interpretation.

Both SDKs report the header for you (`onSafety` in the client options, `lastSafety` on the client). Through the MCP
server an agent cannot see headers, so the tool result carries a `safety` block with the crisis lines and the
instruction to show them first.

## Notifications

Notify a person only when the `patterns.updated` webhook brings a `shown` event and
`GET /v1/patients/{id}/patterns/current` returns that finding with `shown`, `method.patient_facing` and `method.notify`
all `true`. Keep it rare (the Anpheros apps send at most one pattern notification per person per week), never at
night, and keep the content of the finding off the lock screen: "You have an update in the app." is enough. Do not
send notifications about mental health, sexual or reproductive health.

## Errors

| Status | When |
|---|---|
| `400` | A bad parameter (window, types, periods) or body (`response` not one of the four values, `note` too long) |
| `403` | Production credentials without the person's consent, or a stored route called by anything other than the trusted project |
| `404` | The record is not in your view, the finding id changed with the data, or no stored finding with this key |
| `409` | `not_computed_yet`: the person consented and the first background run has not finished |
| `429` | A rate limit (below); retry after `Retry-After` |

Errors use the usual shape (`error.type`, `error.message`); see [Errors](https://developers.anpheros.com/guides/errors).

## Rate limits and paging

- Pattern routes (runs, compare, research on a finding, feedback): 30 requests a minute per credential and record,
  and 600 a minute per credential in total, on top of the usual per-credential limit.
- Research library search: 120 requests a minute per credential.
- Paging: evidence (`limit`, `cursor`) and research records (`limit` up to 50, `cursor`). `GET …/patterns/current`
  returns all stored findings of the record in one response; filter with `types` and `supported_only`.

## Try it in the sandbox

Every sandbox project has 30 synthetic patients; six carry a planted pattern. With a sandbox key (`sk_test_…`):

```ts
import { Anpheros, apiKey } from '@anpheros/sdk';

const anpheros = new Anpheros({ auth: apiKey(process.env.ANPHEROS_KEY!), onSafety: (s) => console.log('show crisis lines now', s) });
const bundle = await anpheros.fhir.search('Patient', { identifier: 'https://anpheros.com/fhir/sid/sandbox-persona|p01' });
const patientId = bundle.entry![0].resource!.id!;

// 1. p01's home blood pressure falls over six months: the engine reports a decreasing systolic trend (8480-6).
const run = await anpheros.patterns.run(patientId, { window_days: 180 });
const sbp = run.findings.find((f) => f.type === 'trend' && f.variables[0].code === '8480-6')!;
console.log(sbp.summary_text, sbp.statistically_supported, sbp.method.maturity, sbp.limitations.map((l) => l.code));

// 2. The resources behind it, and the method's maturity in the registry.
const evidence = await anpheros.patterns.evidence(patientId, sbp.id, { window_days: 180 });
const methods = await anpheros.patterns.methods();

// 3. The safety header: a PHQ-9 with item 9 above 0 triggers onSafety; then fetch the lines.
await anpheros.fhir.create({ resourceType: 'QuestionnaireResponse', status: 'completed', questionnaire: 'http://loinc.org/q/44249-1',
  subject: { reference: `Patient/${patientId}` }, item: [{ linkId: '44260-8', answer: [{ valueInteger: 1 }] }] });
const lines = await anpheros.patterns.crisisResources('US');
```

In the sandbox `shown` stays `false`: stored findings and the display rule exist only for the trusted first-party
project under a real person's consent, so the sandbox shows the on-request run, the evidence, the registry and the
safety header. The whole script is in `sdk/examples/health-patterns`. With the MCP server, `get_health_patterns` returns
only shown findings of patient-facing methods by default (so nothing in the sandbox); pass `include_experimental` in
developer tools only. The trusted project reads its stored findings with `get_stored_patterns`.

## Frequently asked questions

### Can a finding be shown to a person as soon as it appears?
No. Only stored findings with `status = current`, `shown = true` and `method.patient_facing = true`. A finding becomes
shown after it stayed statistically supported over evaluations spanning at least 14 days.

### Does the research endpoint ever answer without research?
Yes: while the library holds nothing for the finding's concepts it answers `available: false` with
`finding.library_not_populated`. Show that reason; never infer a verdict.

### What must my app do with X-Anpheros-Safety?
Show crisis lines to the person at once, from `GET /v1/safety/crisis-resources`. Do not label or interpret the answer.

## Related

- [Health patterns for AI agents](https://developers.anpheros.com/guides/health-patterns)
- [Research library API](https://developers.anpheros.com/guides/research-api)
- [Webhooks](https://developers.anpheros.com/guides/webhooks)
- [Consent and access model](https://developers.anpheros.com/guides/consent)
- [Limits and rate limits](https://developers.anpheros.com/guides/limits)
