A device for treating sleep apnea in a patient through stimulation. The device includes a generator configured to produce stimulation pulses, a stimulator that receives the stimulation pulses produced by the generator and delivers stimulation to the patient, and a controller. The controller is configured to determine a sleep state of the patient, adaptively control the generator based on the sleep state where the sleep state includes a plurality of sleep stages, and modulate a stimulation energy of the stimulation pulses produced by the generator based on the sleep stage of the patient