Taxes can be a burden at any stage of life -- both during your working years, as well as in retirement. But seniors tend to get especially caught off-guard by taxes, especially once they realize that the IRS gets a piece of everything from their pension income to their Social Security benefits.

If you're looking for a way to shield some of your earnings from the IRS, then you'll need to choose your investments wisely. And here's one option worth considering.

The upside of municipal bonds

If you're many years away from retirement, you shouldn't put a ton of money into bonds. Rather, you should focus on buying stocks, which tend to deliver a much higher return than bonds. You can also handle the volatility of stocks, given your age.

Map of the United States

Image source: Getty Images.

But if you're nearing retirement, bonds are a good bet for your portfolio -- specifically, municipal bonds. First, like all bonds, they make interest payments on a regular basis. That's a good way to secure a steady stream of income for your senior years to supplement your Social Security benefits and retirement-plan withdrawals.

But also, municipal bond interest is always tax-free at the federal level. And if you buy municipal bonds that are issued by your state of residence, you won't be taxed on your interest payments at the state or local level, either.

Say you enter retirement with enough money in your IRA or 401(k) plan to withdraw $20,000 a year, and you're also entitled to $20,000 a year in Social Security income, for a total annual income of $40,000. That may seem like a decent chunk of cash -- until you realize you're subject to taxes on all of it. (Remember, retirement-plan withdrawals are only tax-free if you've saved in a Roth account).

On the other hand, say that in addition to your $40,000 of taxable retirement income, you also own municipal bonds that pay you $3,000 in interest each year. If that $3,000 is completely tax-free, it'll serve as a nice cushion and won't add to a tax burden that already exists or push you into a higher tax bracket than you'd like to be in.

Furthermore, while it makes sense to go heavy on municipal bonds if you're nearing or in retirement, they're also a suitable investment if you're younger. Granted, you'll only want to tie up a smaller percentage of your money in bonds, because as mentioned earlier, you'll need stocks to help ensure that your money grows at a decent pace.

But that doesn't mean you can't own any bonds when you're younger. If you want that diversity in your portfolio, plus the guaranteed income of those regular interest payments, then municipal bonds make sense. That way, you don't add to your tax burden at a time when it may be growing.

Though there's no such thing as a risk-free investment, municipal bonds are among the least volatile places to put your money. And if you like the idea of generating some tax-free income in your portfolio, then they're really a solid bet.