Tang campaigns against Karasahr

The Tang campaigns against Karasahr were two military campaigns sent by Emperor Taizong of the Tang dynasty against the Tarim Basin kingdom of Karasahr, a vassal of the Western Turkic Khaganate. The city-state, which later became part of Xinjiang), may have been known to its inhabitants by the Tocharian name Agni, which was rendered Yanqi in Chinese sources. The first campaign in 644 was led by the Tang commander Guo Xiaoke, protector-general of the Anxi Protectorate in western China, who defeated the oasis state and a Western Turkic army and installed a Tang loyalist as ruler. The second campaign in 648, which was part of the campaign against Karasahr's neighboring state of Kucha, was led by a Turkic general of the Tang dynasty, Ashina She'er, who defeated and conquered Karasahr.

Background
Karasahr, a kingdom in the Tarim Basin, became a tributary state of Tang China in 632. In 632, the nearby oasis states Kashgar and Khotan surrendered to Chinese suzerainty, as did the kingdom of Yarkand in 635. Karasahr was influenced by Persian culture, Buddhism, and the Greco-Buddhist culture of Afghanistan.

The rulers of Karasahr grew uneasy as the Chinese expanded into Central Asia. After the conquest of the city of Turfan, center of the kingdom of Gaochang, the Chinese army stationed nearby was seen as a direct threat. This is a little ironic, because Gaochang's military aggression against Karasahr (a response to Karasahr's developing a trade route to China that bypassed Gaochang) had earlier served as one of the pretexts for the Tang campaign against Gaochang.

644 campaign
Karasahr ended its tributary relationship with the Tang dynasty and formed a marriage alliance with the Western Turkic Khaganate, its previous suzerain. The state of Kucha, although technically a Tang vassal, aided Karasahr's rebellion in 644. Fearing Tang military ambitions, Kucha also ended its status as a Chinese tributary state. The Tang emperor responded by sending an army led by commander Guo Xiaoke, protectorate-general of the Anxi Protectorate, against the kingdom. Long Lipozhun, a brother of the former king, defected to the Tang and served as Guo's guide. Guo planned a surprise attack, marching towards the city from the Yulduz, and attacked the kingdom at dawn. The city fell and the king Long Tuqizhi was captured when the Tang troops swam across the moat that surrounded the city. Guo Xiaoke installed Long Lipozhun as ruler and led his army back toward Gaochang with Long Tuqizhi as a captive. The Western Turks sent 5,000 cavalry to assist Karasahr. They arrived three days after Guo's departure and captured Long Lipozhun. They then pursued Guo Xiaoke's army, but were defeated by the Tang troops. One of the Western Turk leaders later sent a tudun to rule Karasahr, but the tudun pulled out after receiving threats from Tang Taizong. The people of Karasahr then installed Long Lipozhun's cousin Xue Apo'anazhi as their king and remained vassals of the Western Turks.

648 campaign
The Tang general Ashina She'er, a member of the Turkic Ashina royal family, was sent to attack Kucha with 100,000 Tiele auxiliary cavalry in 648. Although the Tang expedition did not attack Karasahr directly, the pro-Turk king fled his capital city and attempted to hold off the Tang forces on the eastern borders of Kucha. The Tang expedition pursued, captured, and executed him. Another pro-Tang Karasahr aristocrat, Lipozhun's cousin Xiannazhun, was then installed as ruler of Karasahr.

Aftermath
Karasahr became one of the Tang empire's Four Garrisons of the Pacified West (the others being Kucha, Khotan, and Kashgar) when the headquarters of the Protectorate of the Pacified West was moved from Gaochang to Kucha in 658 or 659, but it is not clear whether any Tang troops were stationed in Karasahr between 648 and 658. The Tang pulled out of Karasahr in 670 as a result of a Tibetan invasion of the Tarim Basin. Tang forces regained the Tarim Basin in 692 but lost it to the Tibetans for the second and last time in the 790s, by which time the Tarim Basin had been cut off from the Tang empire by the Tibetan conquest of the Gansu Corridor in the 760s.