TL;DR Caffeine is a natural substance that's considered a stimulant. It's able to keep you alert because of how it works in the brain. To get the most bang for the buck, it's important to know the right amount of caffeine to consume in a day.
Caffeine. Sometimes, it’s a love-hate relationship. It keeps us awake when we need to (read: all-nighters), but it can also keep us more alert than we’re supposed to.
Flashback to that moment when you found yourself staring at the ceiling at 1 AM, regretting your decision of having that leisurely cup of coffee after dinner. Your body’s not sure whether to jump out of bed and do another round of Netflix or keep counting how many hours left until you need to get up for work.
For something we consume regularly, caffeine tends to consume us back. We enjoy it so much to have one, two, three cups of our favorite caffeinated drink, only to realize the consequence when that 1 AM moment hits.
Caffeine can be a much-needed boost – that’s common knowledge – but we’ve never really paid much attention to how it works.
That’s what this article’s for. We’re going to dive deep into caffeine and how it affects sleep.
Found in coffee, cocoa beans, tea leaves, even in nectar, caffeine is a natural substance that’s considered a stimulant, something that promotes alertness. It can heighten mood and make you happy, improve reaction time, and elevate mental performance. 
While caffeine is found in most natural sources, it can also be synthetically produced. Soda, energy drinks, “energy shots”, and mints all contain caffeine and are available in retail.  These are usually marketed as a dietary supplement taken to enhance performance and lose weight.
Caffeine is considered a “moderately effective alerting agent,” so it can help you achieve an optimal performance in mind and body, helping you achieve laser focus even with lack of rest. 
On the flip side, research suggests that caffeine can spike heart rate and blood pressure, while leaving you feeling stressed, anxious and jittery.
It can also have a disruptive effect on the quality of sleep. If taken excessively or within 6 hours before going to bed, it can eat up on your snooze time. The National Institutes of Health reported that caffeine can leave you feeling wired for up to 16 hours after your last cup. 
To understand what exactly happens when caffeine interacts with your body, you need to know this chemical called adenosine.
Your body breaks down a high-energy molecule called ATP, which is needed for its constant supply of energy. As it performs this function, it liberates adenosine, a sleep-inducing molecule in your body that causes sleepiness. When adenosine binds to adenosine receptors in the brain, it slows down nerve cell activity, making you drowsy.
Here comes caffeine, which looks like adenosine. As an “adenosine receptor antagonist,” caffeine is recognized by the nerve cells and receptors as adenosine. When caffeine binds into the receptors, it blocks adenosine and inhibits the latter’s effects on your body.
So instead of slowing down nerve cell activity, caffeine does the complete opposite – it stimulates you! 
Caffeine can also boost positive feelings. Here's an article on how caffeine can affect your mood.
This physically manifests in the form of: 
New research also shows that caffeine can even alter your circadian rhythm, or your body’s natural cycle and internal clockwork. Two espresso shots, for example, taken 3 hours before bedtime can set you back an hour and make your body think you’re coming from a different time zone. 
Caffeine has an immediate effect on your body… as fast as 30 to 60 minutes. While it can stay in your system for a long time, half of the caffeine you consume can last from 3 to 5 hours.
The American Academy of Sleep Medicine says that the normal dose of caffeine is about 50 to 200 mg, best taken on an “intermittent, on-and-off basis.” Anything higher than this may be considered excessive and can be comparable to the effects of amphetamine – a low dose of it at least. 
Also, when it’s consumed daily, the body gets more tolerant to it.
This doesn’t mean that you can be forever awake by drinking bottomless coffee! Remember, your body can adapt to almost any situation – including higher levels of caffeine. When adenosine keeps getting blocked by caffeine, your body starts to develop more receptors to help the body regain its naturally-occurring process.
You can still get away unscathed by remembering to:
To close, here’s a parting gift:
This Vox article  gives you the best coffee drinking strategy for staying awake and alert when you need to study for an exam or finish that work presentation due first thing in the morning.
The ninja trick? Having coffee then taking a nap. Yes, you read that right – coffee naps!
Drink coffee immediately before taking a nap, then wake up 20 minutes later. This helps you take advantage of both caffeine and sleep.
“But here's the trick of the coffee nap: sleeping naturally clears adenosine from the brain. If you nap for longer than 15 or 20 minutes, your brain is more likely to enter deeper stages of sleep that take some time to recover from. But shorter naps generally don't lead to this so-called "sleep inertia" — and it takes around 20 minutes for the caffeine to get through your gastrointestinal tract and bloodstream anyway.
So if you nap for those 20 minutes, you'll reduce your levels of adenosine just in time for the caffeine to kick in. The caffeine will have less adenosine to compete with, and will thereby be even more effective in making you alert.”
We have good news for lovers of America’s two favorite mood-altering substances, caffeine and marijuana: Caffeine may enhance a pot smoker’s high.
The downside is that caffeine can also increase the chance of becoming addicted to cannabis.
If you often find yourself going to a room in your house only to forget what you’re meant to do, or talking about a movie character you’ve seen so many times but can’t remember the name of, then one thing is going on.
It’s so easy to dismiss these as simple moments of forgetfulness. But if this happens a lot, then it may be your body sending you a signal to do something and beat brain fog.
The writer, Dan Charles, bought a 4-ounce bag of caffeine and said it had as much caffeine as 1,000 tall Starbucks lattes. He said this caffeine was created in coffee beans on a hillside in the tropics. “Slowly and quietly, driven by the energy of sunlight, it formed inside coffee beans hanging on thousands of trees, most likely in Brazil or Vietnam,” Charles wrote.
“Those beans were harvested, loaded on ships bound for the port of Houston, Texas, and ended up at a factory within sight of downtown Houston: Atlantic Coffee Solutions. It’s owned by one of the world’s largest coffee traders, ECOM Agroindustrial Corp., which is based in Switzerland.”