You'll get lots of answers because there are so many variables.
When you hear that they lay a couple of years, that information stems from commercial production where most facilities get about 18 months of lay and slaughter because it isn't economically competitive to carry them through a molt.
Then there are the egg hybrids that many backyarders are now getting from farm and home stores. They'll kick out the majority of their eggs in the first 3 years but if not butchered then, they'll continue to lay sporadically for a while.
Then there's the heritage breeds like Orps, JGs, Rocks, dominiques, etc. that, given good health, will continue to lay for up to 10 years.
Every hen (like mammals) is born with a finite number of eggs. If she remains healthy she'll lay a long time but after each molt she'll lay fewer and larger eggs generally speaking till they lay rarely.
One just has to determine when it's no longer feasible to feed them for the number of eggs produced.