MedCode trains an LLM to identify the appropriate medical calculator, extract its inputs, and generate an executable script instead of directly producing a numerical answer. A deterministic interpreter runs the script and returns the value, explanation, and unit after a single model decoding pass.
Training combines supervised examples derived from MedCalc-Bench with preference pairs formed by matching model-generated erroneous scripts to correct scripts. A weighted DPO method gives more emphasis to difficult pairs, such as scripts differing only in an argument, coefficient, or unit conversion.
Across LLaMA3-8B, Qwen2.5-7B, and Mistral-7B, the approach delivers absolute accuracy gains of 20 to 30 percentage points. The result is relevant to high-stakes LLM workflows where contextual interpretation is needed but arithmetic must be exact.