The final piece was to appear crazy, or crazy enough that when pressed, they would choose the suicide option of striking with a nuclear weapon, if they had one. This was critical because a rational actor possessing one or a few weapons would not think of striking its neighbors, since the U.S. counterstrike would annihilate the North Korean regime. The threat wouldn't work if North Korea was considered rational, but, if it was irrational, the North Korean deterrence strategy could work. It would force everyone to be ultra-cautious in dealing with North Korea, lest North Korea do something quite mad. South Korean and U.S. propaganda did more for North Korea's image of unpredictability than the North could have hoped.
North Korea, then, has spent more than two decades cultivating the image to the outside world of a nation on the verge of internal economic collapse (even while internally emphasizing its strength in the face of external threats). At the same time, the country has appeared to be on the verge of being a nuclear power -- one ruled by potential lunatics. The net result was that the major powers, particularly South Korea, the United States and Japan, went out of their way to avoid provoking the North. In addition, these three powers were prepared to bribe North Korea to stop undertaking nuclear and missile development. Several times, they bribed the North with money or food to stop building weapons, and each time the North took the money and then resumed their program, quite ostentatiously, so as to cause maximum notice and restore the vision of the weak, fearsome lunatic.
The North was so good at playing this game that it maneuvered itself into a position in which it sat as an equal with the United States, Japan, Russia, China and South Korea -- and it would frequently refuse to attend the six-party talks. The ability to maneuver itself into a position equal to these powers was North Korea's greatest achievement, and it had a tremendous effect on stabilizing the regime by reinforcing its legitimacy internally and its power externally. Underneath this was the fact that no one was all that eager to see North Korea collapse, particularly since it was crazy and might have nuclear weapons. North Korea created a superb strategy for regime preservation in a very hostile region -- or one that appeared hostile to the North Koreans.
Crucially for Pyongyang, North Korea was of tremendous use to one power: China. Even more than North Korea's role as a buffer state, its antics allowed China to emerge as mediator between the inscrutable Pyongyang and the frustrated United States. As China's economy grew, its political and military interests and reach expanded, leading to numerous tensions with the United States. But Beijing recognized that North Korea was a particular obsession of the United States because of its potential nuclear weapons and American sensitivity to weapons of mass destruction. Whenever North Korea did something outrageous, the United States would turn to China to address the problem. Having solved it, it was then inappropriate for Washington to press China on any other issue, at least for a while. Therefore, North Korea was a superb mechanism for the Chinese to deflect U.S. pressure on other issues.