You replace them when they seem a bit too wimpy at holding a charge, of course!
More seriously, if you have something like a BC-900 or one of the many really good chargers/cyclers made for R/C use, you can measure how much capacity they have left. Fully charge it, then discharge at a moderate rate down to 1.0 volts/cell and see how long it lasts compared to how long it lasted when new. If it's lost more than x%, replace it, where x% depends on how critical the application is.
But most people don't have a cycler, so you estimate. Carry a spare pack of batteries with you when you ride, and when you start needing to use them when you didn't need them before, replace the batteries. Replace the batteries in your rear light more quickly than the ones in your headlight -- if your headlight goes out or goes dim, you notice that right away. If your rear light goes dim or off, you may not notice until you get home.
And don't forget that you might be able to re-use the old batteries in something less less important -- your kid's toys, for example.