{"skill":{"id":"b2745926-ab8f-4ccf-9479-678d30bc2d80","name":"Clinical-Discharge-Summary-Agent","author":"Anshu Singh","description":"An intelligent clinical AI system that reads patient records, audits medications, flags safety concerns, and generates structured discharge summaries learning and improving from every clinician correction.","source_type":"content","source_url":null,"content":"# Clinical Discharge Summary Agent\r\n\r\nRead a patient's clinical PDF records and produce a structured, clinician-ready discharge summary — with medication reconciliation, pending-result flags, and a self-improving feedback loop. The agent never invents clinical facts; any field it cannot source from the documents is explicitly marked `missing` and flagged for clinician review.\r\n\r\n## Base URL\r\nhttps://clinical-discharge-summary-agent-production.up.railway.app\r\n\r\n## Endpoints\r\n\r\n---\r\n\r\n### POST /api/set-api-key\r\nInject a cloud LLM API key for this session so the agent uses a live model instead of the offline local transformer. Supports OpenAI (`sk-`), Anthropic (`sk-ant-`), Google Gemini (`AIzaSy`), and Groq (`gsk_`). The key is stored only in the server process — not persisted to disk.\r\n\r\nExample:\r\n  curl -X POST \"https://clinical-discharge-summary-agent-production.up.railway.app/api/set-api-key\" \\\r\n    -H \"Content-Type: application/json\" \\\r\n    -d '{\"api_key\": \"AIzaSy...\"}'\r\n\r\nResponse:\r\n  { \"status\": \"success\", \"message\": \"API key saved for this session.\" }\r\n\r\n---\r\n\r\n### GET /api/config-status\r\nCheck which LLM provider and model are currently active, and whether a live API key is configured.\r\n\r\nExample:\r\n  curl \"https://clinical-discharge-summary-agent-production.up.railway.app/api/config-status\"\r\n\r\nResponse:\r\n  { \"has_key\": false, \"provider\": \"local_transformers\", \"model\": \"google/flan-t5-base\", \"is_live\": true }\r\n\r\n---\r\n\r\n### POST /api/upload-pdf\r\nUpload a clinical PDF (text-layer or scanned/handwritten). The server automatically detects patients in the document, segments pages by patient, runs multi-engine OCR on image pages, and returns the extracted text per patient ready for the agent.\r\n\r\nExample:\r\n  curl -X POST \"https://clinical-discharge-summary-agent-production.up.railway.app/api/upload-pdf\" \\\r\n    -F \"file=@patient_record.pdf\"\r\n\r\nResponse:\r\n  {\r\n    \"status\": \"success\",\r\n    \"patients\": {\r\n      \"John Smith\": {\r\n        \"preview\": \"Admitted: 12-Jan-2025. Diagnosis: Acute...\",\r\n        \"full_length\": 4821\r\n      }\r\n    }\r\n  }\r\n\r\n---\r\n\r\n### GET /api/run-full-pipeline?patient_name={name}\r\nRun the complete 3-iteration pipeline for one extracted patient:\r\n  1. ReAct agent loop -> structured discharge draft\r\n  2. Simulated doctor review -> edit distance measured\r\n  3. Correction rules extracted -> injected into next iteration\r\n\r\nThe patient must have been uploaded via /api/upload-pdf first. URL-encode spaces (%20).\r\n\r\nExample:\r\n  curl \"https://clinical-discharge-summary-agent-production.up.railway.app/api/run-full-pipeline?patient_name=John%20Smith\"\r\n\r\nResponse (abbreviated):\r\n  {\r\n    \"patient_name\": \"John Smith\",\r\n    \"iterations\": [\r\n      {\r\n        \"iteration\": 1,\r\n        \"draft\": {\r\n          \"patient_name\": \"John Smith\",\r\n          \"principal_diagnosis\": \"Acute Kidney Injury (Stage 2)\",\r\n          \"medications_on_discharge\": [\"Furosemide 40mg OD\", \"Amlodipine 5mg OD\"],\r\n          \"pending_results\": [\"Urine culture — result pending at discharge\"],\r\n          \"clinical_safety_flags\": [\"Creatinine trending up — monitor closely\"],\r\n          \"follow_up_instructions\": \"Nephrology review in 1 week. Repeat renal panel.\",\r\n          \"missing_fields\": []\r\n        },\r\n        \"edit_distance\": 0.38,\r\n        \"total_steps\": 8,\r\n        \"correction_memory\": [\"Always include ICD-10 code after diagnosis\"]\r\n      },\r\n      { \"iteration\": 2, \"edit_distance\": 0.0 },\r\n      { \"iteration\": 3, \"edit_distance\": 0.0 }\r\n    ],\r\n    \"final_correction_memory\": [\"Always include ICD-10 code after diagnosis\"]\r\n  }\r\n\r\n---\r\n\r\n### POST /api/run-agent\r\nRun a single pass of the ReAct agent loop for one patient (no doctor review or learning). Useful for testing extraction on raw text.\r\n\r\nExample:\r\n  curl -X POST \"https://clinical-discharge-summary-agent-production.up.railway.app/api/run-agent\" \\\r\n    -H \"Content-Type: application/json\" \\\r\n    -d '{\r\n      \"patient_name\": \"John Smith\",\r\n      \"raw_text\": \"Admitted 12-Jan-2025 with AKI. BP 145/90. Creatinine 3.2 mg/dL...\",\r\n      \"feedback_memory\": []\r\n    }'\r\n\r\nResponse:\r\n  {\r\n    \"patient_id\": \"John Smith\",\r\n    \"final_draft\": { \"principal_diagnosis\": \"Acute Kidney Injury\", \"...\" : \"...\" },\r\n    \"execution_trace\": [\r\n      { \"step\": 1, \"reasoning\": \"Check medication reconciliation first.\", \"action\": \"MedicationReconciliation\", \"result\": \"No omissions detected.\" }\r\n    ],\r\n    \"total_steps_executed\": 6\r\n  }\r\n\r\n---\r\n\r\n### POST /api/run-doctor-review\r\nApply the simulated clinician review policy to an existing draft. Returns the edited draft with style corrections applied.\r\n\r\nExample:\r\n  curl -X POST \"https://clinical-discharge-summary-agent-production.up.railway.app/api/run-doctor-review\" \\\r\n    -H \"Content-Type: application/json\" \\\r\n    -d '{\"draft\": { \"patient_name\": \"John Smith\", \"principal_diagnosis\": \"AKI\", \"...\" : \"...\" }}'\r\n\r\nResponse: the edited DischargeSummaryDraft object.\r\n\r\n---\r\n\r\n### POST /api/run-learning\r\nRegister one iteration's edit distance and extract correction rules from the draft vs. doctor-edited diff.\r\n\r\nExample:\r\n  curl -X POST \"https://clinical-discharge-summary-agent-production.up.railway.app/api/run-learning\" \\\r\n    -H \"Content-Type: application/json\" \\\r\n    -d '{\r\n      \"patient_name\": \"John Smith\",\r\n      \"draft_diagnosis\": \"AKI\",\r\n      \"draft_followup\": \"Follow up in 1 week.\",\r\n      \"edited_diagnosis\": \"Acute Kidney Injury (N17.9)\",\r\n      \"edited_followup\": \"Nephrology review in 7 days. Repeat renal function panel.\",\r\n      \"draft\": {},\r\n      \"edited\": {}\r\n    }'\r\n\r\nResponse:\r\n  {\r\n    \"edit_distance\": 0.385,\r\n    \"new_rules\": [\"Always append ICD-10 code in parentheses after the diagnosis name.\"],\r\n    \"correction_memory\": [\"Always append ICD-10 code in parentheses after the diagnosis name.\"],\r\n    \"all_distances\": { \"John Smith\": [0.385] }\r\n  }\r\n\r\n---\r\n\r\n### GET /api/drafts\r\nList all discharge summary drafts saved to disk from previous full-pipeline runs.\r\n\r\nExample:\r\n  curl \"https://clinical-discharge-summary-agent-production.up.railway.app/api/drafts\"\r\n\r\nResponse:\r\n  {\r\n    \"drafts\": [\r\n      { \"filename\": \"John_Smith_draft.json\", \"data\": { \"principal_diagnosis\": \"...\" } }\r\n    ]\r\n  }\r\n\r\n---\r\n\r\n### GET /api/traces\r\nList all saved ReAct agent execution traces (full step-by-step reasoning logs).\r\n\r\nExample:\r\n  curl \"https://clinical-discharge-summary-agent-production.up.railway.app/api/traces\"\r\n\r\nResponse:\r\n  { \"traces\": [ { \"filename\": \"John_Smith_trace.json\", \"data\": [ ... ] } ] }\r\n\r\n---\r\n\r\n### GET /api/learning-curve\r\nReturn the edit-distance learning curve data for all patients — ready to render on a chart.\r\n\r\nExample:\r\n  curl \"https://clinical-discharge-summary-agent-production.up.railway.app/api/learning-curve\"\r\n\r\nResponse:\r\n  { \"learning_data\": { \"John Smith\": [0.385, 0.0, 0.0] } }\r\n\r\n---\r\n\r\n## How the agent should use this\r\n\r\n1. Configure (optional): Call POST /api/set-api-key with a Gemini or OpenAI key to enable the live reasoning model. Without a key the system runs fully offline using google/flan-t5-base.\r\n\r\n2. Ingest the PDF: Call POST /api/upload-pdf with the clinical PDF. The system handles text PDFs, scanned documents, and handwritten notes. The returned \"patients\" object maps each detected patient name to a text preview and character count.\r\n\r\n3. Run the full pipeline: For each patient name returned, call GET /api/run-full-pipeline?patient_name={name}. The agent will:\r\n   - Run a bounded ReAct reasoning loop (up to 10 steps) calling MedicationReconciliation, PendingResultsCheck, DiagnosticCheck, and FlagContradiction tools\r\n   - Produce a structured discharge draft with all missing fields marked explicitly as \"missing\"\r\n   - Simulate doctor review and measure the Normalized Levenshtein edit distance\r\n   - Extract correction rules and re-run for 3 iterations until the draft converges\r\n\r\n4. Read the result: From the pipeline response, read iterations[-1].draft for the final discharge summary. Check clinical_safety_flags for safety concerns. Check missing_fields for data the agent could not find in the source documents — these must be completed by the clinician.\r\n\r\n5. Track improvement: Call GET /api/learning-curve to get edit-distance values across iterations. A curve converging 0.38 -> 0.0 -> 0.0 confirms the agent learned the clinician's style.\r\n\r\n6. Retrieve saved outputs: Call GET /api/drafts and GET /api/traces to retrieve persisted discharge summaries and full reasoning traces for audit or compliance purposes.\r\n\r\nNo-fabrication guarantee: Every field the agent cannot source from the uploaded documents is set to \"missing\" or \"undocumented\" and listed in missing_fields. The agent never invents clinical facts. The output is always a draft for clinician review, never an auto-finalized document.","endpoints":"GET  https://clinical-discharge-summary-agent-production.up.railway.app/api/config-status\r\n\r\nPOST https://clinical-discharge-summary-agent-production.up.railway.app/api/set-api-key\r\n\r\nPOST https://clinical-discharge-summary-agent-production.up.railway.app/api/upload-pdf\r\n\r\nPOST https://clinical-discharge-summary-agent-production.up.railway.app/api/run-agent\r\n\r\nPOST https://clinical-discharge-summary-agent-production.up.railway.app/api/run-doctor-review\r\n\r\nPOST https://clinical-discharge-summary-agent-production.up.railway.app/api/run-learning\r\n\r\nGET  https://clinical-discharge-summary-agent-production.up.railway.app/api/run-full-pipeline\r\n\r\nGET  https://clinical-discharge-summary-agent-production.up.railway.app/api/drafts\r\n\r\nGET  https://clinical-discharge-summary-agent-production.up.railway.app/api/traces\r\n\r\nGET  https://clinical-discharge-summary-agent-production.up.railway.app/api/learning-curve","tags":"Hospital, AI agent","reachable":null,"created_at":"2026-06-21T08:29:14.123Z"}}