AI Discharge Summaries: Code the Med Changes, Let the Model Write the Story
LLM discharge summaries done safely: code diffs the medication changes, the model drafts cited narrative, and a physician signs a pended FHIR note.
Key takeaways
- Compute the discharge medication table in code by matching home MedicationStatements to discharge MedicationRequests on ingredient, and never let the model write it.
- When code can't decide, such as combination products, unparsed sigs or a missing order that could be held or stopped, flag the row for physician review instead of guessing.
- Count physician edits per section, track medication-table errors separately as safety events, and turn every correction into a regression case.
An LLM should never write the medication list on a discharge summary. A note with a perfect hospital course and one wrong dose is not 95% good. It's a patient taking the wrong thing at home.
So split the work in two. Code computes the medication changes by diffing home meds against discharge orders. The model drafts only the narrative sections, and every sentence cites the record. Code checks each citation. The draft goes back to the EHR as a pended note, and a physician signs it.
Compute what you can. Generate the rest.
This is the rule I apply across the clinical-note products I build (MedScrub, MedHook, eSpiral): if a section can be computed from structured data, compute it. The model gets only the parts that need prose.
The discharge summary splits along that line almost exactly. C-CDA already defines Hospital Course (LOINC 8648-8) and Hospital Discharge Medications (LOINC 10183-2) as separate coded sections. One is a story. The other is a table. Build them differently.
Everything below targets FHIR R4. Where R5 differs, I say so.
Scope the context to one Encounter
A discharge summary documents one admission. The context you hand the model should cover that admission and nothing else. That means the Encounter, plus the Observations, DiagnosticReports, Procedures, MedicationRequests, ServiceRequests and notes that reference it.
Prior admissions stay out. If the model never sees an old admission, it can't pull a date or a result from one into this hospital course. Extra history also dilutes everything else in the window. That's context rot, and a patient with six prior admissions is a textbook case. Assemble the bundle in code and hand the model a scoped task, not open access to the chart. Same reasoning as giving the model typed tasks instead of raw FHIR tools.
The Encounter's period.start and period.end also become the bounds for the date checks later.
The medication table is a diff, not a draft
The medication table is the most dangerous part of the summary. The Joint Commission's NPSG.03.06.01 requires hospitals to compare current medications with newly ordered ones and give the patient a reconciled list at discharge. That list is a safety requirement, not a writing task. The model never writes it. Code does.
The inputs exist in FHIR. Home and pre-admission meds are usually MedicationStatement resources. Discharge prescriptions are MedicationRequest resources with category = discharge. Both carry a FHIR Dosage (dosage on the statement, dosageInstruction on the request). Normalize that first, then classify each pair:
DAYS = {"h": 1 / 24, "d": 1, "wk": 7}
def normalize(dosage): # FHIR R4 Dosage
try:
q = dosage["doseAndRate"][0]["doseQuantity"]
r = dosage["timing"]["repeat"]
per_day = r["frequency"] / (r["period"] * DAYS[r["periodUnit"]])
return (q["value"], q["code"], per_day) # e.g. (10, "mg", 2.0)
except (KeyError, IndexError):
return None # free-text sig only
def classify(home, dc, recorded_action):
if home is None:
return "NEW"
if dc is None: # STOPPED or HELD only if the record says so
return recorded_action or "NEEDS_REVIEW"
if home.ingredients != dc.ingredients:
return "NEEDS_REVIEW" # combo product on one side only
if home.dose is None or dc.dose is None or home.dose[1] != dc.dose[1]:
return "NEEDS_REVIEW" # unparsed sig or unit mismatch
same = (home.dose, home.route, home.prn) == (dc.dose, dc.route, dc.prn)
return "UNCHANGED" if same else "DOSE_CHANGED"
Match on ingredient, not product code. Product-level codes can differ by brand and strength. If you match on the product, a brand-to-generic swap reads as one med stopped and another started. Match on the RxNorm ingredient, then compare doses. If dose were part of the match key, every dose change would show up as a NEW row plus a STOPPED row.
Combination products break single-ingredient matching. Home lisinopril/HCTZ against discharge lisinopril alone shares one ingredient and drops another. Don't split the combo into components and emit "HCTZ stopped" as if it were a clean answer. Pair rows whose ingredient sets overlap, and if the sets aren't identical, send the row to NEEDS_REVIEW. A combo-to-single switch is exactly the change a physician should see stated plainly.
"Dose changed" means any change the patient has to act on. That's why the comparison uses dose per administration, unit and times per day, not just total daily dose. 20 mg once daily and 10 mg twice daily are the same daily total and a different instruction. Route and PRN versus scheduled count too. Different units go to review instead of a conversion guess.
Absence is not "stopped." A home med with no matching discharge order might be stopped. It might be held, which tells the patient something different: don't take it now, expect to restart. It might be continued with no new prescription. If your site doesn't write discharge orders for continued meds, absence tells you nothing at all. Only return STOPPED or HELD when something in the record says so, such as an explicit discontinue or hold instruction. Otherwise flag it.
NEEDS_REVIEW is a feature, not a gap. That's the rule I'd hold any team to. When code can't decide, the row says so and the physician resolves it. That beats a confident guess, and it's the reason the model isn't in this loop. A model asked to fill the table has no natural way to say it doesn't know.
The diff is only as good as the home med list. MedicationStatement data is often patient-reported and often incomplete. If admission reconciliation missed a med, the diff will miss it too. Code makes the table consistent with the record. It can't make the record true.
This is the same pattern as LLM ICD-10 coding. There the model doesn't type the code. Here it doesn't type the dose.
What the model writes: hospital course, pending results, follow-up
The model gets three sections: hospital course, pending results and follow-up. These need judgment about what mattered during the stay and how to say it. Code can't write them well.
The constraint is that every sentence carries citations. A citation is either a resource ID from the scoped bundle or a span in a note. Ask for structured output, one object per sentence:
{"section": "hospital_course",
"text": "Blood cultures grew MSSA on day 2; switched to cefazolin.",
"cites": ["DiagnosticReport/bc-7781", "MedicationRequest/mr-2290"]}
Uncited sentences don't make it into the draft. I covered the claim-splitting and citation mechanics in how to detect hallucinations in AI clinical notes, so I won't repeat them here.
Check every sentence against the record
Citations make the draft checkable. The checks are plain code:
| Check | Compare against | What a failure means |
|---|---|---|
| Every cited ID exists | The scoped encounter bundle | The sentence cites something that isn't there, even if it sounds right |
| Hospital-course dates fall inside the encounter | Encounter period.start / period.end | The date leaked from another admission or was invented |
| Stated results match | The value on the cited Observation | A result not in the record is in the note |
| Follow-ups have an order or a booking | ServiceRequest or Appointment | A promise nobody made, like "cardiology in two weeks" |
| Pending results are still pending | DiagnosticReport and ServiceRequest status | The result already came back |
The last row is a rule I set, not something the spec hands you. A cited DiagnosticReport that is already final or amended isn't pending, so it can't sit in the pending section. A cited ServiceRequest must still be active, not completed.
A failed check never gets silently fixed. The sentence is dropped or flagged for the physician. Don't regenerate until the checks pass. A retry loop selects for drafts that pass the checks, not drafts that are right. That's the second rule I'd hold a team to.
Pending results become Tasks
This section has the clearest safety case. Roy et al. studied tests that came back after discharge. Roughly 41% of patients had results pending at discharge, and the responsible physicians were unaware of roughly 61% of those results. Some were potentially actionable. A systematic review by Kripalani et al. found discharge summaries were available at the first post-discharge visit only roughly 12-34% of the time, and they often left pending tests out.
A sentence in a note that someone may never read doesn't close that gap. So every pending result in the summary also becomes a FHIR Task:
focuspoints at the pending ServiceRequest or DiagnosticReport.foris the patient.encounteris the admission.owneris whoever must act on the result.restriction.periodsets when it must be handled.statusstarts atrequestedand moves tocompletedwhen someone reviews the result.
Now "pending at discharge" is a list you can query, and you can see which items are overdue.
Write it back pended, with Provenance
The draft goes back as an R4 DocumentReference with type LOINC 18842-5, "Discharge summary". US Core lists it among the note types servers must support. Don't mix up the two status fields. status is about the reference itself. docStatus is about the document. An unsigned AI draft is docStatus: preliminary, and the physician's signature moves it to final.
{"resourceType": "DocumentReference",
"status": "current", "docStatus": "preliminary",
"type": {"coding": [{"system": "http://loinc.org", "code": "18842-5"}]},
"context": {"encounter": [{"reference": "Encounter/enc-4412"}]}}
In R4, context.encounter ties the note to the admission. In R5 that became a general context reference, so this path doesn't carry over unchanged.
Alongside it, write a Provenance whose target is the DocumentReference. The software goes in as an agent (a Device, not a person). Every resource and note span the draft was built from goes in as an entity with role source. When the physician signs, add them as the verifier. Six months later, when someone asks where a sentence came from, the answer is in the record and not in your logs.
How each EHR accepts that write varies by vendor. I covered that in writing AI notes back to Epic, including why the model never signs.
Evaluate by section, and count med errors apart
The metric is physician edits, counted per section. Diff the preliminary draft against the signed final, section by section. A single "edit rate" for the whole note hides where the problem is. Heavy edits to follow-up point to a different fix than heavy edits to hospital course.
Count medication-table errors separately, and treat each one as a safety event. Don't average them into a quality score. That's how one wrong dose ends up looking like a 95% note. A med-table error also isn't a model error, because the model didn't write the table. It points at ingredient matching, sig parsing or the home med list. Tracking it apart from narrative edits sends each problem to the person who can fix it.
Every correction becomes a regression case. Freeze the encounter bundle and record what the physician changed. Then add an assertion the pipeline must pass from now on: this dose is DOSE_CHANGED, not UNCHANGED; this home med is HELD, not STOPPED; this follow-up is dropped because no Appointment exists. Run them on every prompt, model or mapping change. That's the loop from the honest guide to LLM evals, with signed notes as the labels.
Questions people ask
Can an LLM write a discharge summary?
It can draft the narrative sections: hospital course, pending results and follow-up. Each sentence should cite a resource ID or note span and be checked by code. The medication table should be computed by code, and a physician signs the final note.
How do you generate the discharge medication list with AI safely?
Don't generate it. Match home MedicationStatements to discharge MedicationRequests on RxNorm ingredient, normalize the Dosage, and classify each row as new, stopped, held, dose changed or unchanged. Combination products, unparsed sigs, unit mismatches and unexplained missing orders go to needs review.
How should an AI discharge summary be stored in FHIR?
In R4, write it as a DocumentReference with LOINC 18842-5, docStatus preliminary and context.encounter pointing at the admission. Add a Provenance that lists the software as an agent and the source resources as entities. The physician's signature moves it to final.
How do you evaluate AI-generated discharge summaries?
Count physician edits per section by diffing the draft against the signed note. Count medication-table errors separately as safety events, and turn every correction into a regression case that runs on every prompt, model or mapping change.