This is happening because you inherited a genetic predisposition, at least if what you're experiencing is androgenetic alopecia. It's also possible that your hair started falling out because of stress, but in that case, you'd notice hair falling out all over your head rather than just in certain areas.
You can get a trichoscopy to check whether you have signs of androgenetic alopecia. It can help determine this with a fairly high degree of accuracy.
Testosterone isn't the actual cause — it just accelerated the process. If you block the conversion to DHT with finasteride, you can significantly slow down hair loss, but there's no guarantee, especially since you're only 23.
I started noticing androgenetic alopecia when I was 27, and by 30 I cut my hair short because of it. But by that point, I'd already been using steroids for 6 years, including trenbolone and high doses.
So if you're going to try to slow down your hair loss, it's possible you won't go as bald as you otherwise would. But I've read a lot of stories on Reddit, including photos, from guys who tried everything they could to keep their hair. You'll probably have to put a lot of effort into it because finasteride alone won't be enough. Then there's minoxidil, either tablets or topical solutions, followed by various experiments with topical antiandrogens, hair transplants, dermarollers, and so on. I've been through all of this myself.
If your hair is really that important to you, I just want to warn you that this will most likely be a long-term battle, and one where you're essentially fighting against your own biology. So it won't be easy.
As for the risks, finasteride does have potential side effects. How common or serious they are is still a subject of debate. We can discuss that too.