During retirement, healthcare has the potential to become your greatest expense. So it's important to have funds available to pay for it. And if you contribute to a health savings account, or HSA, you can set yourself up to tackle your medical bills in retirement more easily.

But if you're going to open an HSA, it pays to do so as early as possible. You should aim to have your HSA well-funded before age 65.

A person at a laptop.

Image source: Getty Images.

Why HSA contributions may be off the table at age 65

Age 65 is when Medicare eligibility begins, but not everyone signs up for Medicare then. If you're covered by a qualifying group health plan at age 65, you can generally delay your Medicare enrollment without penalty.

Otherwise, once you turn 65, you may be inclined to sign up for Medicare immediately. But once you're enrolled, you can no longer make contributions to an HSA.

This holds true even if you're only signing up for Medicare Part A at 65 (which is a route many people take, even if they have group health coverage because it's free and can serve as secondary insurance). So it's best to pump as much money as you can into an HSA before turning 65.

Enjoy the tax benefits while you can

What makes HSAs so great is that they're loaded with tax benefits. Contributions are tax-free, investment gains are tax-free, and withdrawals are tax-free, provided the money is used to cover qualifying medical expenses.

You may not be eligible for an HSA every year, depending on your health coverage. But it pays to take advantage of an HSA during periods when your health insurance plan meets the requirements.

While it would be nice to continue funding an HSA indefinitely, once you enroll in Medicare, that option will go away. But don't worry if you're signing up for Medicare and have a large HSA balance left over. You can use HSA funds as a Medicare enrollee -- you just can't add to them. So rest assured that the money you've saved won't go to waste.