cs.AISep 25, 2026

Improving Medical Calculation of LLMs with Embedded Coding

Authors: Tianshi Ming, Yingying Zhang, Xian Wu

Organizations: Carnegie Mellon University · Tencent YouTu Lab

Abstract

Large Language Models (LLMs) perform well on medical examinations and question-answering benchmarks, but remain unreliable on medical calculation tasks that require exact numerical outputs. These calculations support high-stakes decisions such as medication dosing, organ-function assessment, and prognostic scoring, for which even small errors can have serious clinical consequences. We introduce MedCode, a framework that improves medical calculation by training LLMs to generate embedded executable code. Given a clinical context, the model identifies the relevant calculator, extracts its input variables, and produces a script that delegates arithmetic operations to a deterministic interpreter. Executing the script returns the calculated value together with an explanation and the appropriate unit. We construct supervised fine-tuning (SFT) and preference datasets from the MedCalc benchmark and additionally curate a dataset for calculation tasks in Intensive Care Unit (ICU) scenarios. We further propose weighted Direct Preference Optimization (wDPO), which adaptively emphasizes preference pairs that are difficult for the model to distinguish. Experiments with LLaMA3-8B, Qwen2.5-7B, and Mistral-7B show absolute accuracy gains of 20--30 percentage points, demonstrating the effectiveness of embedded code generation for medical calculation.

Figures & tables

Appendix figures & tables11 assets

Supplementary material from the paper’s appendix.

Appendix

Explore similar work

CardsList
  1. MedCalc-Pro: Solving Complex Medical Calculations with LLM Agents

    Jul 3, 2026Siran Zhao, Ruihui Hou, Ziyue Huai +2LLM Agent EvaluationLLM Tool Use

  2. Tool-Augmented Large Language Models for Reliable Medical Calculation from Patient Notes

    Oct 15, 2026Yang Zhong, Ling Luo, Xuanlyu Zhu +4LLM Tool UseClinical Decision Support

  3. Medmarks: A Comprehensive Open-Source LLM Benchmark Suite for Medical Tasks

    May 2, 2026Benjamin Warner, Ratna Sagari Grandhi, Max Kieffer +32LLM EvaluationHealthcare