Ambien (zolpidem) and Xanax (alprazolam) are used for treating insomnia. Xanax is used off-label to treat insomnia; it is approved to treat panic attacks and anxiety disorders. Ambien and Xanax belong to different drug classes. Ambien is a sedative/hypnotic and Xanax is a benzodiazepine.