Magnesium is often sold to athletes for cramps, sleep and recovery. It does matter for muscle and nerve function, but whether a supplement helps depends mostly on whether you are short of it.
What I take from the evidence:
Adult recommendations are roughly 300-420 mg per day, varying by sex and age
Nuts, seeds, whole grains, legumes and leafy greens are good sources; a varied diet often covers needs
Supplementation seems to help mainly when intake or status is low; in people who are already replete, benefits are unclear
The evidence that magnesium prevents exercise-associated cramps is weak
Who might be at higher risk of low intake? People on restrictive diets, athletes cutting weight, and those who eat few whole grains or nuts. For them, fixing the diet is usually the first step, and a supplement can be a backup.
Blood magnesium is not a great marker of total body stores, so testing is less straightforward than for iron.