This is one of the most important questions a reader of this verse can ask, and the Torah itself answers it. The verse says God is "visiting the iniquity of parents upon children and children's children, upon the third and fourth generations" [S1]. Read alone, that sounds as if a grandchild pays for a grandfather's sin. But the Torah rules out that reading in its own law code: Deuteronomy 24:16 says "Parents shall not be put to death for children, nor children for parents; each shall be put to death only for their own sin" [S2]. And the prophet states the principle in full: Ezekiel 18:20, "The person who sins shall die. A child shall not bear the iniquity of the parent, nor the parent the iniquity of the child" [S3].
So how do the verses fit together? Rashi on our verse gives the classical answer: God visits the parents' iniquity on the children only "when they hold onto the deeds of their ancestors in their hands," meaning when they continue the same sins. Rashi proves it from the parallel verse in the Ten Commandments, which adds the words "of those who hate Me" (Exodus 20:5) [S4]. The generations are not punished for someone else's guilt; they are judged for their own choice to carry the family's wrongdoing forward. A child who breaks the chain is not touched by it at all.
There is also a comfort hidden in the numbers. Rashi notes that "keeping kindness to the thousands" means at least two thousand generations, while punishment reaches only the fourth. So the measure of God's goodness outweighs the measure of His judgment by five hundred to one [S4]. The verse that seems to threaten is really teaching how much longer God's kindness lasts than His anger.
2 answers
go deep