In order to understand the North Korean policy one needs to understand that their enemy number one is not the United States. Their declared enemy is South Korea.
South Korea is not a nuclear power. But when North Korea is, that gives them quite an edge in the North-South Korean conflict. Yes, the United States have 28,500 soldiers stationed in South Korea, they supported South Korea in the last Korean War (60 years ago) and continuously side with South Korea diplomatically, but it is far from given that the United States would be willing to perform a nuclear strike on behalf of their South-Korean allies if push comes to shove.
The threat of incinerating Seoul (which is dangerously close to the inner-Korean border) is a very real one for South-Korea and it might force them to make concessions they would otherwise not be willing to make.
It is also a threat which is a very effective deterrent against a conventional invasion of North-Korea on behalf of South-Korea. NK is well aware that their existence after the Korean war is just thanks to Chinese intervention and that their chances of winning a conventional war on their own didn't increase much in the past 60 years. Should NK ever fall in disfavor with China, there is nothing which would stop the United States and South Korea from starting the Korean reunification by force. But having the bomb gives NK a second life insurance. It allows them to threaten that should someone put them into a situation where they have nothing to lose anymore, then Seoul and other South-Korean cities will be destroyed too.