Dou later served as a scribe for
Yang Xiu the Prince of Shu, a son of Sui's founding emperor
Emperor Wen, but when he saw that Yang Xiu was engaging in improper behavior, he resigned, and subsequently when Emperor Wen discovered Yang Xiu's improper deeds in 602 and subsequently deposed him, many of Yang Xiu's subordinates were punished, but Dou escaped punishment. In 608, during the reign of Emperor Wen's son
Emperor Yang, he became a senior official at the legislative bureau (內史省,
Neishi Sheng), but as he often pointed out problems with Emperor Yang's governance, he offended Emperor Yang and was demoted to be the director of civil service matters (考功郎中,
Kaogong Langzhong), and subsequently was removed from governmental office altogether. In 617, the general
Li Yuan the Duke of Tang rebelled and captured the capital
Chang'an, declaring Emperor Yang's grandson
Yang You the Prince of Dai emperor (as Emperor Gong). Li Yuan's deceased wife
Duchess Dou was a niece of Dou Wei's, and Li Yuan, who retained power as
regent, retained Dou on staff. At that time, the government was in a state of flux, and with Dou being knowledgeable with ceremonies and governmental organization, Li Yuan entrusted to him the task of reorganizing the government as well as preparing the ceremonies to have Yang You yield the throne to Li Yuan. Li Yuan compared him to the
Han dynasty official
Shusun Tong (叔孫通), who organized the ceremonies for
Emperor Gao of Han. == During the Tang dynasty ==