I heard the Messenger of Allah ﷺ say, "The first thing Allah created was the Pen. He said to it, 'Write.' It asked, 'My Lord, what shall I write?' He said, 'Write the decrees of everything until the Hour comes.'"
سمعت رسول الله ﷺ يقول: إن أول ما خلق الله القلم، فقال له: اكتب. قال: رب وماذا أكتب؟ قال: اكتب مقادير كل شيء حتى تقوم الساعة.
Abu Dawud (4700) narrated it from Jafar bin Musafir Hudhali, who said, Yahya bin Hassan told us, Walid bin Rabah told us, from Ibrahim bin Abu Abla, from Abu Hafsa, who said, Ubada bin Samit said to his son, then he related the hadith.Its chain of transmission is hasan, due to the discussion surrounding Jafar bin Musafir, Abu Dawud's teacher, though he is hasan al-hadith. The discussion of this matter is treated at length in the Book of Faith, along with mention of other hadiths on the same subject.