Last month, the Houthis took over Yemen, delivering a blow to Washington’s counterterrorism strategy, which held out Yemen as one of the few bright spots in an otherwise chaotic Middle East. Adding insult to injury, as Washington took steps to shutter its embassy earlier this month, the Houthis took all U.S. embassy vehicles parked at the airport in Sanaa, and even confiscated the weapons of departing U.S. Marines.

The Houthis are not likely to benefit from a handful of M-16 rifles, especially in a heavily armed country that has long served as the region’s arms market. But with the collapse of the government, the Houthis have also inherited as much as $24 million in weapons that Washington has transferred to Yemen since 2004. Through the Defense Department’s “train and equip” program, the U.S. has also provided more than $400 million in equipment and supplies tor Yemen’s air force, special operation, border control, and coast guard. As the Congressional Research Service notes, these “funds have supported the Yemeni Air Force’s acquisition of transport and surveillance aircraft.”

This is not the first time that Washington has lost military investments intended for a Middle East ally, only to watch them find their way into the hands of hostile forces. Nor is the first time that the America made a bad bet on a weak or autocratic government whose demise was only a matter of time.

Consider Washington’s investment in the Palestinian Authority that backfired in 2007, when the terrorist group Hamas overran the Gaza Strip by force. Hamas conquered the Palestinian Authority’s security complex, which housed both the PA’s intelligence and military infrastructure. This was infrastructure that the U.S. had helped to create. After the compound was captured, Hamas claimed that it “acquired thousands of paper files, computer records, videos, photographs, and audio recordings containing valuable and potentially embarrassing intelligence information gathered by Fatah.” Washington feared that Hamas had gained access to important spy technology, as well as arms that the U.S. had paid Arab partners (such as Egypt) to provide to the PA.

That black eye for American policy paled in comparison to the loss of Iran in 1979. Leading up to the revolution, U.S. support to the Shah put him in “within reach of becoming the world’s fifth-most-advanced force in 1978.” Then, as Stephen McGlinchey notes, “the largest deployment of U.S. arms in one single country fell into the hands of angry mobs shouting ‘death to America and Israel’.” The only consolation was that, within a year, the weapons were deployed against Saddam Hussein’s Iraqi army in the Iran-Iraq war.

Perhaps the worst bad bet for the United States was the one we made recently in Iraq. More than $2 billion in weapons over the last decade, and $300 million in 2014 were all designed to help Iraq defend itself after the ill-fated U.S. invasion that toppled Saddam Hussein in 2003. When the Islamic State (IS) acquired swaths of land in Iraq and Syria in 2014, especially during their takeover of Mosul, they acquired “weapons stores of the 2nd and 3rd [Iraqi army] divisions in Mosul, the 4th division in Salah al Din, the 12th division in the areas near Kirkuk, and another division in Diyala,” which were all provided by the United States. ISIS also seized 52 American artillery weapons from the Iraqi military in July 2014; each weapon cost up to $500,000 each.

Much of the weaponry lost to IS has since been bombed in coalition air raids. But the hardware undeniably helped IS expand their footprint across Syria and Iraq.

Meanwhile, other American weaponry continues to leak from Iraq’s armed forces into the hands of Shi’ite militias under the influence of Iran’s Islamic Revolutionary Guard Corps (IRGC), which is a designated terrorist entity here in the United States.

The loss of our military investment in Iraq reflects unforced errors on at least two levels. There was the poorly planned intervention of 2003 that necessitated huge investments beyond what war planners could foresee. Making matters worse, America’s premature withdrawal without a viable plan for protect our sizeable investment of some $2 trillion has allowed for Iran to tighten its grip on the country.

The lessons from lost materiel in Yemen, the Palestinian Authority, Iran, and Iraq underscore a lesson not yet learned in Washington. It’s dangerous to put our weapons and intelligence into the hands of autocratic regimes or weak Middle East governments that lack the infrastructure or legitimacy to rule. It’s even more dangerous to pull our support before we are certain that these governments can keep what we give them.

Jonathan Schanzer, a former terrorism finance analyst at the U.S. Department of the Treasury, is vice president for research at Foundation for Defense of Democracies.

This article originally appeared in The Huffington Post.