Some say creativity is a matter of producing as much alpha waves as possible. Apparently this was measured by electroencephalogram, and there are a lot of alpha waves during creative states. First you have to be technically and physically prepared to play well when an idea occurs to you, but even in the best of cases we don't understand very well where the idea comes from. For example, I've gone through periods where I write a composition per day for a month. Stravinsky said, and coming from him it's rather moving, that inspiration is not the exclusive property of composers, but can happen to everyone.
Recording is another problem, due to financial constraints.
Most of my records were done in 8 or 9 hours in the studio. The others were recorded live or in two tracks, without mixing, in one day in the studio. But in jazz, spontaneity is valued. If we hadn't done live concerts before the recording, it would have been difficult. We needed that rehearsal time in concert.