Context : Mass loss plays a dominant role in the evolution of low mass stars while they are on the Asymptotic Giant Branch ( AGB ) . The gas and dust ejected during this phase are a major source in the mass budget of the interstellar medium . Recent studies have pointed towards the importance of variations in the mass-loss history of such objects . Aims : By modelling the full line profile of low excitation CO lines emitted in the circumstellar envelope , we can study the mass-loss history of AGB stars . Methods : We have developed a non-LTE radiative transfer code , which calculates the velocity structure and gas kinetic temperature of the envelope in a self-consistent way . The resulting structure of the envelope provides the input for the molecular line radiative calculations which are evaluated in the comoving frame . The code allows for the implementation of modulations in the mass-loss rate . This code has been benchmarked against other radiative transfer codes and is shown to perform well and efficiently . Results : We illustrate the effects of varying mass-loss rates in case of a superwind phase . The model is applied to the well-studied case of VY CMa . We show that both the observed integrated line strengths as the spectral structure present in the observed line profiles , unambiguously demonstrate that this source underwent a phase of high mass loss ( \sim 3.2 \times 10 ^ { -4 } M _ { \odot } yr ^ { -1 } ) some 1000 yr ago . This phase took place for some 100 yr , and was preceded by a low mass-loss phase ( \sim 1 \times 10 ^ { -6 } M _ { \odot } yr ^ { -1 } ) taking some 800 yr . The current mass-loss rate is estimated to be in the order of 8 \times 10 ^ { -5 } M _ { \odot } yr ^ { -1 } . Conclusions : In this paper , we demonstrate that both the relative strength of the CO rotational line profiles and the ( non ) -occurrence of spectral structure in the profile offer strong diagnostics to pinpoint the mass-loss history .