close

DEV Community

Cover image for Your Last Coffee Isn’t the Problem; the First Three Are Still in Your System
Aral Roca
Aral Roca

Posted on • Originally published at kitmul.com

Your Last Coffee Isn’t the Problem; the First Three Are Still in Your System

The 50mg sleep disruption threshold

A friend of mine was averaging 4 cups of coffee a day. Morning espresso at 7. Drip coffee at 10. Post-lunch lungo at 14:00. And an afternoon ristretto around 16:00 "just to push through." He wasn't sleeping well, but blamed stress, screens, the usual suspects.

I told him to do the math.

The exponential decay you're ignoring

Caffeine follows first-order elimination kinetics with a half-life of approximately 5 hours in healthy adults. The formula is simple:

remaining = dose * 0.5 ^ (elapsed_hours / 5)
Enter fullscreen mode Exit fullscreen mode

A single 95mg drip coffee at 10:00 AM leaves ~47mg by 3 PM and ~24mg by 8 PM. Sounds harmless. But most of us don't drink a single cup.

When you stack multiple intakes, the decay curves overlap. That 63mg espresso at 7 AM is almost gone by evening, but the 80mg lungo at 2 PM still has 40mg in your system at 9 PM. Add the 40mg ristretto at 4 PM and you're sitting at 64mg of caffeine at bedtime; well above the 50mg sleep disruption threshold identified in clinical research.

The problem isn't any single cup. It's the superposition of all of them.

Different types of coffee beans and brewing methods, each with a different caffeine content

Why I built a multi-intake caffeine tracker

Most caffeine calculators I found online only handle one beverage. You enter "95mg at 8 AM" and get a decay curve. Useless if you're a multi-cup person; which is most of us, according to the FDA's average of 3-4 cups per day in the U.S.

So I built one that handles the actual workflow: add as many beverages as you want, each with its own type and time, and see the combined decay curve.

The Caffeine Half-Life Calculator supports 11 beverage presets:

Beverage Caffeine (mg)
Ristretto 40
Espresso 63
Lungo 80
Drip Coffee 95
Cold Brew 200
Matcha 70
Energy Drink 80
Black Tea 47
Green Tea 28
Cola 34
Decaf 7

You can also enter custom amounts. Everything runs client-side in the browser; no data leaves your machine.

The math behind the combined curve

For N intakes, the total caffeine at time T is:

total(T) = sum(dose_i * 0.5 ^ ((T - t_i) / 5)) for all i where T >= t_i
Enter fullscreen mode Exit fullscreen mode

Each intake only contributes to the total after its consumption time. The resulting curve isn't a simple exponential; it's a sum of shifted exponentials, which can produce surprising plateaus when intakes are spaced closely.

This is why eyeballing it doesn't work. A 3 PM coffee "doesn't seem like much," but combined with the residual caffeine from morning drinks, it can push your bedtime levels past the threshold.

A bed with white sheets, the place where caffeine levels finally matter

What we learned about his consumption

After plugging in his actual daily routine:

  • 7:00 AM Espresso (63mg) ; mostly gone by evening; only 5mg left at 11 PM
  • 10:00 AM Drip Coffee (95mg) ; still 17mg at 11 PM
  • 2:00 PM Lungo (80mg) ; 28mg at 11 PM
  • 4:00 PM Ristretto (40mg) ; 18mg at 11 PM

Total at 11 PM: 68mg. That's 36% above the sleep threshold.

Dropping the 4 PM ristretto brought him to 50mg; right at the edge. Moving the lungo to 1 PM dropped it to 43mg. That single hour shift made the difference.

Developer workspace with a coffee cup next to a laptop

The developer who also drinks cold brew

If you're a developer reading this; and statistically you probably are, given where I'm posting this; cold brew is particularly dangerous. At 200mg per serving, a single cold brew at 2 PM leaves 71mg in your system at 11 PM. By itself.

Add that to a morning coffee and you're looking at 80+ mg at bedtime. No wonder you're staring at the ceiling at 1 AM wondering if it's the code review or the caffeine.

Technical details

The tool is built with React, runs entirely in the browser (no backend), and uses URL state for shareability. The decay chart uses a simple bar visualization with color coding:

  • Amber/dark: high caffeine (>50% of peak)
  • Amber/light: moderate (below 50% of peak, above sleep threshold)
  • Green: safe for sleep (below 50mg)

The source is part of Kitmul, a collection of 300+ free browser-based tools. If you're into health tracking, you might also find the BMI Calculator, BMR Calculator, or Water Intake Calculator useful; staying hydrated actually helps your body process caffeine more efficiently.

Practical takeaways

  1. Track all your intakes, not just the last one. Cumulative caffeine is what matters for sleep.
  2. The cutoff time depends on how many cups you've already had. A 2 PM coffee is fine after one morning cup, but not after three.
  3. Cold brew is not just "strong coffee." At 200mg it's in a different category entirely.
  4. Ristretto < Espresso < Lungo. Shorter extraction = less caffeine. If you want an afternoon coffee, go short.
  5. The 50mg threshold is a population average. If you're a slow metabolizer (CYP1A2 gene variant), your half-life could be 7+ hours. Adjust accordingly.

Try the Caffeine Half-Life Calculator with your actual daily intake. You might be surprised how much caffeine is still in your system at bedtime.


All calculations run locally in your browser. No data is sent anywhere. The tool is free, open, and has no accounts or limits.

Top comments (6)

Collapse
 
klaudiagrz profile image
Klaudia Grzondziel • Edited

Wow, it's insane how much caffeine cold brew has! I'm also surprised to see matcha here; I thought it was pretty "safe". I am usually careful with my caffeine intake, but it happens, of course, to have a drip or tonic espresso in the afternoon if I want to treat myself.

I tried out your calculator, and it's super cool! Easy to use, nice UI :) It would be great to see some more beverages there, for example, tonic espresso or latte. I hope you continue working on it!

Collapse
 
aralroca profile image
Aral Roca

Hey Klaudia, thanks so much for the kind words and for the suggestion! You were absolutely right, those were missing. I just shipped an update that expands the calculator from 11 to 19 beverages, including tonic espresso and latte, plus americano, cappuccino, flat white, mocha, instant coffee and yerba mate.

Each new preset is backed by official reference values from the FDA, EFSA and Mayo Clinic, so the numbers aren't just guesses. Fun detail you'll probably appreciate: a latte has the same caffeine as the espresso shot it's built on (~63mg), it's just diluted with milk, so "switching to a latte in the afternoon" doesn't actually help your sleep the way people assume.

I also updated the blog post with the full table and the sources. Give it another spin and let me know if there's anything else you'd want in there 🙌

Collapse
 
klaudiagrz profile image
Klaudia Grzondziel • Edited

"switching to a latte in the afternoon" doesn't actually help your sleep the way people assume

^ Yes, exactly my way of thinking 😅 Thanks for putting me right on this!

I'm happy to see you developed your calculator! It's really cool, and I appreciate that you based your measurements on the official data, not on guesses. It's really valuable. Now I see that my drip in the morning and "innocent" matcha or latte in the afternoon are not so innocent as I thought 😅 Not to mention all the tea I'm drinking during day...

Good job! Your calculator is a real eye-opener! 🎉

Thread Thread
 
aralroca profile image
Aral Roca

thanks 🙏

Collapse
 
aralroca profile image
Aral Roca

thanks for the feedback, I'm going to add it

Collapse
 
mykola_kondratiuk_b7f7966 profile image
Mykola Kondratiuk

first-cup timing matters as much as the count. clients who shift from 7am to 90 min after waking end up needing less overall - the afternoon ristretto usually disappears on its own