Context. In the dense and cold prestellar cores, many species freeze out onto grains to form ices. The most conspicuous case is that of CO itself. Only upper limits of this depletion amplitude can be estimated because the CO emission from the external undepleted layers mask the emission of CO left inside the depleted region. The finite signal-to-noise ratio of the observations is another limitation. However, depletion and even more desorption mechanisms are not well-known and need observational constraints, i.e., depletion profiles. Aims. We describe a method for retrieving the CO and N 2 abundance profiles inside prestellar cores, which is mostly free of initial conditions. Methods. DCO + is a daughter molecule of CO, which appears inside depleted prestellar cores. The main deuteration partners are the H + 3 isotopologues. By determining the abundance of these isotopologues via N 2 D + , N 2 H + , and ortho-H 2 D + observations and a chemical model, we can uniquely constrain the CO abundance, the only free parameter left, to fit the observed DCO + abundance. The N 2 abundance is also determined in the same manner once CO is known. DCO + -H 2 collisional rates including the hyperfine structure were computed in order to determine the DCO + abundance. Results. To illustrate the method, we apply it to the main L183 prestellar core and find that the CO abundance profile varies from ≥2.4 × 10 −5 at the core edge to ≤6.6 × 10 −8 at the center. This represents a relative decrease in abundance by ≥360, and by ≥2000 compared to the standard undepleted CO abundance (1-2 × 10 −4 ). Comparatively, N 2 abundance decreases much less, from ≤3.7 × 10 −7 down to ∼2.9 × 10 −8 , in contrast to the similar binding properties of the two species. Because the N 2 abundance is lower than its steady state value at the edge, while CO is close to its own, a possible explanation is that N 2 is still in its production phase in competition with depletion. Conclusions. The method allows the CO and N 2 abundance profiles to be retrieved in the depleted zone both without needing extremely high signal-to-noise observations and free of masking effects by extended emission from the cloud envelope. The main uncertainties are linked to the N 2 H + collisional rates and somewhat to the H + 3 isotopologue rates, both collisional and chemical, but hardly to the initial conditions of the model. This method opens up possibilities of testing depletion and desorption mechanisms in prestellar cores and time evolution models, and of addressing the debated CO/N 2 depletion controversy.