Anas bin Malik reported,

The Prophet ﷺ said, "Allah عز وجل has assigned to the womb an angel who says, 'My Lord, a drop of fluid. My Lord, a clinging clot. My Lord, a lump of flesh.' When Allah wills to complete his creation, the angel asks, 'Male or female? Wretched or blessed? What is his provision? What is his lifespan?' All of this is then written while he is in his mother's womb."

Azami 1002

Sahih

عن أنس

أن النبي ﷺ قال: إن الله عز وجل وكل بالرحم ملكا يقول: يا رب نطفة، يا رب علقة يا رب مضغة، فإذا أراد أن يقضي خلقه قال: أذكر أم أنثى؟ شقي أم سعيد؟ فما الرزق والأجل؟ فيكتب بطن أمه.

الأعظمي 1002صحيح
Contents
Reference
Commentary
Ayat
Collection
Jami al-Kamil
Book
Faith
Chapter
Belief in the divine decree and predestination
Section
How the human is created in his mother's womb and the recording of his provision, lifespan, deeds, misery, and happiness
Numbering
Jami al-Kamil 1002 (2nd edition)
Azami 1002