His grandfather Wei Jin (韋津) was an official for Northern Zhou's successor and Tang's predecessor Sui dynasty and, after Sui's destruction in 619, served one of the competing claimants for the throne, Wang Shichong the Emperor of Zheng, before eventually surrendering to and serving Tang's founder Emperor Gaozu.
It is not known when it occurred, but at some point Wei Anshi passed the imperial examination and served as the sheriff of Qianfeng County (乾封, in modern Tai'an, Shandong).
[1] In 700, by which time Wu Zetian was carrying the title of "emperor" (after having had Emperor Ruizong yield the throne to her in 690, establishing a new Zhou dynasty and interrupting Tang), Wei Anshi was recalled to then-capital Luoyang to serve as Wenchang You Cheng (文昌右丞), one of the secretaries general at the executive bureau of government (文昌臺, Wenchang Tai), but was soon made Luantai Shilang (鸞臺侍郎), the deputy head of the examination bureau (鸞臺, Luantai) and given the designation Tong Fengge Luantai Pingzhangshi (同鳳閣鸞臺平章事), making him a chancellor de facto.
There was an occasion when, at an imperial feast that Wei was attending, Zhang Yizhi brought in several merchants (who were considered of low status under traditional Chinese thinking of the time), led by one Song Bazi (宋霸子), and began gambling with them.
Soon after Emperor Zhongzong's restoration, Wei Anshi was made the minister of justice (刑部尚書, Xingbu Shangshu), but was no longer a chancellor.
Two months later, however, as part of a government reorganization, he was made the minister of civil service affairs (吏部尚書, Libu Shangshu) and given the designation Tong Zhongshu Menxia Sanpin (同中書門下三品), again making him chancellor de facto.
Shortly after Emperor Ruizong's return to the throne, Wei Anshi was removed from his post as Shizhong and made an advisor to Li Longji, now crown prince, as well as Zuo Pushe (左僕射), one of the heads of the executive bureau (now named Shangshu Sheng (尚書省)).
Traditional historians believed that Princess Taiping was beginning to gather a faction of officials around her, intending to find fault with Li Longji as to remove him from being crown prince.
It was said that under Wei's and Li Rizhi's oversight, the civil service affairs, which had fallen into a state of confusion during Emperor Zhongzong's reign and which Song and Yao had tried to streamline, again became unwieldy.
Four months later, he was made advisor to Li Longji and Zuo Pushe (左僕射, also a head of the executive bureau), but retained chancellor status with the designation Tong Zhongshu Menxia Sanpin.
Three months later, as part of a major governmental reorganization, Wei, along with Guo, Dou Huaizhen, Li Rizhi, and Zhang Shuo, were removed from their chancellor posts, with Wei acting only as Zuo Pushe and also put in charge of Luoyang (the capital having been moved back to Chang'an when Emperor Zhongzong returned to the throne).
These former chancellors were all demoted, with Wei Anshi reduced to being the secretary general of Mian Prefecture (沔州, in modern Wuhan, Hubei).
Later in the reign of Emperor Xuanzong, when his son Wei Zhi (韋陟) had become a powerful official, he was posthumously conferred a number of honors, including the restoration of the title of Duke of Xun.