Published · Updated · 8 min read
Shipping an LLM feature on health data, switched off
A health platform I build drafts session summaries with an LLM. The model call was the easy part. Here is the boundary around it, and why it shipped off.
Every product brief I get in 2026 has an AI line in it. On a French speech-therapy platform I build for a client, it reads: once a session is transcribed, draft a summary the therapist can edit.
The model call is about ten lines. The real work is everything around it, and the feature still went live switched off. This post covers both.
1. Write down what the AI may not do
Before any code, the architecture doc got a list of exclusions. The scope is a summary. Not SOAP notes, not homework, not parent reports, not structured clinical data, not decisions.
Three rules came out of that list. Each one is enforced by the shape of the code, not by a reviewer's memory:
- A draft is not a record. Patients never see it. Notes and exercises cannot reference it, and it is never exported. Only the text the therapist validates becomes part of the session record.
- No side effects. The AI's answer never changes a state, assigns anything, or sends a notification.
- Transcript in, text out. The boundary takes transcript lines and instructions. It has no way to read anything else on the platform.
The third one costs nothing on day one. Adding it later, after the prompt has started pulling in "a bit of context" from other tables, costs a rewrite.
2. One interface, and a stub behind it
The Summaries module in the NestJS API talks to one injected interface:
export interface AiSummarizer {
readonly provider: string;
readonly modelVersion: string;
/** Whether a request sends text out of the platform. */
readonly leavesPlatform: boolean;
summarize(request: SummarizerRequest): Promise<string>;
}It returns a string. Not a tool call, not JSON that drives logic. A string a person reads and then edits.
On a developer machine and in the test suites, the implementation is a stub. It calls nothing, says it is a test draft, and counts how many lines it received per speaker. That count is how the tests prove the names were replaced before a real provider would have seen them.
NODE_ENV picks the implementation. No environment variable chooses it, so there is nothing to set wrong in a deploy.
3. Replace names before text leaves
Teams labels every transcript line with whatever name the speaker typed when they joined. Before a transcript goes to a provider, the therapist's lines become « Orthophoniste » and everyone else's become « Patient ».
export function isTherapistSpeaker(speaker: string, therapistNames: readonly string[]) {
const words = new Set(normaliseName(speaker).split(" ").filter(Boolean));
return therapistNames.some((name) => {
const wanted = normaliseName(name).split(" ").filter(Boolean);
// A first name alone could be anyone's: it identifies nobody.
return wanted.length > 1 && wanted.every((word) => words.has(word));
});
}normaliseName drops accents, case, and punctuation, so "Dupont Marie (orthophoniste)" matches "Marie Dupont". The therapist's names come from three places: the profile's display name, first plus last name, and the account's full name. People type their name into Teams however they like.
To be clear about what this is: it is not anonymisation. The words are not changed, and anyone in a session can still say a name out loud. It removes the identifier stamped on every single line, the one a model sees hundreds of times per transcript. Whether that is enough is a question for the client's compliance review, not for my code, and the architecture doc says so in writing.
4. A draft is a row with a state machine
A draft moves through generating, ready, then final or discarded. A generation that gives up is failed. Regenerating marks the old draft replaced and opens a new one.
Postgres holds the invariants, not the service:
CREATE TABLE summary_drafts (
-- ...
provider text NOT NULL,
model_version text NOT NULL,
prompt_version text NOT NULL,
generated_content text,
content text,
CONSTRAINT summary_drafts_named_source
CHECK (provider <> '' AND model_version <> '' AND prompt_version <> '')
);
CREATE UNIQUE INDEX summary_drafts_one_live
ON summary_drafts (transcript_reference_id)
WHERE status IN ('generating', 'ready', 'failed');One live draft per transcript, however two requests race. And no draft without the provider, model, and prompt version that produced it. The prompt has a version string (the first one is ais-01), and a changed prompt gets a new one, so every draft says which prompt wrote it.
Each draft also keeps two texts. generated_content is what the model wrote and never changes. content starts as a copy and is what the therapist edits. A few months in, the gap between the two will be the best evidence I have of how useful the model is.
5. Never hold a lock during a model call
The worker runs a pass every 10 seconds and takes up to ten drafts. A model call can take many seconds, so it must not run inside a transaction that holds a row lock. Instead, the worker takes a lease with a conditional update:
const [taken] = await db
.update(summaryDrafts)
.set({ nextAttemptAt: leaseUntil, updatedAt: now })
.where(
and(
eq(summaryDrafts.id, id),
eq(summaryDrafts.status, "generating"),
lte(summaryDrafts.nextAttemptAt, now),
),
)
.returning();If the update returns a row, this worker owns the draft for five minutes. The model is called outside any transaction. The answer is written back only if the draft is still generating under that same lease. If the therapist discarded or regenerated it in the meantime, the late answer is dropped and their action stands.
It is close to the claim step in my BullMQ outbox relay: claim in one short statement, do the slow work after, and write back only if the claim still holds.
Failures split in two:
| Failure | What happens |
|---|---|
| Provider error that may pass | Retried after 1, 5, 15, then 60 minutes, then failed |
| Refusal or empty answer | failed at once |
Any failed draft | The therapist can regenerate it |
There is no fallback that shows the raw transcript instead. A missing summary is a normal state. A wrong one is not.
6. Log identifiers, never words
Each change of state writes one audit row with identifiers only: the draft, the transcript, the meeting, the appointment, the new status. The generation row adds the provider, the model, the prompt version, and a boolean left_platform. The stub sets it to false, because it sends nothing.
No log line carries a word of a transcript or a draft. Not the audit rows, not the retry warnings, not the error messages. A provider failure becomes a SummarizerError with a short code (empty_answer, provider_error) and a retryable flag, and nothing else.
7. Pick where the model runs before which model
In France, a company that hosts health data for someone else needs HDS certification. Sending a transcript to a model sends health data to another processor, so the choice of model starts with the choice of cloud.
The first pick, on 15 October, was Scaleway's Generative APIs. It lasted a day. Scaleway's HDS page names no AI service, and its HDS products need a support plan of at least €250 a month. Mistral's own API was out too: it is not HDS-certified. On 16 October the decision became Amazon Bedrock, under the same AWS HDS certificate as the rest of the hosting.
Then the region. Calls go to Bedrock in Paris (eu-west-3) or through its EU cross-region route, never the global one. AWS warns that a route's prefix does not prove which regions it uses, so the regions get listed from the account before go-live, and each must be one of AWS's six EEA regions. The test script refuses anything else:
export function assertEuProfile(profile: string): void {
if (!profile.startsWith("eu.")) {
throw new Error(`Refused: ${profile} is not an EU inference profile.`);
}
}The model gets picked by a test, not a leaderboard. Three candidates (Pixtral Large, Nova 2 Lite, and Claude Haiku 5.5) get the same invented French transcripts, the same prompt, and the same settings for the test (temperature 0.2, 800 tokens at most). I compare the summaries side by side. No patient data goes near it.
8. Then AWS said no
The new AWS account was given no Bedrock quota. At the time of writing, Support has not answered.
So on 25 October, go-live became go-live without the AI. Because of the boundary, that was a small change. One function decides whether a provider is wired:
export function isAiSummariesAvailable(): boolean {
return process.env.NODE_ENV !== "production";
}In production it returns false. The worker runs no generation pass. Every summary route answers 501 feature_blocked, after its access check, so the 501 tells nobody whether a draft exists. GET /v1/me returns ai_summaries_available: false, and the web app hides every AI link. The table exists and stays empty.
When the quota arrives, the Bedrock adapter is one class that implements AiSummarizer, and this function learns to say yes. The review screens, the state machine, the audit trail, and the tests are already there.
What I'd do again
- Write the exclusions first. They became the architecture.
- One interface that returns plain text, with a stub that proves what it was sent.
- Replace speaker names before the call, and say plainly that this is not anonymisation.
- Let Postgres enforce one live draft and a named source on every row.
- Leases, not locks, around slow calls.
- Audit identifiers, never content.
- Ship the off switch with the feature. I needed mine before the feature had ever run in production.
If you are adding AI to a product that handles sensitive data, get in touch.