There's no sure way to prevent agoraphobia. However, anxiety tends to increase the more you avoid situations that you fear. If you start to have mild fears about going places that are safe, try to practice going to those places before your fear becomes overwhelming. If this is too hard to do on your own, ask a family member or friend to go with you, or seek professional help.
If you experience anxiety going places or have panic attacks, get treatment as soon as possible. Get help early to keep symptoms from getting worse. Anxiety, like many other mental health conditions, can be harder to treat if you wait.