Eighteen Songs of a Nomad Flute (Chinese: ; pinyin: ) are a series of Chinese songs and poems about the life of Han Dynasty (202 BCE - 220 CE) poet Cai Wenji, the songs were composed by Liu Shang, a poet of the middle Tang Dynasty. Later Emperor Gaozong of Song (1107-1187) commissioned a handscroll with the songs accompanied by 18 painted scenes.
Poet and composer Cai Yan, more commonly known by her courtesy name "Wenji", was the daughter of a prominent Eastern Han man of letters, Cai Yong. The family resided in Yu Prefecture, Chenliu Commandery, in what is now eastern Henan Province. Cai Wenji was born shortly before 178 CE, and was married at the age of sixteen according to the East Asian age reckoning (corresponding to the age of 15 in Western reckoning) to Wei Zhongdao in 192 CE. Zhongdao died soon after the wedding, without any offspring. 194-195 CE brought Xiongnu nomads into the Chinese capital and Cai Wenji was taken, along with other hostages, into the frontier. During her captivity, she became the wife of the Zuoxianwang ('Leftside Virtuous King' or 'Wise King of the Left'), and bore him two sons. It was not until twelve years later that Cao Cao, the Chancellor of Han, ransomed her in the name of her father, who had already died before her capture. When Cai Wenji returned to her homeland, she left her children behind in the frontier.
A number of poems have been written to immortalize Cai Wenji's life story including her own. Liu Shang (c. AD 770), wrote a poem about Cai Wenji called Hujia Shiba Pai (; "Eighteen Songs on a Nomad Flute"). Liu Shang's poem accompanied by 18 scenes painted on a handscroll was commissioned by the first emperor of the Emperor Gaozong of Song. Four fragments, presumed to be of the original, are in the Boston Museum of Fine Arts, while the earliest complete copy, apparently from the fourteenth century and formerly in the C. C. Wang collection, resides in the Metropolitan Museum of Art; these were published by the museum in 1974 in a book entitled Eighteen Songs of a Nomad Flute.
Chinese allegorical devices, as defined by Erich Auerbach, are "something real and historical which announces something else that is also real and historical...the relation between the two events is related by an accord or similarity". There are obvious parallels between Cai Wenji's story and that of Gaozong's mother, the Empress Dowager Wei (), who was captured along with the rest of the imperial clan and held hostage in the north. She was not released until a peace treaty was concluded between the Song Dynasty and the Jurchens in 1142. Despite its allegorical development derived from Cai Wenji's story, her image today reverberates primarily with the feeling of sorrow.