Tesla boss Elon Musk (L) walks with Shanghai Mayor Ying Yong during the ground-breaking ceremony for a Tesla factory in Shanghai on January 7, 2019. - Musk presided over the ground-breaking for a Shanghai factory that will allow the electric-car manufacturer to dodge the China-US tariff crossfire and sell directly to the world's biggest market for 'green' vehicles.

Tesla said on Thursday it signed an agreement with lenders in China for a 12-month facility of up to 3.5 billion yuan ($521 million) for the electric carmaker's Gigafactory in Shanghai.

The company broke ground on the factory in January, and a Shanghai city government official said on Wednesday that the facility is expected to be completed in May.

Tesla has said that the Gigafactory will cost around $2 billion.

Producing cars locally is likely to help the company minimize the impact of the U.S.-China trade war, which has forced Tesla to adjust prices of its U.S.-made cars in China.

Keeping prices in check will also help Tesla fend off competition from a swathe of domestic electric vehicle startups such as Nio Inc, Byton and XPeng Motors.

WATCH: How taxpayers have boosted Elon Musk and Tesla