The GM: *Makes a clearly overpowered monster, intending for the party to flee.*
The Party:
[Picture with the text "Hit him with your crossbow Steve!" overlaid, of a large octopus/squid-like creature with tentacles raised out of the ocean. It towers over a pair of humanoid figures, one holding a staff in one hand and pointing at the squid with the other, the other person aiming a crossbow at it.]
The other solution is to have an alternative 'out'. Like if the overpowered enemy doesn't actually want to kill the players, but knock them out and capture them. Or if there's an established (non–deus ex) rescuer who can come in and save them...preferably in a way that forces them to escape without removing the threat from being there in the future.
Enemy clerics and paladins can be so tremendously badass. "I will bring you back to life just to kill you again for having the audacity to invade my temple, you Godless worm."
Then they keep gloating until the wizard casts Dimension Door or Boblin the Goblin shows up with his friends and throws a molotov.
Some games ask the players to define the stakes and goals when a conflict starts. This can help keep players on track.
Like, the players are on a journey through the mountains, and as they pass through a tunnel they encounter a giant spider. The default mode is "fight the spider to the death!". But if you ask the players again "what is your goal here?" they might remember it's "get through the mountains", not "kill everything we meet." Now they might focus on how to get past it safely.
If the DM rewards players for advancing their goals instead of just murder, that can also encourage non-murder behavior.
this is where dm'ing is much more about storytelling and less about direct mechanics.
for example, in this case I'd let them attack the kraken, only for them to realize as the crossbow bolt sails up towards the eye, then down, into the sea... "Your weapon doesn't have the range, for shit's sake, the arms are hundreds of feet long, it's much larger and further away than you assumed." Block out a disengagement - it grabs a few villagers and smashes some stuff (roll for athletics to dodge flying debris) and is gone leaving a trail of slime and destruction to the sea before you know what's happened.
Now, when they follow the fucker to it's lair.... then it's on like donkey kong lol, stupidity can only survive so many attempts.
“While it is usually immune to nonmagical damage from +0 bolts, because you rolled a 20, the kraken will recognize you next time you’re nearby. It is currently too busy gleefully dismembering dozens of legendary adventurers to respond to your provocation. The captain, noticing that the titan’s gaze is fixed directly upon you, has put it to a vote whether to toss you off the ship. 0 damage, by the way.”
I had a friend who would go straight to the final boss in Chrono Trigger at the start of the game. Somehow he knew how to stalemate the fight so that he could keep the party alive for hours even though everyone was level 1 and impossibly outclassed.