Each clinical concept that a large language model must detect needs its own prompt, and in most clinical AI projects a person writes and tunes that prompt by hand. Pythia automates this step for any concept with an annotated evaluation set. The engine behind our studies of clinical symptom detection and optimization instability is now open source, with documentation at clai-group.github.io/Pythia and code on GitHub.

The optimization loop

Pythia's four-step optimization loop: evaluate, analyze errors, improve, and control

Pythia runs a multi-agent loop built on LangGraph, a library for stateful agent workflows. In each iteration, an evaluation step runs the current prompt against a held-out evaluation set and records its score. A specialist model then reviews the failures in aggregate, identifies recurring error patterns, and proposes a cause for each. Working from that analysis, an improver agent drafts several candidate prompts and ranks them before the strongest one returns to evaluation. A controller closes the loop by deciding whether to continue, return to the best earlier prompt, or stop at convergence.

Backtracking is the design choice we consider most consequential for clinical use. When a new iteration scores below the best prompt so far, Pythia restores that best version and resumes from it. This safeguard follows from our earlier finding that autonomous optimization can degrade performance while standard metrics report improvement. Pythia also logs each prompt version, score, and controller decision, so a team can audit, compare, export, or roll back any run.

Illustrative Pythia run in which the controller backtracks after a lower-scoring fourth iteration

The project site shows this behavior in an illustrative run. The score rose from 0.612 at baseline to 0.741 in the second iteration and 0.863 in the third. When the fourth iteration fell to 0.847, the controller restored the third-iteration prompt.

Designed for clinical deployment

Two features adapt Pythia to clinical settings. The first concerns the objective: optimization targets sensitivity and specificity thresholds set by the user, the operating characteristics that clinicians already apply to screening tests. The second concerns location, because Pythia runs on open-weights models from one billion to one trillion parameters hosted within the institution, which keeps protected health information on local infrastructure.

A typical call names the model backend, the development and validation datasets, an initial prompt, an optional standard operating procedure, a maximum number of iterations, and the two thresholds. The example on the project site uses ten iterations and thresholds of 0.7 for both sensitivity and specificity, and the selected prompt returns as an attribute of the result. The GitHub README documents installation and each parameter.

The National Institute on Aging (R01AG074372, RF1AG074372) and the National Institute of Allergy and Infectious Diseases (R01AI165535) supported the development of Pythia.