An Lushan
rebelled against the rule of
Emperor Xuanzong of Tang in late 755, and Li Huaixian, serving under An, participated in An's campaign to capture Tang's central territories, on which An established a state of
Yan as its emperor. In spring 756, Li participated in the campaign led by fellow Yan general Linghu Chao () against the city of
Yongqiu, defended by the Tang general
Zhang Xun, but the Yan generals were unable to capture Yongqiu and eventually forced to withdraw. He later successively served under the next three Yan emperors – An Lushan's son
An Qingxu, An Lushan's major general
Shi Siming, and Shi Siming's son
Shi Chaoyi. After Shi Chaoyi had succeeded to the Yan throne after assassinating his father Shi Siming in 761, he made Li the mayor of the key city
Fanyang. According to the Tang dynasty historian Ping Zhimei (), whose
Jimen Jiluan () is no longer extant but is often cited in other works, when Li arrived at Fanyang, Fanyang was in a state of disturbance after various Yan generals in Fanyang had fought and killed each other in street battles in the confusion after Shi Siming's death, and it was Li who put down the disturbance and restored order. After a joint Tang and
Huige army decisively defeated Shi Chaoyi and recaptured Shi Chaoyi's capital
Luoyang, Shi Chaoyi fled north toward Fanyang. Before Shi Chaoyi reached Fanyang, however, Li sent messengers to the powerful Tang eunuch Luo Fengxian () and offered to submit to Tang. He sent his subordinate Li Baozhong () to Fanyang County (a city near, but not the same, as Fanyang) to block off Shi Chaoyi's path, and Shi Chaoyi, after being unable to capture Fanyang County, fled further north. Li Huaixian sent an army to pursue him, and Shi Chaoyi, believing the situation to be hopeless, committed suicide. Li Huaixian cut off Shi Chaoyi's head and presented it to Emperor Xuanzong's grandson
Emperor Daizong, ending the Anshi Rebellion. == After the An-Shi Rebellion ==