It seems to me that every year you would want to sell enough such that the gains hit the top of your target income. This may be some percentage of the Federal Poverty Level as explored in the GoCurryCracker post Systems101 posted. Ignoring ACA, then it would probably be the top of the 15% bracket since that would be taxed at 0%.

You know your target spending ($40,000), target taxable income (

Lets call it 138% FPL for a couple, so $22,411), tax lot A gains ratio/percentage (50,000/500,000 = 0.1), tax lot B gains ratio/percentage (450,000/500,000 = 0.9). So then if we say is A is the amount sold from tax lot A, and B is the amount sold from tax lot B, then we have:

A + B = 40000 => A = 40000 - B

0.1A + 0.9B = 22411

Substitute the formula for A from above and solve for B

0.1(40000 - B) + 0.9B = 22411 => 4000 - 0.1B + 0.9B = 22411 => 0.8B = 22411 - 4000 => B = 18,411/0.8 => B = 23013.75

Now solve for A using the formula above

A = 40000 - 23013.75 => A = 16986.25

So you would sell $16,986.25 of tax lot A (the tax lot that's mostly basis) and $23,013.75 of tax lot B (the tax lot that's mostly gains).

In reality you don't really care if you sell exactly $40K worth, you just need to sell at least $40k worth, so what I've really shown above is the maximum amount you can sell of Lot B (the one that's mostly gains) while still meeting your minimum spending requirement and maximum income requirement. You could also just as well meet your "real" requirement of staying under 138% FPL and maxing spendable money by withdrawing only from A like so:

0.1A + 0.9*0 = 22411 => A = 22411/0.1 => A = $224,110

So basically, those are the extremes. You can sell anywhere between $16,986.25 and $224,110 of tax lot A and $0 and $23,013.75 of tax lot B and solve for the other using the formulas above while still meeting the goals described above.

Generally, I think I would try to stick closer to my first example (by selling as much of the high gain tax lot as you can within constraints, then making up the difference with the low gain lot) as that leaves you more flexibility in the future, since, as you can see from my second example, you can realize much greater spending power from a tax lot with small gains than one with large gains while still staying under an income limit.

Remember that you'll need to adjust the gain percentages at any given time you perform the transaction. Also remember that you probably can't be this precise in real life since you won't know the exact gain percentages you will end up selling at when you place the sell order. This wouldn't be such a big deal if your gain limit is the 15% bracket since it would just result in any small overage being taxed at 15% capital gains rates. It would be much worse if your gain limit is some percentage of FPL for ACA purposes since that's a cliff. Basically just leave some leeway.

Depending on your emergency fund situation you may also want to err on the side of caution by not maxing sales of the high gain tax lot in case you need some extra spending money over what you've predicted.

Of course this gets much more complicated when you have more than the two tax lots you've outlined in your simplified scenario. I think the generalizable message we can apply from this is to sell as much of our highest gain tax lot as we can while still having enough income headroom to sell enough of a lower gain tax lot to meet our spending needs and probably also to have a little extra in case we need it unless we have other resources (like and emergency fund) that we can draw on. This best meets our goals of having enough to spend, staying under an income limit, and maintaining as much flexibility in the future as possible in case either our spending needs rise or income requirements fall.

I think that makes sense?

Um.... maybe I'm being dull here, but I always assumed that any sell off would automatically consider the oldest tax lot as sold off first, and then the next oldest, etc...

As you say, FiFo is generally the default, but you can request specific identification. I think you make the change at any point, but I'm not certain about that (you might have to do it when you set the account up).