I avoid drinking coffee 12 hours before bedtime, because I read from multiple sources this is the time it takes for caffeine to be cleared from the body.
For example, if my bed time is 11pm, no coffee later than 11am.
It's been working for me so far to prevent caffeine interfering with my sleep.