I won't tell you how to break your habit, but I'll tell you to do whatever you can to break it. I'm glad that you asked for help, because it's really important. Because I know what's ten years later. I feel like you described exactly me when I was your age. Every. Single. Word.

Then it only gets worse. From 3 hours of real work a day to 3 days of real work a week, then to 1 week of real work a month. This single week is of course much more intensive and you work long hours then - either because something you're working on really excited you and you're having fun or because you've stretched so much all the deadlines and patience of other people that you know you simply have to. These on/off periods are not healthy neither to your self-feeling nor your finances nor the product or the team you're working with.

Fast forward ten years and I'm totally broke with overdue debts high over my head. I haven't slept well in five years and I feel like shit both physically and mentally. I know I need some help and I don't want you to end up in this place as well.

I still can be super productive and build great things, but the catch is I am able to do this only when I do things that excite me (what isn't that hard as I still have great passion for technology). Which is not always feasible at my current job, so the end result is that I'm having super productive month or two and then totally unproductive two-four months. My employer is not happy with that, but they learnt to accept this as I still can be very exceptional when I'm in my flow. The catch is I'm paid by the result so I don't earn well in my non-productive periods.

Part of the problem probably is that they often make me to work on totally boring stuff that should be done by totally different people and which could be done by people with 20% of my skills and 20% of my rates. This includes non-development and non-product related stuff, virtually everything: dev, bizdev, product, marketing, sales, management or simple administrative tasks - I can be good in everything I have vast experience in various areas and they use/abuse that.
I sometimes think that they do that on purpose to don't spend too much on me and build my guilt up, as they know that throwing me into all that variety of tasks (which highly varies by area/contexts, complexity and required skill level) basically kills my motivation and ability to do stuff. I know that I need to break away from this toxic relationship. Well, that is still pending.

Anyway. I'm glad that you see your problem as a problem. I didn't see it that way and when I started, it was already too late. Please consider this as a warning on how you can screw up your life if you go down this path, as this path only goes down and down. Don't waste your gifts and your life.

Find something that excites you and is so much fun that you simply want to work on it. This will be different for different people. It may be a startup with super exciting product or it may be a freelancing career because it might turn out that you get bored easily and cannot enjoy a single project for long. If you feel you're burned out, just throw it away while you can. Make a break. Travel the world. Try a totally different career. Or simply change the job. I am just throwing guesses here and these are just things I think might work, but I haven't tried them (I'm in a hard position to try because of the overwhelming debts which I need to service, so I cannot switch to anything less paid).

Read every single post in this thread and try things that you think might work for you. But do something about it. I keep my fingers crossed.