Rabbi Elazar said: a person should always offer prayer before trouble arrives. For had Abraham our father not anticipated trouble with prayer between Beth-El and Ai, not a remnant or a survivor of Israel would have been left. And so it says, "Will your cry avail, that you be not in distress?" (Job 36:19).
Pray Before the Trouble Comes Like Abraham at Ai
Yalkut Shimoni on Nach 18:8
אמר רבי אלעזר לעולם יקדים אדם תפלה לצרה שאלמלי לא הקדים אברהם אבינו תפלה בין בית אל ובן העי לא נשתייר מישראל שריד ופליט, וכן הוא אומר היערוך שועך לא בצר: