The local-density and generalized gradient approximations ͑LDA and GGA͒ to density functional theory ͑DFT͒ exhibit incomplete error cancellation when energy differences are taken between chemically dissimilar systems. This energy inconsistency is manifested, e.g., in the tendency to underestimate the heat ͑enthalpy͒ of formation of semiconducting and insulating compounds in LDA and, even more so, in GGA. Considering a set of 61 compounds that can be formed from 14 elements ͑cations: Cu, Mg, Ca, Zn, Cd, Al, Ga, and In; anions: N, P, As, O, S, and Se͒, optimized elemental reference energies are determined by least-squares error minimization of an overdetermined set of linear equations. These elemental energies are "optimally consistent" with the DFT energies of the semiconductor compounds and imply corrections of up to 1 eV compared to the respective LDA or GGA energies. While these "corrections" are not to be understood to yield the correct absolute total energies of the elements, they are proposed to give appropriate bounds for the chemical potentials for thermodynamic processes in semiconductors and insulators, such as, e.g., defect formation, surface reconstruction, or catalytic processes. The present model allows to evaluate thermodynamic processes using DFT energy differences taken only between systems that are expected to show good error cancellation.