Is Xanax (alprazolam) safe when taken as needed?

It can be. If it is prescribed for you and taken on a limited basis, xanax (alprazolam) can be safe. However, it can also be habit forming when used for longer than a few weeks. If you have panic attacks (as on the additional clinical information you wrote) there are other treatments that would not risk this and could help your symptoms. Please consider seeing a psychiatrist for full evaluation if you haven't already.