How does the human body know which substances are toxic (and need excreting) and which are not?

That’s more of a instance of dose vs. body mass. A spray of insecticide kills insects in part because such a spray delivers a lethal sized dose to a small insect but it’s not enough poison to kill a human.

Insecticides certainly are lethal to humans if they get a large enough dose. They just need a much larger dose than a bedbug or wasp.

I have seen it as somewhere between two to three times as bad for dogs as humans. That probably does count as only a little, but it is enough to get worried. The rate of elimination is different, but doesn’t totally explain the difference.

Looking at Permethrin, which has some interesting traits.
http://npic.orst.edu/factsheets/archive/Permtech.html

The topical LC50 for honeybees is 0.029 μg/bee - That is topical, not ingested. Honeybees mass about 0.1 grams. So that translates to 0.29mg/kg. Animals fed on a diet of Permethrin with an equivalent dose a thousand times higher showed some minor effects over time. None died.
Birds are pretty tolerant.

You can’t make some of this up:

Aquatic life is ruinously sensitive. nanograms per litre in the water is deadly.

I would substitute “every” for “any”, although that may just be you and I writing / reading different forms of “any” 's many meanings. Some bad things the body can handle. Some bad things it cannot.

You are right to add the time element to all this. Some fast-acting substances do great harm before they can be adequately handled.

I’ll quibble a bit with “excrete”. In most cases the body doesn’t eject bad stuff as-is. Which is what “excrete” implies.

Instead it chemically disassembles = “metabolizes” the bad stuff into less-harmful or fully harmless constitutent components. Which in turn may be further metabolized and eventually excreted in the simplified harm-reduced forms.

Given time and metabolic capacity. As someone pointed out upthread, your system has X capacity to perform the Y → Z+A+B chemical transformation. If you’re taking in more Y than you can disassemble per unit time, you’re on a trajectory to become overloaded with Y even as you’re still successfully converting some of it to Z+A+B.

Some things aren’t even directly toxic. Like ethylene glycol. It harms you because a metabolite of a metabolite of it is toxic.

There are also pro-drugs where the material you ingest doesn’t do anything useful in the body itself as-is. But once the body partially disassembles it, some of the resulting daughter products are effective at doing whatever the medicine is intended to do.

This whole branch of how drugs (or anything else) are processed once inside the body is called

The OP might enjoy reading some of this article or other related ones.

Also dosage. I can shotgun a 12-ounce beer containing an ounce of ethanol, and I’ll be pretty much back to normal in an hour. If I shotgun 12 ounces of ethanol, I’m guessing I’ll end up dead (or close to it).

There are also toxins for which, if the fatal effects are temporary and can be treated with supportive measures, the body will metabolize the toxin and you’ll recover.

Therapy is supportive and based on symptoms, with aggressive early airway management.[51] If consumed, treatment can consist of emptying the stomach, feeding the victim activated charcoal to bind the toxin, and taking standard life-support measures to keep the victim alive until the effect of the poison has worn off.

Bolding mine.

There’s quite a lot of that, in fact.

The liver (or other detoxifying organ) doing its job can create the actual toxic substance. For instance, acetaminophen/paracetamol is decomposed in the liver into a toxic substance that it can usually detoxify quite rapidly in turn, but that second stage requires a chemical that can run out if too much is taken… so acetaminophen toxicity is based on an overdose creating a toxic metabolite that the liver runs out of the ability to detox.

Fair correction. I like your language better.

And as you and @Velocity noted, a timely manner is important for this. Although, some bad things for us do seem to linger and accumulate over time. There is no metabolizing some things (like mercury).

ETA: I may be wrong about metabolizing mercury. It seems it is more complicated than I thought.

Here’s the way I like to think about evolutionary questions like this. It’s not that our bodies “learned” which substances are toxic and need expelling, it’s that our ancestors who didn’t expel or avoid those substances, for whatever reason, didn’t survive to pass on their genes. Subtle genetic variation and/or mutations mean that those ancestors who were less turned off by bitter or otherwise foul smelling and tasting substances were more likely to eat or drink something poisonous and die.

Similarly, we didn’t “learn” to be creeped out by bugs and spiders. Our ancestors who were less likely to swat at itchy/ticklish feelings on their skin were more likely to get stung or bitten and thus more likely to die either directly by venom or indirectly by being incapacitated and less able to gather food and water. Iterate this over millions of years and generations and here we are.

It’s similar to the hypothesis of why we tend to like shiny things. Shiny things are generally safer because they’re cleaner. Water is shiny/sparkly in the light, and water is critical to our survival. So our ancestors who liked shiny things more often noticed the glint of some water through the trees and were less likely to die of dehydration.

I think many mechanisms for coping with toxins evolved as separate little details in our makeup. A possible example I find fascinating is the sensitivity of the inner ear to some toxic substances, which makes us dizzy when we consume them. We have a nausea response to dizziness, which tends to make us vomit. I’m tempted to see this nausea response as an evolved mechanism for ejecting the toxins. On the other hand, we are awfully good at noticing relationships that aren’t actually there, so…

Here is a blogger I follow who today happened to speak to exactly this idea: The Next Paxlovid | Science .

In this case, in the rush to get Paxlovid to market they formulated it as a combination of the actual active substance that attacks COVID (nirmatrelvir), and a different drug substance (ritonavir) that does nothing whatsoever to COVID, but keeps the liver so busy tearing up the second decoy drug that the first one sneaks through in sufficient quantity to attack your circulating COVID virus.

Very clever drug design.

Biology in general and the human body in particular is very very very complicated and the experts readily admit we are just barely, barely scratching the surface of understanding how this all works. Every time somebody thinks they’ve peeled the onion all the way, somebody else discovers a brand new umpteen-layer deep onion hiding inside.

Heh. I finishing reading that exact blog only a couple of minutes ago.
Derek is always worth reading.

Totally not my field of expertise, but I really enjoy his skill and attitude.

Like with your writing here, listening to an expert speak skillfully to laymen about his own wheelhouse is always a fulfilling journey of discovery.

any alcoholic knows that on a visceral level…

just mentioning Derek in this context - is enough to put a smile on many faces … he is def. one of those (nowadays) rare internet nuggets beneath a sea of influencer-crap that might actually lower the reader’s IQ.

Always 10 min. well spent with Derek - and the igually great comment section!