Aims. We present a new sub-grid model, HYACINTH -- HYdrogen And Carbon chemistry in the INTerstellar medium in Hydro simulations -- for computing the non-equilibrium abundances of H2 and its carbon-based tracers, namely CO, C, and C+, in cosmological simulations of galaxy formation. Methods. The model accounts for the unresolved density structure in simulations using a variable probability distribution function of sub-grid densities and a temperature-density relation. Included is a simplified chemical network that has been tailored for hydrogen and carbon chemistry within molecular clouds and easily integrated into large-scale simulations with minimal computational overhead. As an example, we applied HYACINTH to a simulated galaxy at redshift z∼2.5 in post-processing and compared the resulting abundances with observations. Results. The chemical predictions from HYACINTH are in reasonable agreement with high-resolution molecular-cloud simulations at different metallicities. By post-processing a galaxy simulation with HYACINTH, we reproduced the HI−H2 transition as a function of the hydrogen column density NH for both Milky-Way-like and Large-Magellanic-Cloud-like conditions. Column density maps reveal that CO is concentrated in the peaks of the H2 distribution, while atomic carbon more broadly traces the bulk of H2 in our post-processed galaxy. Based on both the column density maps and the surface density profiles of the different gas species in the post-processed galaxy, we find that C+ maintains a substantially high surface density out to ∼10kpc as opposed to other components that exhibit a higher central concentration. This is similar to the extended [CII] emission found in some recent observations at high redshifts.