Caffeine's half-life — the time it takes your body to clear half of it — averages five to seven hours, but the real range across healthy adults is much wider: some people clear it in two hours, others take up to ten.
Where the research is more consistent is on the downside of cutting it close: caffeine consumed around six hours before bedtime has been shown to reduce sleep efficiency and cut total sleep time by around 40 minutes in controlled trials, even in people who don't subjectively feel "wired."
The practical takeaway isn't a single magic cutoff time — it's that caffeine lingers far longer than most people assume, and individual sensitivity varies enormously.
Further reading: Dose and timing effects of caffeine on subsequent sleep (SLEEP journal).