When you sign up for Medicare Part A after age 65, the Social Security Administration automatically backdates your Part A coverage up to six months (but never earlier than your 65th birthday). The moment that retroactive Part A coverage kicks in, you are no longer allowed to have an HSA-eligible health plan, which means any HSA contributions made during that backdated window become excess contributions in the eyes of the IRS, even though you made them in good faith before you ever enrolled.
This catches a lot of people off guard. Someone who works past 65, stays on a high-deductible employer health plan, and keeps contributing to their HSA often assumes the clock only starts the day they actually file for Medicare. In reality, once Part A is backdated, the IRS treats you as having been "entitled to" Medicare for those prior six months, and HSA eligibility requires that you not be entitled to any part of Medicare, not just Part B.
The fix is to identify and remove the excess contributions, plus any earnings those contributions generated, before you file your tax return for that year. You do this by contacting your HSA custodian and requesting a "removal of excess contribution." If you catch it in time, the excess amount is simply treated as if it never went into the account, and you avoid the 6% excise tax that otherwise applies to each year an excess contribution sits uncorrected in the account.
To avoid this problem in the first place, stop contributing to your HSA at least six months before you plan to file for Medicare, or before your 65th birthday if you intend to enroll in Medicare right at 65. Many financial advisors recommend stopping HSA contributions on a schedule tied to your expected Medicare start date rather than waiting until enrollment paperwork is filed, precisely because of this retroactive six-month rule.
It's worth noting that the six-month lookback is capped at your 65th birthday, so if you turn 65 in June and apply for Medicare in August, the backdating only reaches to June, not earlier. Also, employer contributions to your HSA count toward the same limit, so if your employer deposits a lump sum or ongoing contributions, those need to stop on the same schedule as your own.
Money already in the HSA before the ineligible period isn't affected. You can keep the account open indefinitely and use existing funds tax-free for qualified medical expenses, including many Medicare premiums and out-of-pocket costs, even after you're enrolled in Medicare. The issue is only with new contributions made during or after the retroactive coverage period.
Because the rules around timing, tax forms (like IRS Form 8889), and excess contribution corrections can get complicated, it's a good idea to talk with a tax professional or benefits advisor a few months before you plan to apply for Medicare, especially if you or your employer are still actively funding your HSA.