An algorithm isn't really AI then. Something worthy of being called AI should be capable of this understanding and judgement.
But they are though. For a seminal book discussing why and detailing many algorithms categorized under the AI umbrella, I recommend:
Artificial Intelligence: A Modern Approach[0]
Foundations of Large Language Models[1]
1 - https://arxiv.org/pdf/2501.09223
An algorithm isn't really AI then. Something worthy of being called AI should be capable of this understanding and judgement.