In a 2013 study in the Journal of Clinical Sleep Medicine, volunteers took 400 milligrams of caffeine, a large coffee's worth, at bedtime, three hours before, or six hours before. Even at six hours before bed, the caffeine cut total sleep by more than an hour on laboratory measurement, and crucially, the participants often did not perceive the loss. Sleep was lighter and more broken whether they felt it or not. This is the uncomfortable core of caffeine and sleep: subjective tolerance is real for the alerting effect, but the sleep architecture pays anyway. You can fall asleep fine and still sleep measurably worse.
Caffeine works by blocking adenosine receptors. Adenosine is the chemical pressure that builds through the waking day and drives sleep depth. Blocking the receptors does not remove the pressure; it masks it, and the underlying need keeps accruing. Through the night, as caffeine clears by halves, the masked pressure floods back in fragments, lightening sleep in the second half of the night and trimming deep and REM stages. The result is the familiar pattern of sleeping seven hours and waking unrefreshed, blamed on age or stress. Tolerance to caffeine's wakefulness develops within days; tolerance of the sleep disruption, the research suggests, develops much less.
Work backwards from your bedtime using half life. With an average five hour half life, a coffee finished ten hours before bed leaves a quarter of its caffeine, twelve hours leaves under a fifth. For a 23:00 bedtime that means the last coffee around 11:00 to 13:00, earlier for slow metabolisers, pregnancy or anyone on interacting medicines. Our caffeine calculator draws your personal curve so you can test your own habit rather than obey a generic rule. Keep the morning ritual, defend the evening: moving the cut off earlier rescues more sleep than any supplement, and it costs nothing but a small negotiation with habit. Test the rule against your own life with the caffeine calculator above: enter your real last coffee and your real bedtime, and look at the milligrams still on duty when the lights go out.