The half-life of caffeine determines how long the stimulant remains in the body. A half-life is the time it takes for one-half of the amount of a substance to be eliminated from the body.
For example, if you consume 200 mg of caffeine, the half-life is the amount of time until you have just 100 mg left in you.
The half-life of caffeine varies
It’s impossible to tell coffee’s exact half-life because it differs from person to person.
According to the article “Caffeine Pharmacology” :
The half-life of caffeine … varies widely between people, depending on factors such as age, body weight, pregnancy status, medication intake and liver health. In healthy adults, the half-life is approximately 5 to 6 hours. Heavy cigarette smoking can decrease the half-life of caffeine by up to a half, and in pregnancy the half-life may be increased by as much as 15 hours.
Caffeine’s stimulating properties start as soon as 15 minutes after taking it and can last up to six hours. It reaches its peak concentration in the blood about 15 to 120 minutes after ingestion.
Taken in moderate amounts, caffeine increases alertness and decreases sleepiness, the article says.
Caffeine tolerance and resetting
Whatever the half-life, people do build up a tolerance for caffeine that reduces the stimulating and invigorating effects of the world’s most popular mood-altering substance.
According to Caffeine Informer, a first-time user or someone who has been off of caffeine for a long time has zero tolerance. These people get the maximum effects of euphoria, alertness, good mood and increased energy and motivation. 
To reset the tolerance to zero, the article says a person should do a caffeine cleanse by not ingesting it for between two weeks to two months. Then, don’t let caffeine dependence develop again by consuming it too regularly.
This is called caffeine cycling, and some people do this to get the optimal effects of caffeine.
12 hours to clear it from the body
It can take several hours to completely eliminate caffeine in our system.
That can be good or bad, depending on when you take it. If you have more than your usual dose late in the day, it can lead to a night of tossing, turning and no sleep. So it’s important to know when the best time to drink your cup of joe is.
Many people stop taking caffeine, whether in beverages, medicines, candy or supplements, well before bedtime. And it takes up to 12 hours to clear it from the body. James Lane, an emeritus professor at Duke University School of Medicine in North Carolina, told Live Science that the timing makes it more likely that people will develop a dependency on caffeine because they want to drink it the next day when they wake up. 
"The reason people who regularly drink caffeine wake up feeling groggy, confused or with a headache is that they are experiencing withdrawal symptoms from yesterday’s coffee," Lane said. These withdrawal symptoms are relieved when they get their morning fix of caffeine.
The American Academy of Sleep Medicine recommends steering clear from caffeine at least six hours before hitting the hay. 
So if your usual bedtime is at 12 AM, your last java fix should be done by 6 PM.
So it is safe?
When we talk about the “half-life” of a substance, people might be nervous because the term is closely associated with poisons and nuclear radiation. But half-life is a medical term too, and even the most benign or beneficial substances have half-lives.
Caffeine is considered safe when taken in moderation.
But what is considered a moderate dose?
The European Food Safety Authority in 2015 said caffeine intake of up to 400 mg a day, or single doses of 200 mg, is safe for adults.
The professional journal Coffee & Health states: 
EFSA also advised that single doses of 100 mg of caffeine may increase sleep latency and reduce sleep duration in some adult individuals, particularly when consumed close to bedtime.
To determine how much caffeine the products you consume contain, consult this table from the University of Rochester:
Please note that some other products, including energy drinks, supplements, mints and some medications, contain caffeine. Read the label of the product you are about to consume carefully.
A caution for pregnant women
Medical experts caution pregnant women to limit caffeine intake to 200 mg per day to avoid lower birth weights and risk of miscarriage.
Coffee & Health also advises all people to take moderate amounts of caffeine to avoid unpleasant side effects:
As with many elements of our daily diet, over-consumption may in some people lead to unwanted side effects. Most people consume a level of food or drink that they are comfortable with and therefore would not experience such effects. However, those who do not self-moderate their intakes of caffeine, may experience feelings of anxiety, hyper-activity, nervousness and sleep disturbance.
Certain factors make caffeine stay in your system longer
Factors like age, body type, your build, genes, if you eat food with coffee, medication and tobacco can all affect how long caffeine stays in your system.
Erectile dysfunction. In combination, those are two of the ugliest words known to man. But can caffeine help you get it up?
Science hasn't found the definitive answer to this question, but one study concluded that fewer men who consume caffeine have problems performing. The study said:
Caffeine intake reduced the odds of prevalent ED, especially an intake equivalent to approximately 2-3 daily cups of coffee (170-375 mg/day). This reduction was also observed among overweight/obese and hypertensive, but not among diabetic men. Yet, these associations are warranted to be investigated in prospective studies