And the men rose up from there, and looked toward Sodom." (Gen. 18:16) Rabbi Elazar said: Come and see how G‑d shows goodness to all beings, but especially to those who follow His path. Even when He decides to judge the world, He arranges for anyone who loves Him to do a mitzvah before the world is punished.
...He arranges for anyone who loves Him to do a mitzvah before the world is punished.
This corresponds to what we have learned. When G‑d loves a person, He sends him a gift. And what is this gift? It is a poor man
[related to the Shechinah, called a poor man] for whom he can do a mitzvah. So now that he is meritorious and deserving, G‑d draws a cord of grace from the right side upon him. He winds the cord around his head and marks him, so that when judgment falls on the world, the Angel of Destruction will take care not to harm him. Because he raises his eyes and notices that mark, he avoids him. This is why G‑d arranged beforehand to make him meritorious.

Come and see! When G‑d planned to execute judgment on Sodom, He first gave Avraham the opportunity to make himself meritorious by sending him a gift
[the three wayfarers] to give him merit through them. [Because of the three angels who enabled his merits of hospitality, he became deserving.] This was in order to save Lot, the son of his brother, from destruction. This is why it is written: "and G‑d remembered Abraham, and sent Lot out of the midst of the overthrow," (Gen. 19:29) and not, 'and G‑d remembered Lot.' As a result of Abraham's mitzvah, he was saved and "remembered" - He remembered the previous meritorious act that Abraham performed with the three angels.
Every time a person performs a mitzvah it is noted above...
In the same manner, when harsh judgment hangs over the world, G‑d remembers the charitable deeds that men performed. Every time a person performs a mitzvah it is noted above; even when there is harsh judgment on the world, G‑d remembers the good that a person has done and has merited through other people. As it is written: "but charity delivers from death." (Proverbs 11:4) According to this, G‑d arranged in advance that opportunity for Abraham to do a mitzvah, so that by his merit Lot would be saved.

BeRahamim LeHayyim:

A mitzvah brings forth another mitzvah. Who would have known that this nice saying also is a law of spiritual science, everything we do is recorded, seen, and heard, by supernal sources. We are specially blessed in Abraham's merit of leaving the presence of G‑d while in terrible pain, three days after his circumcision in the blistering desert heat of midday, to be hospitable to three Arab-looking strangers? And as for us, what have we done lately?

Bracketed annotations from Metok Midevash and Sulam commentaries
Copyright 2003 by, a project of Ascent of Safed (// All rights reserved, including the right to reproduce this work or portions thereof, in any form, unless with permission, in writing, from Kabbala Online.