A multistage gas generator for use in a borehole for recovering hydrocarbons from the subsurface formations. The gas generator has an upper end and a lower restricted outlet. A pilot stage is located at the upper end and a plurality of spaced apart intermediate stages are located between the pilot stage and the lower restricted outlet. Hydrogen and oxygen are fed to the pilot stage for ignition. Oxygen is fed to a first intermediate stage and hydrogen and oxygen are fed to each of the other intermediate stages for injection into the gas generator. Excess hydrogen from the pilot stage is burned in the zone of the first intermediate stage and hydrogen and oxygen of each succeeding stage is ignited by the preceeding stage. Water for cooling purposes also is fed to each intermediate stage. Downhole valves selectively controllable from the surface are provided for the pilot stage and each of the intermediate stages. These valves allow the intermediate stages to be selectively operated to increase or decrease the BTU output or to vary other characteristics of the output.