Cao Yu was a son of Cao Cao and his concubine Lady Huan. He had two brothers who were also born to Lady Huan – Cao Chong and Cao Ju. He was enfeoffed as a Marquis of a Chief District in 211 during the reign of Emperor Xian in the Eastern Han dynasty, and was promoted to a county marquis under the title "Marquis of Luyang" in 217. In 221, a year after Cao Yu's elder half-brother, Cao Pi, ended the Han dynasty and established the state of Cao Wei, Cao Yu became a duke, and was enfeoffed as the Prince of Xiapi in the following year. In 224, his princedom was changed from Xiapi to Shanfu County. In 232, during the reign of Cao Rui, Cao Yu's title was changed to "Prince of Yan". Cao Rui had been very close to Cao Yu in his childhood and he favoured the latter, so he treated Cao Yu exceptionally after ascending the throne in 226 upon his father's death. In 235, Cao Yu was summoned to serve in the Wei imperial court. He returned to Ye in 237, but was summoned back to the Wei capital Luoyangin the summer of 238. Cao Rui became seriously ill in the winter of 238–239 and he appointed Cao Yu as General-in-Chief in the hope that Cao Yu would assist his successor in governing the state. After serving as General-in-Chief for a few days, Cao Yu had the intention of resigning. At the same time, Cao Rui had also changed his mind so he relieved Cao Yu of his duty. Cao Yu returned to Ye in the summer of 239 after Cao Rui's death. In 260, after the fourth Wei ruler Cao Mao was killed in a failed coup against the regentSima Zhao, Cao Yu's son Cao Huan, the Duke of Changdao District, was chosen to be the new emperor and he ascended the throne shortly after Cao Mao's death. Cao Yu remained as the Prince of Yan when his son was a nominal emperor under Sima Zhao's control. The number of taxable households in his princedom increased throughout the reigns of Cao Rui, Cao Fang, Cao Mao and Cao Huan until it reached 5,500. On 4 February 266, Sima Zhao's son Sima Yan forced Cao Huan to abdicate the throne in his favour, ending the state of Cao Wei and establishing the Jin dynasty. Sima Yan became the emperor and he granted Cao Huan the title "Prince of Chenliu". Cao Yu's status was reduced from that of a prince to a duke, so he became known as the "Duke of Yan".