From what I’ve read:
First, don’t assume that Kim (or Saddam, or Gadhafi, or any dictator in a vicious lethal dictatorship) simply snaps their fingers and everyone does what they say. It’s better described as riding a tiger - they have to constantly watch for other people in the power structure are doing, that they are not getting to popular, or making alliances; there’s the perpetual risk that some clique would get together and organize a coup. That’s likely why for example, he offed his uncle (although suggestions he fed him to wild dogs may be an exaggeration). Kim’s dad Kim was nearly blown up when his train was coming back from China a decade ago.
So there’s the suggestion too (always suggestions, it’s difficult to know really what’s going on) that a lot of the crises with the outside world are engineered to keep the local government on it’s toes - they would be too busy running around the country, mobilizing and moving troops, to allow any time for coup arrangements.
Plus, they recognize that being unpredictably dangerous is a good way to keep the other side off guard.
Kim rests secure in the knowledge that:
China would never allow the South or America to invade the North. They do NOT want a united Korea with American troops on the Chinese borer.
The South would never initiate a war - or allow the USA to do so. (Nor would Japan). The risk of nuclear weapons, lately, is also added now to the risk that the North could pulverize Seoul with artillery fire from over 10,000 artillery dug into the hills capable of shelling the South within 30 miles of the capital. As mentioned the other day on CNN, the South is the 11th largest economy in the world, and to have it’s major city pulverized overnight by Kim would be catastrophic.
The North knows they can do marginally provocative acts and spout crazy rhetoric without fear of retaliation.
As long as some (corrupt? complicit?) Chinese are willing to front for the North, and as long as the Chinese government tolerates them, they can bypass sanctions.
They obviously don’t want the Chinese in there to run the country, as Kim and cronies would lose a lot of control.
The Chinese want to stay as far from the mess as possible; but they could see a giant cluster of chaos if there were unrest in North Korea, so they will tolerate and prop up any the status quo.
meanwhile, North Korea keeps building bigger and better missiles and bombs. They have learned from Saddam and Gadhafi the most important lesson - the only way to stop the west from invading is to actually have weapons of mass destruction. They are in no hurry to lose that leverage - the bigger and better and more reliable their weapons, the more it deters an invasion.
they also note the problem that allowing free markets - as China has - introduces a different problem. As people get rich (and some do) they can buy influence, brie others, and possibly buy their way into a coup. So they try as hard as they can to limit free commerce.
So, Kim knows he won’t be attacked as long as he maintains his warlike posture. As the saying goes, “your mouth shouldn’t write cheques your ass can’t cover…” When Trump threatens retaliation, he can safely say “well, maybe we’re gonna bomb Guam. then whatcha gonna do?” Supposedly trump has an answer for that.