This is a subject that is argued about almost as much as 9mm vs .45 or AR vs AK.

The common answer has been put forth several times already, 1-2K rounds.

But another way to look at this is with a jaundiced eye and a tight pocketbook.

Look objectively at what you really need over the years and what you can afford at one time.
By building your stockpile using ammo that you use and as you can afford it, you are much better off than just grabbing a case of ammo that your gun may not like or function with.

You should replace your ‘carry ammo’ yearly, to ensure reliability and uniformity of the ammo.
The years carry effects the ammo in various ways, including vibration (breaking down the powder), heat (inside a vehicle) also effecting the powder, the bullet sealant breaking down/the bullet deep seating from repeated chambering in a semi auto and more.

So for daily defensive carry (but not shooting) one needs a 50 round box (or two) of defensive ammo per year. That covers three normal sized magazines and a couple of rounds to replace any that may foreshorten from chambering.
500 rounds = 10 years supply of defensive ammo.
When you rotate the old stuff out, keep it for sight verification, or spare ammo in case you have to use the new stuff and run out of new ammo, it’s still good just older. Pretty soon, you’ll have a pile of good defensive ammo put back, one box at a time. Over a ten year period, that’s 500 rounds of good ammo put back without spending any extra.

Me, I recommend buying a box (50 rounds) of ball or generic jhp ammo for your pistol whenever you go shopping. Make sure it’s reliable in your gun before choosing one particular brand or load. That extra $20ish dollars per week or two isn’t much monetarily but in a year, that can add up to 2600 rounds fairly quickly. And while it’s over $1000, it’s easier for most of us to get it $20 at a time. This gives you a stockpile of practice/emergency ammo that can be used for defensive purposes (jhp) if needed but doesn’t command the premium price of the new fangled boutique bullets. Most ‘generic’ jhp loads are nothing but yesterdays good bullet that’s been replaced by a newer version.

Rifles, it depends on what you’re doing. Again, a rifle and ammo that is carried on a horse, in a vehicle or such should have it’s ammo changed yearly or sooner. Ammo left at home in magazines or on shelves, can be left alone and loaded in magazines/speedloaders. I recommend replacing every couple of years but it’s not critical as with carry ammo.

Make sure as with the handgun that the rifle likes the ammo and is both accurate and reliable.
My local SO brought me one of their AR’s, for the last 15 years it’s been a problem child, not liking most ammo and preferring one particular and expensive load. Several hundred rounds later, I found the problems and now it will eat anything that fits in the chamber.
Had someone just stockpiled a case of ammo for that rifle before the repair, they would have had an expensive single shot.

Hunting ammo, few of us use more than a 20 round box of ammo a year for our hunting rifles, it’s fairly easy to put back a 5/10 year supply at a time. Just keep purchasing the same ammo (check the box codes) when you add more years to your stockpile. 200 rounds = 10 years worth of ammo. And any you don’t use, you just put back for later or use for next years sighting in/checking the sights. Much like the handgun ammo, pick up a box or two every so often during the year and you’ll fairly quickly have years worth of hunting ammo.

Unlike many, my SHTF ammo is not ball ammo for the most part.
I look at what I may be doing and most of what I expect to be doing involves hunting/scrounging food and soft points work better for this as well as some specialized handloads (silent).
As for handgun ammo, it’s hunting/target handloads or defensive loads.
Any of my loads will suffice for defensive work post-SHTF, but I’ll use up the ‘good stuff’ first, it’s a known factor what it’s going to do and that’s important, especially if someone else uses your guns.

One thing to consider in a defensive situation/firearm, at least prior to SHTF, mark your ammo boxes/magazines
with the box # or code, or vice versa. This aids you in a court situation.
Being able to identify what box the ammo came out of may keep you out of jail. More on this later.
I individually mark my mags with magic marker numbers, the 20 round mags are marked 18/A, 18/B, and so on. The 30 round mags are marked 28/A, 28/B, etc.
The 18 stands for the number of rounds in the mag originally, the letter is also marked on the ammo box with the date loaded, so I know which mag the ammo is in and when it was loaded.

So why is the date and which box important, a handful of years back, a young couple were at home. The husband in the kitchen, the wife in the bedroom. For whatever reason, the wife took their handgun, a .38 revolver and shot herself in the head, killing herself.
The husband was tried and convicted and went to jail.
Why? Because of the ammo.

The ammo was handloaded, very light loads and there were three(+) different loads in the gun. It was the target ammo they had been using. The problem? The coroner and investigators were unable to reproduce the blast pattern or lack of pattern in testing. What was shown and the husband convicted on was the lack of blast pattern (ejected material) on the wife, the prosecutors insisted that it showed that the husband had shot the wife from farther away and planted the gun, that way there was no blast on her skin, rather than it was an extremely light load and their investigator couldn’t replicate the evidence.
Because the husband couldn’t prove what the load used was, there was no way to replicate the incident and evidence. He couldn’t prove his innocence against a rabid prosecutor, he was convicted.