The on-axis total depth of the calorimeter of Fermi is only 8.6 radiation lengths. As a consequence, gamma rays or electrons with more than 300 GeV are not well contained in the detector and the measurement of their energy is a difficult task. Above 1 TeV, the situation is even worse because of crystal saturation: the electronic signal saturates when more than 70 GeV is deposited in a single crystal. We present here a fit of the shower longitudinal profile that overcomes these limitations and gives a good energy resolution up to 3 TeV. Extending the Fermi energy range above 1 TeV is particularly important for the study of the extragalactic diffuse emission and the measurement of the spectrum of cosmic electrons and positrons.