TrajOnco: a multi-agent framework for temporal reasoning over longitudinal EHR for multi-cancer early detection
Authors: Sihang Zeng, Young Won Kim, Wilson Lau, Ehsan Alipour, Ruth Etzioni, Meliha Yetisgen, Anand Oka
Organizations: Truveta Inc., Bellevue, WA, United States. · University of Washington, Seattle, WA, United States. · Fred Hutch Cancer Center, Seattle, WA, United States.
Accurate estimation of cancer risk from longitudinal electronic health records (EHRs) could support earlier detection and improved care, but modeling such complex patient trajectories remains challenging. We present TrajOnco, a training-free, multi-agent large language model (LLM) framework designed for scalable multi-cancer early detection. Using a chain-of-agents architecture with long-term memory, TrajOnco performs temporal reasoning over sequential clinical events to generate patient-level summaries, evidence-linked rationales, and predicted risk scores. We evaluated TrajOnco on de-identified Truveta EHR data across 15 cancer types using matched case-control cohorts, predicting risk of cancer diagnosis at 1 year. In zero-shot evaluation, TrajOnco achieved AUROCs of 0.64-0.80, performing comparably to supervised machine learning in a lung cancer benchmark while demonstrating better temporal reasoning than single-agent LLMs. The multi-agent design also enabled effective temporal reasoning with smaller-capacity models such as GPT-4.1-mini. The fidelity of TrajOnco's output was validated through human evaluation. Furthermore, TrajOnco's interpretable reasoning outputs can be aggregated to reveal population-level risk patterns that align with established clinical knowledge. These findings highlight the potential of multi-agent LLMs to execute interpretable temporal reasoning over longitudinal EHRs, advancing both scalable multi-cancer early detection and clinical insight generation.
Figures & tables
Figure 1: TrajOnco framework and study design. a. TrajOnco uses sequential worker agents, long-term memory, and a manager agent to process longitudinal EHR and produce predicted risk with a patient-level summary. b. In this early detection task, EHR data from the start of records to the prediction time were used to predict risk of cancer diagnosis at 1 year. Two cancer diagnosis codes within 2 months were used to define time of cancer diagnosis. c. Patient-level summaries can be aggregated to generate population-level insights.
Figure 2: Zero-shot prediction performance of TrajOnco. a. Area under receiver operating characteristic (AUROC) of cancer-specific prediction on each cancer type; b. ROC curves and c. precision-recall curves for different methods on the lung cancer benchmark.
Figure 3: Sensitivity analyses on the scalability and efficiency of TrajOnco. a. AUROC and median runtime of TrajOnco on the lung cancer benchmark using different base models. The radius of each point is proportional to the API price of the input token of the model. The pink dashed line represents the Pareto front. Among these base models, GPT-4.1-mini achieved a balance between performance, latency, and cost. b. Improved predictive performance when expanding the maximum length of left-truncated patient trajectory. c. LLM-as-a-judge comparison between one-stage TrajOnco and two-stage TrajOnco. The one-stage model had better clinical reasoning, completeness of detail, and temporal reasoning than the two-stage model. d. The two-stage model achieved lower latency especially for very long EHR sequences.
Figure 4: LLM-as-a-judge evaluation of TrajOnco versus a single-agent baseline. a, Pairwise comparison across five dimensions using GPT-5 with high reasoning effort as the judge. Bars show the proportions of TrajOnco wins (blue), ties (gray), and single-agent wins (green). TrajOnco outperforms the baseline across all dimensions, with the largest advantage in Temporal Reasoning (68% wins vs. 23% losses). b, TrajOnco win rate by EHR length (input tokens; log scale) for selected dimensions. Solid lines indicate smoothed win rates and shaded areas indicate confidence intervals. Each scattered point represents the average win rate over two runs with different candidate orders. For each run, “win” assigns a score of 1.0, “tie” assigns a score of 0.5, and “loss” assigns a score of 0.0.
Figure 5: Dynamic short-term risk prediction and event aggregation by TrajOnco worker agents. The top panel displays the patient’s raw EHR data, encompassing observations, medications, lab results, and conditions from 2012 to 2020. Different temporal chunks processed sequentially by the worker agents are distinguished by alternating background colors. The bottom panel shows TrajOnco’s continuously updated short-term risk prediction corresponding to the timeline. Key clinical insights synthesized by the agents and stored as events in LTM are explicitly marked with diamond symbols along the risk trajectory. Two critical updates are highlighted: (1) In October 2012, an observation confirming ongoing active smoking shifts the baseline risk. (2) In April 2016, the discovery of multiple pulmonary nodules and mild COPD triggers a risk escalation to high, with the system’s memory specifically noting the need for malignancy risk stratification in a smoker, while remaining robust to alternative inflammatory etiologies such as autoimmune disease.
Figure 6: Thematic patterns of lung cancer-related events identified by TrajOnco among case patients. a. UMAP projection of cancer-related events output by TrajOnco, demonstrating coherent clustering of events into clinically interpretable themes across demographic factors, comorbid diagnoses, health behaviors, laboratory findings, medications, procedures, radiology, symptoms, and vital signs. b. Distribution of the most frequently identified events within each category, highlighting dominant population-level signals such as smoking exposure. Note that the counts were the mentions of all identified events within each category, which can be more than one for each patient.
Figure 7: Cancer-specific themes and relationships derived from patient summaries. a. UMAP of patient summaries colored by cancer type, showing that patients with the same cancer tend to cluster together. b. Heatmap of median cosine similarity between patients from pairwise cancer types, highlighting higher within-cancer similarity and relationships among related cancers. c. Top prevalent themes identified for each cancer type from patient summaries.
Appendix figures & tables15 assets
Supplementary material from the paper’s appendix.
Appendix
Figure 8: Model architecture of the two-stage approach.
Figure 9: Any-cancer prediction performance stratified by a. cancer type, b. screening availability, and c. age groups.
Figure 10: Population-level trajectories of lung cancer risk over time. Sankey diagram showing transitions in model-assigned lung cancer risk states across age, from no record, low, moderate, and high risk to diagnosis. Flow width indicates the number of patients following each trajectory, revealing dynamic shifts in risk over time and the dominant pathways leading to diagnosis. Insets summarize representative transitions and the clinical factors most commonly associated with risk escalation or de-escalation.
Figure 11: Sensitivity analysis on how the time gap between time of prediction and diagnosis affects predictive performance.
Figure 12: Top prevalent themes identified for each cancer type from patient summaries in the 3-year prediction task.
Initial worker system prompt
You are an expert clinical AI assistant specializing in {cancer_type} risk assessment from longitudinal EHR data. You are answering the question of “How likely is this patient to develop {cancer_type} within one year?” based on the provided EHR data chunk.
Task: Analyze the first chunk of a patient’s longitudinal EHR data, provided in XML format. Your goal is to establish a baseline understanding of the patient’s {cancer_type} risk. You should filter out any irrelevant information and focus solely on the clinical aspects that pertain to {cancer_type} risk assessment.
Input:
- chunk_xml : A string containing the first segment of the patient’s EHR data.
Instructions:
1. Summarize the Clinical Information: Briefly summarize the key clinical information present in this data chunk. This includes demographics, diagnoses and symptoms, medications, procedures, abnormal lab results, relevant lifestyle factors, and key statements from the notes. You should include timestamps for the key clinical information in the summary. Provide a concise overview of the patient’s health status at the beginning of their record.
Appendix
Table 2: Initial worker system prompt template for multi-cancer early detection.
Initial worker user prompt
Here is the first data chunk:
<chunk_xml>
{chunk_xml}
</chunk_xml>
Please provide the initial clinical summary and cancer risk assessment in JSON format.
Appendix
Table 3: Initial worker user prompt template for multi-cancer early detection.
Subsequent worker system prompt
You are an expert clinical AI assistant specializing in {cancer_type} risk assessment from longitudinal EHR data. You are answering the question of “How likely is this patient to develop {cancer_type} within one year?” based on the provided EHR data chunk and previous clinical summary.
Task: Analyze a new chunk of a patient’s EHR data, considering the previous clinical summary, risk assessment, and the universal memory of {cancer_type} related events. Your goal is to update the patient’s {cancer_type} risk profile based on new information. You should filter out any irrelevant information and focus solely on the clinical aspects that pertain to {cancer_type} risk assessment.
Input:
- previous_summary : A JSON object from the previous agent containing the summary, {cancer_type} related events, and risk assessment up to this point.
- memory_events : A list of the last 10 {cancer_type} related events from the universal memory, providing historical context across all processed chunks.
- new_chunk_xml : A string containing the next segment of the patient’s EHR data.
Appendix
Table 4: Subsequent worker system prompt template for multi-cancer early detection.
Subsequent worker user prompt
Previous Agent Output:
<previous_summary>
{previous_agent_output}
</previous_summary>
Memory Events (Last 10 from Universal Memory):
<memory_events>
Appendix
Table 5: Subsequent worker user prompt template for multi-cancer early detection.
Manager agent system prompt
You are a senior clinical AI expert specializing in longitudinal {cancer_type} risk analysis. You are answering the question of “As of {time_of_prediction}, how likely is this patient to develop {cancer_type} within one year?” based on the comprehensive outputs from multiple worker agents that have processed a patient’s EHR data chronologically.
Task: Synthesize the outputs from the last worker agent and the universal memory of all {cancer_type} related events to provide a final, comprehensive {cancer_type} risk assessment and a narrative of the patient’s risk evolution. You should filter out any irrelevant information and focus solely on the clinical aspects that pertain to {cancer_type} risk assessment.
Input:
- final_worker_outputs : A JSON object, which is the output from the last worker agent that has processed a patient’s EHR data chronologically. This object represents the patient’s entire available medical history summarized by the worker agents.
- universal_memory_events : A list of all {cancer_type} related events from the universal memory, providing complete historical context across all processed chunks.
Instructions:
Appendix
Table 6: Manager agent system prompt template for multi-cancer early detection.
Manager agent user prompt
All Worker Agent Outputs:
<final_worker_outputs>
{final_worker_outputs}
</final_worker_outputs>
Universal Memory Events (All Events):
<universal_memory_events>
Appendix
Table 7: Manager agent user prompt template for multi-cancer early detection.
LLM judge system prompt
You are an expert pulmonologist and clinical informatician. Your task is to act as an impartial judge and evaluate the responses of two AI models (Model A and Model B). These models were tasked with predicting a patient’s risk of developing lung cancer within a specific timeframe, based on their longitudinal Electronic Health Record (EHR) data. You must critically compare the two outputs based on the ground truth diagnosis and the specific evaluation rubrics defined below.
You will evaluate the models on five key dimensions. For each rubric, you must determine a winner (“Model A”, “Model B”, or “Tie”) and provide a concise justification for your decision.
1. Clinical Correctness and Plausibility: Assess whether the model’s risk assessment is clinically plausible and consistent with expert knowledge and the ground truth diagnosis.
2. Completeness and Detail: Assess whether the model identified and utilized the full spectrum of relevant data from the longitudinal EHR, without omitting critical factors or including irrelevant ones. Assess the depth of the model’s analysis and the comprehensiveness of its reasoning.
3. Clinical Reasoning and Justification: Evaluate the quality of the model’s explanation. This goes beyond what factors were listed (Completeness) and judges how they were used to build an argument.
4. Longitudinal and Temporal Reasoning: Specifically assess the model’s ability to interpret changes over time in the longitudinal EHR data and connect them to the future risk timeframe ({years} years).
Appendix
Table 8: LLM judge system prompt template.
LLM judge user prompt
The prediction problem is to predict whether a patient will be diagnosed with lung cancer within {years} years.
The ground truth diagnosis is: {diagnosis}
The model outputs are as follows:
Model A Output:
{model_a_output}
Model B Output:
Appendix
Table 9: LLM judge user prompt template.
Topic modeling theme generation system prompt
You are a clinical NLP analyst. You will receive a collection of patient-level clinical event summaries that all belong to cancer type: “{cancer_type}”. Each summary is a list of dated clinical events for one patient.
Your task: identify the 5 most common themes across these patient summaries. Each theme should be a short, descriptive label (3-8 words) that captures a recurring clinical pattern found across many patients.
Rules:
- Derive themes purely from the provided events; do not introduce outside knowledge.
- Themes must be mutually exclusive and collectively representative.
- Order themes from most to least common.
Appendix
Table 10: Topic modeling phase 1 system prompt for theme generation.
Topic modeling theme assignment system prompt
You are a clinical NLP assistant. Each patient below has cancer type “{cancer_type}”. The top-5 themes for this cancer type are:
{themes_list}
For every patient you receive, decide which themes (zero, one, or several) are mentioned in their summary. A patient can match multiple themes.
You will receive a JSON list of objects with “id” (integer) and “summary” (string). Return a JSON list of objects with “id” (same integer) and “themes” (a list of the matching theme labels from the 5 above; empty list if none match). Return ONLY the JSON list, no extra text.
Appendix
Table 11: Topic modeling phase 2 system prompt for theme assignment.
School of Computing and Data Science, The University of Hong Kong · 2Shenzhen Institute for Advanced Study, University of Electronic Science and Technology of China · School of Computer Science, The University of Sydney
Department of Surgery, Mayo Clinic, Rochester, MN, USA · Kern Center for the Science of Health Care Delivery and the Department of Surgery, Mayo Clinic, Rochester, MN, USA · Kern Center for the Science of Health Care Delivery and the Department of Artificial Intelligence and Informatics, Mayo Clinic, Rochester, MN, USA