The U.S. and Mexico have reached an agreement aimed at keeping the 4,000-person migrant caravan from reaching America.

The plan was developed over many months and included a request from Mexico that the United Nations High Commissioner for Refugees establish shelters along its border with Central American, a senior administration official told Fox News . The goal is to provide humanitarian aid to members of the caravan, and either prevent them from entering Mexico, or process them individually as refugees or as legal entrants.

[Also read: John Kelly, John Bolton trade expletives in immigration argument outside Oval Office]

Mexico has also agreed to take back any migrant who evades their processing system and makes it to the United States. In the past, the U.S. has been required to house those who crossed the border illegally until their cases could be heard, and only after could they return them to their country.

“Just today, the Mexican government, and this is a very important step, requested the intervention of the U.N., the Office of the High Commissioner on Refugees, to help Mexico review any asylum claims from the members of the caravan,” Mexican Ambassador to the U.S. Geronimo Gutierrez told Fox Thursday night as he explained the situation.

Gutierrez said the request will allow for those arriving at Mexico’s border with Guatemala, where the first members of the Honduran caravan arrived Thursday, to make claims they are seeking asylum. The ambassador also said it would ensure Mexico was respecting international law.

U.N. officials will assess refugees while they are staying at the shelters at Mexico’s southern border, and then decide which ones have legitimate claims to refugee status.

Once a person is deemed to have a legitimate claim, they could be placed in the U.S., or any other host country that has an agreement with the UNHCR.