The core obligation is codified by Rambam (Tefillah 2:13) and Rambam (Berachot 2:6), who rule that on Chanukah and Purim one inserts Al HaNissim in the blessing of thanksgiving (Hoda'ah) in the Amidah and in the blessing for Eretz Yisrael in Birkat HaMazon, and that this addition is also made in the Musaf of a Shabbat falling during Chanukah even though there is no special Musaf for Chanukah itself.
The Gemara in Shabbat 24a:2 records the dispute over whether Chanukah must be mentioned at all in Birkat HaMazon, since it is only a rabbinic obligation, and concludes that one need not mention it, but if one does, it is inserted specifically in the blessing of Hoda'ah — the same placement later codified in halacha.
Shulchan Arukh (Orach Chayim 682:1-2) and the Tur (Orach Chayim 682) fix the practical details: Al HaNissim is said throughout the eight days of Chanukah in both Birkat HaMazon and the Amidah, one who omits it is not required to repeat the prayer or blessing (unless he has not yet said God's name in that blessing), and it must also be mentioned in the Musaf of a Shabbat or Rosh Chodesh that falls during Chanukah; the Tur additionally notes a dispute over whether the formula opens with 'just as You did' or simply continues with 'and You performed for them miracles.'
Shulchan Arukh (Orach Chayim 682:3) specifies the closing language of the text itself, ruling that one does not say 'just as You did' but concludes 'and You performed for them miracles and mighty deeds in those days at this time,' though some say the former formula as well.
Mishnah Berurah (682:1-3) explains that the reason Al HaNissim belongs specifically in the blessing of Hoda'ah is that the entire theme of Chanukah is fundamentally one of thanksgiving, and he notes that it is not mentioned in the Me'ein Shalosh blessing after food, and that failure to say it does not require repetition since the obligation is rabbinic rather than Scriptural.
For Purim, Shulchan Arukh (Orach Chayim 693:2) rules that Al HaNissim is said both at night and during the day of the fourteenth but not on the fifteenth, and that on the night of the fourteenth it is said even before the Megillah has been read; Mishnah Berurah (693:5-6) adds that omitting it on the fifteenth is not required because that day has no inherent connection to the miracle, while Mishnah Berurah (695:15-17) discusses the particulars of saying it in Birkat HaMazon depending on which meal of Purim one is eating.
Contemporary halachic digests continue to refine wording and scope: Kitzur Shulchan Arukh Yalkut Yosef (Orach Chaim 682:1) rules that Al HaNissim is said at Ma'ariv on the first night of Chanukah even before candles have been lit at home, and likewise on the night of Purim before the Megillah is read; Kitzur Shulchan Arukh Yalkut Yosef (Orach Chaim 682:21) and Kitzur Shulchan Arukh Yalkut Yosef (Orach Chaim 695:7) require the plural formulation 'nissim ve-niflaot' rather than the singular, corresponding to the two miracles of each holiday; and Kitzur Shulchan Arukh Yalkut Yosef (Nuschaot HaTefillah 134:68) rules that the correct text begins with the connecting vav, 've'al ha-nissim,' continuing the preceding passage of thanksgiving.
Rav Eliezer Melamed (Peninei Halakhah, Zemanim 11:8) summarizes the practical halacha for Chanukah, stating that Al HaNissim is recited in the Hoda'ah blessing of the Amidah and in the second blessing of Birkat HaMazon but not in Me'ein Shalosh, that one who forgets need not repeat the blessing once God's name has been said, and that it is nonetheless preferable to add it at the end of the Amidah or Birkat HaMazon among the supplementary supplications; separately, Rav Eliezer Melamed (Peninei Halakhah, Zemanim 12:5) situates this thanksgiving formula alongside the two candle-lighting blessings, the second of which — 'she'asah nissim la'avoteinu' — expresses the same thanks for the miracles being recalled through Al HaNissim.
The historical background for why such thanksgiving was instituted is drawn from Shabbat 21b:10, which recounts the miracle of the single cruse of oil lasting eight days as the basis for fixing these days 'with Hallel and thanksgiving (Hoda'ah),' the same Hoda'ah later specified as the home of Al HaNissim; and from Esther 9:20-22, which records the institution of Purim as days of feasting, joy, and gift-giving in gratitude for the deliverance, paralleling Daniel 2:20-23, where Daniel models the language of blessing and thanking God for revealing wisdom and might in time of crisis.
Megillah 14a:4-6 records that the Sages instituted the reading of the Megillah as a unique rabbinic addition to the Torah's mitzvot precisely as an act of praise for deliverance from death to life, while Rambam (Megillah and Chanukah 3:6) clarifies that Hallel was not instituted for Purim because the reading of the Megillah itself serves that function — framing why Al HaNissim, rather than Hallel, became the operative mode of thanksgiving on Purim.
Rav Mordechai Eliyahu (Maamar Mordechai, Hilchot Chanukah 1:5) explains that the Sages instituted two distinct responses to the two miracles of Chanukah — lighting candles in memory of the miracle of the oil and the restoration of Temple service, and reciting Al HaNissim together with the full Hallel in memory of the military victory — and in Maamar Mordechai (Hilchot Chanukah 9:6) he adds, citing the Meiri, that when reciting 'she'asah nissim' one should have in mind both the miracle of the oil and the miracle of the war, noting that on the first night, before the oil-miracle had occurred, the blessing was understood to refer to the redemption and discovery of the flask itself.
Several Chassidic and Acharonic sources explore the deeper structure of the text: Likkutei Sichos 15, Chanukah analyzes why 'Haneirot Halalu' lists 'teshuot,' 'nissim,' and 'niflaot' in a chronological sequence reflecting the order the events actually occurred; Peri Tzadik (Chanukah 14:1) explains why Chanukah and Purim mention 'in those days' in prayer though the Shemot is not invoked the same way, since Chanukah's and Purim's miracles came through Israel's own initiative rather than direct Divine command through a prophet like Moshe; and Sefat Emet (Bereishit, For Chanuka 30:5) and Sefat Emet (Bereishit, For Chanuka 12:7) similarly explain why the formula 'she'asah nissim la'avoteinu bazeman hazeh' was composed only for Chanukah and Purim and not for Pesach, since those miracles were bound to a particular historical moment rather than eternally fixed in the Written Torah.
Sefat Emet (Shemot, For Purim 15:2) likewise addresses why the blessing 'she'asah nissim bazeman hazeh' is said on Purim despite the fact that the primary miracle, on this reading, may actually have occurred near Pesach, resolving that the hidden miracle becomes revealed specifically through the day's commemorative mitzvot.
Levush (Orach Chayim 676:1) records the three candle-lighting blessings recited on the first night of Chanukah — for the lighting itself, for remembrance of the miracle ('she'asah nissim'), and for the season (Shehecheyanu) — situating the Al HaNissim formula of the candles alongside its parallel in the prayer and Birkat HaMazon.
Among the sources recorded, not all bear directly: Megillah 17a:17 concerns the language in which the Shema may be recited, Turei Even (Megillah 14a) and Turei Even (Megillah 17a) and Turei Even (Megillah 4a) work through related Talmudic sugyot on Hallel and Megillah obligations, and Megillah 30a:16-17, Megillah 30b:3, Megillah 16b:8-9, Magen Avraham 685:1, Mishnah Berurah 218:7-9, Shabbat 23a:6-7, Sfas Emes al HaShas, Shabbat 24a, Rav Kook, Ein Aya Shabbat 2:5, Shenei Luchot HaBerit, Machshavot Charutz 17:1, Derekh Hashem IV:5, Maamar Mordechai 1:1, Maamar Mordechai 9:12, and Maamarim Melukatim, Zot Chanukah 5743 each touch on readings, candle-lighting blessings, or the broader character of Chanukah and Purim that surround the Al HaNissim practice described above.
Among sources read but not directly quotable, Revavos Ephraim (7:189) distinguishes the Al HaNissim obligation in prayer, instituted for thanksgiving and publicizing the miracle, from its recitation in Birkat HaMazon, which he treats as a separate question of whether a rabbinic occasion requires the standard insertion; Rav Gavriel Zinner (Nitei Gavriel, Purim 75) rules that one who omits or misplaces Al HaNissim generally need not repeat it, whether in prayer or in Birkat HaMazon; Az Nidberu (13:48) explains the Chanukah observance as commemorating the miracle of 'the many delivered into the hands of the few' and situates Al HaNissim as the vehicle of thanks and praise for it; Tzitz Eliezer (18:42) explains why the Purim formula omits the phrase 'just as You did' that appears in the Chanukah version, since Purim's observance, rooted in prophetic enactment and recorded in the Megillah, included additional mitzvot beyond thanksgiving; Maayan Omer (3:8:1) rules that in cities of doubtful status regarding ancient walls, Al HaNissim should be said on both the fourteenth and fifteenth of Adar; and Siddur Im Dach 90d and Avnei Yashfeh 6:102 likewise confirm that Al HaNissim is recited on both Chanukah and Purim before 've'al hakol,' and that omitting it does not require repeating the prayer, though it may be added within the closing Harachaman supplications; Kitzur Shulchan Arukh Yalkut Yosef, Nuschaot HaTefillah 134:124 further fixes the precise wording of the candle-lighting blessing itself as 'bayamim hahem bazeman hazeh.'