During Emperor Gaozu's reign
In 617, the governor of Taiyuan Li Yuan rebelled against the rule of Emperor Wen's son Emperor Yang, and by 618 had established the Tang dynasty at Chang'an (as its Emperor Gaozu). Du Ruhui served on the staff of Emperor Gaozu's son Li Shimin the Prince of Qin, a major general. At that time, Li Shimin had many talented people on his staff, and soon, many were commissioned to be local officials. Du was scheduled to become the secretary general for Shan Prefecture (陝州, roughly modern Sanmenxia, Henan), when Li Shimin's trusted advisor Fang Xuanling pointed out that Du was highly talented and that Li Shimin could not afford to lose him. Li Shimin thus interceded with Emperor Gaozu to keep Du on his staff. Du later followed Li Shimin in campaigns against Tang's major enemies Xue Rengao the Emperor of Qin, Liu Wuzhou the Dingyang Khan, Wang Shichong the Emperor of Zheng, and Dou Jiande the Prince of Xia, contributing strategies to Li Shimin's campaigns. For his achievements, Du was created the Baron of Jianping. (When the Zheng capital Luoyang fell to Li Shimin in 621, Du Ruhui's uncle Du Yan, a Zheng official, was captured. Du Yan was not on friendly terms with Du Ruhui and his brothers and who had given information to Wang that Du Ruhui's older brother was not faithful to Zheng, causing Wang to execute Du Ruhui's older brother and to imprison Du Ruhui's younger brother Du Chuke. Du Ruhui was originally intending to allow Li Shimin to execute Du Yan, but Du Chuke reasoned with him that even though Du Yan had caused their brother's death, they should not in turn cause an uncle's death. Du Ruhui relented and interceded on Du Yan's behalf, and Du Yan was spared. Du and Fang continued to serve as key advisors for Li Shimin, and it was said that Fang was capable of planning and strategizing, but was not resolute, while Du was capable of making quick and correct decisions, and they divided their strategical responsibilities while on Li Shimin's staff in that manner. (This eventually led to the Chinese proverb "Fang plans and Du decides" (房謀杜斷, Fang mou Du duan), after they became renowned.)
Later in Emperor Gaozu's reign, Li Shimin was locked in an intense rivalry with his older brother Li Jiancheng the Crown Prince, and Fang and Du often suggested that he act first against Li Jiancheng. As both Li Jiancheng and another brother who supported Li Jiancheng, Li Yuanji the Prince of Qi, feared Fang's and Du's strategic capabilities, they falsely accused both Fang and Du and had them demoted out of Li Shimin's staff. In summer 626, when Li Shimin decided to act against Li Jiancheng and Li Yuanji, however, he summoned Fang and Du to his mansion. Fang and Du, initially fearing Emperor Gaozu's orders forbidding them to serve Li Shimin, declined. In anger, Li Shimin sent the general Yuchi Gong to summon Fang and Du, with directions that if they declined again, to kill them. Yuchi, however, was able to persuade them that Li Shimin was in fact intending on acting against Li Jiancheng and Li Yuanji, and so Fang and Du put on disguises as Taoist monks and were able to get to Li Shimin's mansions, where they assisted Li Shimin in planning the ambush against Li Jiancheng and Li Yuanji. Li Shimin subsequently ambushed his brothers at Xuanwu Gate and killed them, and then effectively forced Emperor Gaozu to create him crown prince. Once he was created crown prince, Fang and Du were restored to honored positions on his staff, and soon, Du was made the minister of defense. Two months later, Emperor Gaozu yielded the throne to Li Shimin (as Emperor Taizong).