Respiratory motion causes artifacts in cone-beam (CB) CT images acquired on slow rotating scanners integrated with linear accelerators. Respiration-correlated CBCT has been proposed to correct for the respiratory motion but only a subset of the CB projections is used to reconstruct each frame of the 4D CBCT image and, therefore, adequate image quality requires long acquisition times. In this article, the authors develop an on-the-fly solution to estimate and compensate for the respiratory motion in the reconstruction of a 3D CBCT image from all the CB projections. An a priori motion model of the patient respiratory cycle is estimated from the 4D planning CT. During the acquisition, the model is correlated with the respiration using a respiratory signal extracted from the CB projections. The estimated motion is next compensated for in an optimized reconstruction algorithm. The motion compensated for is forced to be null on average over the acquisition time to ensure that the compensation results in a CBCT image which describes the mean position of each organ, even if the a priori motion model is inaccurate. Results were assessed on simulated, phantom, and patient data. In all experiments, blur was visually reduced by motion-compensated CBCT. Simulations showed robustness to inaccuracies of the motion model observed on patient data such as amplitude variations, phase shifts, and setup errors, thus proving the efficiency of the compensation using an a priori motion model. Noise and view-aliasing artifacts were lower on motion-compensated CBCT images with 1 min scan than on respiration-correlated CBCT images with 4 min scan. Finally, on-the-fly motion estimation and motion-compensated reconstruction were within the acquisition time of the CB projections and the CBCT image available a few seconds after the end of the acquisition. In conclusion, the authors developed and implemented a method for correcting the respiratory motion during the treatment fractions which can replace respiration-correlated CBCT for improving image quality while decreasing acquisition time.