How grateful x should be to y for ϕing depends on:
The expected benefit to x
The actual benefit to x
The expected cost to y
The actual deontic status of y’ ϕing
The believed deontic status of y’s ϕing.
The greater the expected benefit, the greater the appropriate gratitude. Zeroing the expected benefit zeroes the appropriate gratitude: if someone completely accidentally benefited me, no gratitude is appropriate.
I think the actual benefit increases the expected gratitude, even when the expected benefit is fixed. If you try to do something nice for me, I owe you thanks, but I owe even more thanks when I am an actual beneficiary. However, zeroing the actual benefit does not zero the expected gratitude—I should still be grateful for your trying.
The more costly the gift to the giver, the more gratitude is appropriate. But zeroing the cost does not zero the expected gratitude: I owe God gratitude for creating me even though it took no effort. I think that in terms of costs, it is only the expected and not the actual cost that matters for determining the appropriate gratitude. If you bring flowers to your beloved and slip and fall on the way back from the florist and break your leg, it doesn’t seem to me that more gratitude is appropriate.
I think of deontic status here as on a scale that includes four ranges:
Wrong (negative)
Merely permissible (neither obligatory nor supererogatory) (zero)
Obligatory (positive)
Supererogatory (super positive)
In cases where both the actual and believed deontic status falls in category (i), no gratitude is appropriate. Gratitude is only appopriate for praiseworthy actions.
The cases of supererogation call for more gratitude than the cases of obligation, other things being equal. But nonetheless cases of obligatory benefiting also call for gratitude. While y might say “I just did my job”, that fact does not undercut the need for gratitude.
Cases where believed and actual deontic status come apart are complicated. Suppose that a do-not-resuscitate order is written in messy handwriting, and a doctor misreads it as a resuscitate order, and then engages in heroic effort to resuscitate, succeeds, and in fact benefits the patient. (Maybe the patient thought that they would not be benefited by resuscitation, but in fact they are.) I think gratitude is appropriate, even if the action was actually wrong.
There is presumably some very complicated function from factors (1)–(5) (and perhaps others) to the degree of appropriate gratitude.
I am really grateful to Juliana Kazemi for a conversation on relevant topics.