We introduce a scheme for deriving an optimally-parametrised Langevin dynamics of few collective variables from data generated in molecular dynamics simulations. The drift and the position-dependent diffusion profiles governing the Langevin dynamics are expressed as explicit averages over the input trajectories. The proposed strategy is applicable to cases when the input trajectories are generated by subjecting the system to a external time-dependent force (as opposed to canonically-equilibrated trajectories). Secondly, it provides an explicit control on the statistical uncertainty of the drift and diffusion profiles. These features lend to the possibility of designing the external force driving the system so to maximize the accuracy of the drift and diffusions profile throughout the phase space of interest. Quantitative criteria are also provided to assess a posteriori the satisfiability of the requisites for applying the method, namely the Markovian character of the stochastic dynamics of the collective variables.