You know, I have been watching various people get headaches from altitude for over 35 years now, and I came to the conclusion that the biggest factor in starting the headache is from simple dehydration. At altitude, the air tends to be cool and dry, so with each breath you are taking in cool and dry air, and you are exhaling warm and moist air. That yields a net loss of energy and moisture for your body.
So, for a dehydration condition, what should you take?
I would suggest fluid, like a pint of water, or else some rehydration solution. Then as you consume that, down your favorite OTC headache remedy, such as aspirin or something similar. One of Acetaminophen (Tylenol) plus one of aspirin would not be a bad idea, either. Let that soak in for 15 minutes and see what happens.
Caffeine may or may not be a good thing. It kind of depends on what your body is used to.