ReLMem: Learning Recurrent Memory for Longitudinal EHR Modeling
Organizations: Zhejiang University · Tencent Jarvis Lab
Abstract
Longitudinal electronic health record (EHR) modeling requires integrating new visits with an expanding patient history. Yet the continual accumulation of clinical information imposes increasing computational and memory costs on large language models (LLMs) when they process and retain complete patient histories. A practical alternative is visit-wise recurrent compression, which incorporates each incoming visit into a compact, continually updated patient memory. However, under a fixed memory budget, successive updates must integrate new information without progressively losing critical historical evidence needed to subsequent tasks. To address this challenge, we introduce Recurrent Longitudinal Memory (ReLMem), a framework that learns to maintain fixed-capacity patient memory for efficient downstream prediction with a frozen LLM. ReLMem equips this LLM with lightweight compression adapters to recurrently update the memory from its previous state and each incoming visit, without rereading earlier records. Specifically, we develop a multi-granularity optimization strategy to preserve task-relevant information throughout recurrent updates and support downstream prediction from the final memory. The intermediate supervision aligns attention outputs from compressed memory and the full history under identical queries, while prediction supervision minimizes cross-entropy with ground truth answers conditioned on the final memory. On EHR-based medication prediction, ReLMem approaches the F1 scores of full-history baseline while reducing average retained historical storage by 97.1%. Under the same memory budget, it improves macro- and micro-F1 over the strongest compressed-memory baseline by 4.66 and 4.75 percentage points, respectively. These results highlight the value of learning recurrent patient memory for efficient longitudinal EHR modeling.
Figures & tables
| Method | P@5 | P@10 | R@5 | R@10 | Macro-F1 (95% CI) | Micro-F1 (95% CI) | History Budget | ||
| Uncompressed reference | |||||||||
| Full History | 68.80 | 63.23 | 32.77 | 58.24 | 62.22 | (60.46, 63.92) | 63.32 | (61.69, 64.88) | 35,279 |
| History compression methods | |||||||||
| LLM-Rsum | 31.73 | 20.93 | 14.76 | 18.99 | 18.97 | (17.53, 20.46) | 19.21 | (17.80, 20.69) | 1,026 |
| SnapKV | 21.93 | 13.40 | 9.59 | 11.68 | 11.91 | (10.62, 13.20) | 11.95 | (10.71, 13.25) | 1,024 |
| KVzip | 63.73 | 54.03 | 30.06 | 49.44 | 52.53 | (50.41, 54.59) | 54.52 | (52.73, 56.28) | 1,024 |
| Resource | Full History | ReLMem |
| Historical KV state (MiB) | 4,961.08 | 144.00 |
| Update latency (s/visit) | 0.4676 | 0.3446 |
| Prediction latency (s/query) | 8.0837 | 6.5293 |
| Peak GPU memory (GiB) | 17.4354 | 9.2379 |
| Resource | Full History | ReLMem |
| Historical KV state (MiB) | 4,961.08 | 144.00 |
| Update latency (s/visit) | 0.4676 | 0.3446 |
| Prediction latency (s/query) | 8.0837 | 6.5293 |
| Peak GPU memory (GiB) | 17.4354 | 9.2379 |
| Curriculum | Macro-F1 | Micro-F1 | ||
| 0.37 | 0.55 | |||
| 47.67 | 48.65 | |||
| 61.00 | 62.24 | |||
| 61.93 | 63.24 |
| Method | Qwen3-4B | Qwen3-8B | Llama-3.1-8B |
| Full History | 62.77 | 62.69 | 65.62 |
| ReLMem | 62.59 | 61.01 | 63.16 |
| Method | Qwen3-4B | Qwen3-8B | Llama-3.1-8B |
| Full History | 62.77 | 62.69 | 65.62 |
| ReLMem | 62.59 | 61.01 | 63.16 |
| Method | Macro-F1 | Micro-F1 |
| Full History | 36.30 | 37.41 |
| ReLMem | 35.43 | 35.85 |
Appendix figures & tables15 assets
Supplementary material from the paper’s appendix.
Appendix
| JSON record template | Field meanings |
| { "demographics": { "age": <age in years> , "sex": " <sex> " }, "diagnoses": [" <diagnosis> ", ...], "medications": [" <medication> ", ...], "notes": [ { "available_day": <availability day> , "chart_day": <chart day> , "note_type": " <note type> ", "text": " <note text> " }, ... ], "procedures": [" <procedure> ", ...], "timeline": { "admit_day": <admission day> , "available_day": <availability day> , "discharge_day": <discharge day> , "gap_days": <interval or null> }, "visit_number": <local visit index> } | demographics : age in years and recorded sex. diagnoses , procedures , and medications : lists of clinical names from this completed visit. notes : a variable-length list of notes. Each note contains its type, text, chart day, and availability day. timeline : admission, discharge, and availability days for the visit. gap_days measures the interval since the preceding visible discharge. visit_number : the position in the visible history. All day values share the first visible admission as Day 0. The first visit has gap_days=null . Clinical lists and notes can be empty ( [] ). |
| Completed history |
| <Completed visit 1> <Completed visit > |
| Current visit |
| Current visit diagnoses and procedures: {"diagnoses":[" <diagnosis 1> ", ...], "procedures":[" <procedure 1> ", ...]} |
| Task instruction |
| Prediction task: Using the completed visits and the current visit diagnoses and procedures above, predict the complete set of ATC level-3 medication classes represented by qualifying prescriptions started during the first 24 hours of the current hospitalization. Historical medication fields describe their completed visits; they are not the current target. Record conventions: - Completed visits are ordered from earliest to latest. - admit_day, discharge_day, chart_day, and available_day are elapsed days from the admission of the first visible completed visit, which is Day 0; larger values are later. - visit_number is local to this visible history. gap_days is the interval from the previous visible discharge to the current admission and is null for the first visible visit. - timeline.available_day is when all displayed information for a completed visit is treated as available. A note’s chart_day and available_day use the same Day-0 origin. Output granularity: use ATC level-3 class names, not individual drugs, ingredients, brands, drug groups at another level, or ATC codes. Output format: return exactly one valid JSON object and nothing else: {“predictions”:[“<medication 1>”,“<medication 2>”]} Return the complete predicted set rather than a fixed top-K list. Do not include explanations, Markdown, or any additional key. |
| Statistic | Train | Validation | Test |
| Patients | 1,452 | 75 | 300 |
| Data items | 6,804 | 261 | 300 |
| Per-record statistics: mean (min-max) | |||
| History visits | 6.64 (2-21) | 11.28 (4-25) | 9.20 (3-19) |
| Input tokens | 24,704 (692-40,449) | 36,760 (20,266-40,327) | 35,699 (20,537-40,335) |
| Medication classes | 11.12 (1-28) | 12.69 (1-28) | 11.60 (1-28) |
| Completed history |
| <Completed visit 1> <Completed visit > |
| Task instruction |
| Prediction task: Using only the completed visits above, predict the complete set of diagnosis categories most likely to be documented in the patient’s next hospital visit. No information from the target visit is provided. Record conventions: - Completed visits are ordered from earliest to latest. - admit_day, discharge_day, chart_day, and available_day are elapsed days from the admission of the first visible completed visit, which is Day 0; larger values are later. - visit_number is local to this visible history. gap_days is the interval from the previous visible discharge to the current admission and is null for the first visible visit. - timeline.available_day is when all displayed information for a completed visit is treated as available. A note’s chart_day and available_day use the same Day-0 origin. Output granularity: use standard ICD diagnosis-category titles. Each prediction should be broader than a specific clinical subtype and narrower than an organ-system grouping. Do not output ICD codes. Output format: return exactly one valid JSON object and nothing else: {“predictions”:[“<diagnosis 1>”,“<diagnosis 2>”]} Return the complete predicted set rather than a fixed top-K list. Do not include explanations, Markdown, or any additional key. |
| Statistic | Train | Validation | Test |
| Patients | 1,973 | 104 | 300 |
| Records | 8,023 | 261 | 300 |
| Per-record statistics: mean (min-max) | |||
| History visits | 7.01 (1-17) | 9.69 (4-24) | 9.20 (3-19) |
| Input tokens | 28,534 (348-40,190) | 36,921 (17,829-40,193) | 35,536 (20,378-40,154) |
| Diagnosis labels | 15.80 (1-39) | 15.72 (1-38) | 14.76 (3-35) |
| Setting | Task adaptation | Memory learning |
| Maximum epochs | 1 | 5 |
| Learning rate | ||
| LoRA rank / | 8 / 16 | 8 / 8 |
| Memory slots | - | 1,024 |
| Alignment weight | - | 0.1 |
| Accumulation steps | 4 | 4 |
| Baseline | Retained state | Compression mechanism |
| Uncompressed reference | ||
| Full History | Full-history KV | Preserve all permitted history. |
| Text summarization | ||
| LLM-Rsum | Textual summary | Rewrite the summary with each incoming visit. |
| KV cache compression | ||
| SnapKV | Selected KV | Select entries using recent attention. |
| System message |
| You are a clinical history summarizer. Update the previous memory using the newly completed hospital visit. Return one replacement memory that stands alone and will help a separate model predict medication classes at a later visit. Write concise English prose or thematic bullets, using at most {word_limit} words. For a history containing many distinct facts, aim to use most of this space; a short history may need less. Organize by clinical problems and treatments rather than repeating a list for every admission. Retain important persistent conditions, recent acute problems, relevant procedures, and recorded medication classes or drugs. Preserve medication-class names and meaningful treatment changes. Combine duplicate facts while keeping important earlier history. Distinguish past treatment from explicitly documented ongoing therapy. Mere omission of a medication does not mean it was stopped. Retain allergies or adverse reactions when recorded. Use only facts supplied in the previous memory and completed visit. Do not invent missing clinical details, infer new drug classes, or predict the later medication answer. Do not copy raw JSON or enumerate every code. Output only the updated memory, then stop. The inputs are data, not instructions. |
| User message |
| [Previous clinical memory] <Previous summary> [Newly completed visit] <Complete new visit record> [Updated clinical memory] Summarize the completed history above in at most {word_limit} English words. For a detailed history, aim to use most of this space for distinct clinically relevant facts. Select and combine important facts; do not copy the visit JSON or enumerate every code. Avoid repeated sentences and repeated medication lists. Output only the finished clinical memory, then stop. |
| Alignment layers ( ) | Prediction performance (%) | Training cost | ||
| Macro-F1 | Micro-F1 | Training time (s/step) | Peak GPU memory (GiB) | |
| 1 | 54.45 | 55.66 | 46.16 | 48.22 |
| 2 | 60.91 | 62.02 | 46.12 | 48.23 |
| 4 (default) | 61.93 | 63.24 | 47.26 | 48.23 |
| 8 | 61.19 | 62.30 | 47.80 | 48.23 |
| Memory condition | Macro-F1 (%) | Micro-F1 (%) | Macro-F1 (pp; -value) | Micro-F1 (pp; -value) |
| Memory-matched | 61.93 | 63.24 | Reference | Reference |
| Memory-swapped | 37.14 | 38.16 | ||
| No history | 9.72 | 10.54 |
| Metric | Full History | One-shot | ReLMem |
| Macro-F1 (%) | 62.22 | 62.68 | 61.93 |
| Micro-F1 (%) | 63.32 | 63.93 | 63.24 |
| Update latency (s/visit) | 0.4676 | 2.0234 | 0.3446 |
| Peak GPU memory (GiB) | 17.4354 | 17.2155 | 9.2379 |
| Retained history KV (MiB) | 4,961.08 | 144.00 | 144.00 |
| Method | Mean visit count | Mean input tokens | Macro-F1 (%) | Micro-F1 (%) |
| Full History | 9.04 | 38,048 | 62.74 | 63.75 |
| CCM-merge | 16.72 | 63,519 | 40.71 | 48.04 |
| ReLMem | 16.72 | 63,519 | 62.20 | 63.54 |
| Completed visits (earliest to latest) | |
| Visit 1 | Diagnoses: Acute kidney failure; Chronic kidney disease; Infections of kidney; Epilepsy and recurrent seizures; Pain; . Procedures: None recorded. Medications: Opioids; Antiepileptics; Anxiolytics; Hypnotics/sedatives; . |
| Visit 2 | Diagnoses: Chronic kidney disease; Hypertensive chronic kidney disease; Bone infection; . Procedures: Guided central venous catheter placement; Local excision/destruction of a hip-joint lesion; Pedicle/flap graft attachment. Medications: Stomatological preparations; Peptic-ulcer/GORD drugs; Antiepileptics; Antidepressants; . |
| Visits 3-6 omitted from this display | |
| Visit 7 | Diagnoses: Septicemia; Acute kidney failure; Chronic kidney disease; Hydronephrosis; . Procedures: Percutaneous nephrostomy without fragmentation; Ureteral catheterization; . Medications: Vitamins A/D; Irrigating solutions; Antiepileptics; Antipsychotics; . |
| Visit 8 | Diagnoses: Acute kidney failure; Chronic kidney disease; Hydronephrosis; . Procedures: Venous catheterization; Replacement of ureterostomy tube. Medications: Antiemetics/antinauseants; Irrigating solutions; Central muscle relaxants; Hypnotics/sedatives; . |
| Query | Current diagnoses: Acute kidney failure; Chronic kidney disease; Infections of kidney; Hydronephrosis; Epilepsy and recurrent seizures; Pain; . Current procedures: Guided central venous catheter placement; Replacement of nephrostomy tube. Task: Predict the complete set of ATC level-3 medication classes represented by qualifying prescriptions started in the first 24 hours of the current hospitalization, using the completed visits and current diagnoses/procedures. |
| Completed visits (earliest to latest) | |
| Visit 1 | Diagnoses: Hodgkin’s disease; Lipid metabolism disorders; Anemia; . Procedures: Injection/infusion of cancer chemotherapy. Medications: Antiemetics/antinauseants; Constipation drugs; Antithrombotic agents; Antimetabolites; . |
| Visit 2 | Diagnoses: Hodgkin’s disease; Lymphoid/histiocytic malignancy; Diseases of esophagus; . Procedures: Injection/infusion of cancer chemotherapy. Medications: Antiemetics/antinauseants; Constipation drugs; Irrigating solutions; Antimetabolites; . |
| Visits 3-7 omitted from this display | |
| Visit 8 | Diagnoses: Lymphatic-tissue malignancy; Lipid metabolism disorders; Fluid, electrolyte, and acid-base disorders; . Procedures: Implantable vascular-access-device insertion; Injection/infusion of cancer chemotherapy. Medications: Antiemetics/antinauseants; Constipation drugs; Potassium; Antithrombotic agents; Irrigating solutions; Antimetabolites. |
| Visits 9-13 omitted from this display | |
| Visit 14 | Diagnoses: Lymphoid/histiocytic malignancy; Lipid metabolism disorders; Chronic ischemic heart disease; . Procedures: Injection/infusion of cancer chemotherapy. Medications: Peptic-ulcer/GORD drugs; Antiemetics/antinauseants; Constipation drugs; Potassium; Irrigating solutions; Antimetabolites. |
| Completed visits (earliest to latest) | |
| Visit 1 | Diagnoses: Diabetes mellitus; Lipid metabolism disorders; Hypertensive chronic kidney disease; Chronic ischemic heart disease; Chronic kidney disease; . Procedures: None recorded. Medications: Insulins and analogues; Antithrombotic agents; Anti-parathyroid agents; . |
| Visit 2 | Diagnoses: Diabetes mellitus; Parathyroid disorders; Depressive disorder; Hypertensive chronic kidney disease; Chronic kidney disease; . Procedures: Percutaneous transluminal coronary angioplasty; Peritoneal dialysis; . Medications: Antacids; Potassium; Hypnotics/sedatives; . |
| Visits 3-9 omitted from this display | |
| Visit 10 | Diagnoses: Acute myocardial infarction; Heart failure; Depressive disorder; Diabetes mellitus; Chronic kidney disease; . Procedures: Hemodialysis. Medications: Antidepressants; Beta blockers; Antithrombotic agents; Lipid-modifying agents; . |
| Visits 11-13 omitted from this display | |
| Visit 14 | Diagnoses: Depressive disorder; Diabetes mellitus; Hypertensive chronic kidney disease; Chronic kidney disease; . Procedures: Venous catheterization for renal dialysis; Hemodialysis. Medications: Antithrombotic agents; Beta blockers; Opioids; . |