The very first Rashi in the Torah, quoting a Midrash, indicates the importance of Bikkurim, the first fruits offering that opens parshas Ki Savo. Bikkurim is one of the “raishis” concepts that the word Beraishis (understood as “for the sake of something called raishis) refers to.
And Bikkurim, as evident from the words that are spoken when they are brought, is an expression of “hakaras hatov,” a truly fundamental Torah concept that is usually, though, not entirely accurately, thought of as “gratitude.”
In truth, it is something more subtle and sublime, indicated in a direct translation of the phrase: “recognition of the good.” That is why an example of the concept, as per the Gemara (Bava Kamma, 92b) is the commandment that “You shall not abhor an Egyptian, because you were a stranger in his land,” in last week’s parsha. And why the maxim expressing it is “Don’t throw a clod of dirt into a well from which you drank” (ibid).
So even someone who intended you nothing good, even an inanimate object, deserves hakaras hatov. How does that work?
My understanding is that the recognition, while expressed to a person, people or object, is ultimately to Hashem, for causing the good – in the case of the Egyptian slavery, the “purification” (kur habarzel) needed to prepare Klal Yisrael to receive the Torah; in the case of the well, its appearance when one was thirsty. The action of hakaras hatov is through the Egyptians and through the well but its ultimate expression is to Hashem.
I remember having negative feelings when on rare occasions I would see a certain person who in effect once forced me to leave a job and community I loved. But then, pondering how what resulted in the end was in fact a tremendous brachah for me and my family, I realized that I needed to feel hakaras hatov toward him. Well, toward him, which I indeed came to feel, but as a means to truly recognizing the good that Hashem had bestowed.