When the United States was born, Algiers was a greater military power. Its Ottoman-aligned sultans kept European slaves and deployed nine battleships and fifty gunboats to prey on merchant shipping in the Mediterranean. America had no international Navy, and isolationists among the Founding Fathers weren’t sure they ever wanted one. Yet North Africa was a place where Americans could make a fortune if they were willing to bear risks.

In October, 1784, Algerian pirates attacked the American ship Betsy; the assailants had “sabers grasped between their teeth and their loaded pistols in their belts,” according to an eyewitness description that the historian Michael B. Oren cites in his book “Power, Faith and Fantasy: America in the Middle East, 1776 to the Present.” (Oren is currently Israel’s Ambassador to the United States. He also wrote “Six Days of War,” a terrific account of the 1967 Arab-Israeli conflict.) The American sailors who surrendered were sent on to Morocco’s slave markets. Algerian brigands seized two other American ships a few months later and sent their crews into slavery, too. Even before it had ratified its Constitution, the United States was flummoxed by what it would later refer to as Middle Eastern terrorism.

Before 1776, American capitalists in the Mediterranean had relied on the British Navy for protection; the Revolution left these entrepreneurs exposed, and they grumbled about the loss of the perks of King George’s patronage. Oren quotes Benjamin Franklin: “If there were no Algiers, it would be worth England’s while to build one.” America’s new leaders asked France for help on the Barbary seas, but the French declined; they said they had their own interests in the region to look after. Oren’s implication is that it was Barbary terrorism that first awakened America’s leaders to their dependence on international commerce and their need to project national power overseas.

These days, of course, the American military cannot be described as reticent about facing groups of kidnappers or much else. This week, American transport planes are lifting a battalion of French soldiers and their equipment into Bamako, the capital of Mali, so that the French can battle Islamists who have seized control of the northern part of that country. (The French have reportedly asked for more help, but the Obama Administration has declined.) U.S. Navy gunships are today patrolling against pirates off the coasts of West Africa and the Horn of Africa. And in Algeria, diplomats are repatriating the bodies of the American workers who were killed during last week’s attack on a southern Algerian gas field by Al Qaeda-affiliated militants—an attack that resulted in the deaths of at least thirty-seven foreign hostages, according to Algeria’s government.

The terrorists who struck the sprawling Amenas field and attempted a mass kidnapping of foreign workers there were border-hoppers from at least five different countries; the militant who initially took charge of the international hostages at the gas plant was an Islamic radical from Canada, according to Algeria’s Prime Minister. The terrorists evidently planned a spectacular strike inspired by the “Die Hard” movies. They rolled up in pickup trucks with large coils of explosives; if they had managed to blow the field’s natural-gas wells, they might have created a fireball visible from outer space. To the plus ça change aspects of Maghreb kidnapping rackets they brought Imax production values.

Al Qaeda in the Islamic Maghreb (A.Q.I.M.), the Osama bin Laden-inspired franchise from which the attackers emerged, has its origins in a failed 1991 election in Algeria, when democracy swept Africa after the Cold War’s end. Algeria’s secular-leaning, corrupt, and brutal military refused to honor a victory at the polls won by a mostly peaceful Islamist movement called the Islamic Salvation Front. The Algerian suppression that followed touched off a civil war and spawned a fringe outfit of hardened, internationally minded Islamic radicals known as the Group for Preaching and Combat.

That organization carried out terrorism in France and later connected with bin Laden. In 2007, the group changed its name and adopted the fearsome Al Qaeda brand. Presumably, they believed that their new name came with a certain kind of value already attached to it. (A business-administration student like bin Laden might note that “preaching and combat” is a confusing pairing.) One of the group’s main revenue streams has been the kidnapping of tourists, aid workers, and businessmen. In that endeavor, the Al Qaeda global brand position has impact. When kidnappers associated with worldwide specialists in beheading videos demand a ransom, the targeted organization or government often pays. A.Q.I.M. reportedly has raised almost a hundred million dollars from ransoms in recent years; that will buy a lot of C-4 and rocket-propelled grenade launchers.

After a period of decline, A.Q.I.M. and its offshoots have revived themselves recently, in part because the governments ruling the Sahel desert—Algeria, Libya, Chad, Niger, Mali—are weak and corrupt. Their weaknesses are not the fault of the United States (France and Italy were the region’s colonial powers, if one is looking for foreigners to blame), yet the policies that America has lately employed to try to strengthen Sahel governments, and particularly their counterterrorism forces—to “build capacity,” in the Washington jargon—have at least partly backfired.

One of the lesser-known strands of President George W. Bush’s global war on terrorism was the Pan-Sahel Initiative, launched in 2002. American mentors tried to train up special counterterrorism units in Chad, Mali, Niger, and Mauritania. That program later expanded, renamed as the Trans-Sahara Counterterrorism Initiative. The new program reached more countries, including Algeria, and sought to add development aid and “strategic communications” to military training.

The Pentagon has yet to meet a military in a desperately poor or hopelessly corrupt country that it does not believe it can train and equip to a professional standard. Pentagon training has in fact strengthened and stabilized professional militaries in many developing countries. Yet some of the militaries that the United States has mentored in North and West Africa are best understood as criminal organizations that happen to wear pressed uniforms and epaulets. The better that some of these students learn to shoot while at Fort Benning, and the better the equipment they receive as favored clients, the more effective they become at their enduring vocations—drug smuggling, coup-making, and profitable collusion with pirates and terrorists.

Captain Amadou Sanogo, of Mali, was a longtime mentee of American trainers. He led a coup d’état against Mali’s weak democratic government early last year; after he seized power, reporters who interviewed him noticed that he proudly sported a United States Marine Corps pin on his uniform. As it turned out, Sanogo had been dispatched to the U.S. for training several times. Unfortunately, his skill as a mutineer ran “contrary to everything that is taught in U.S. military schools,” as a Pentagon spokeswoman later put it to the Agence France Presse.

Sanogo’s coup in Bamako, and the disarray in Mali that has followed ever since, made it easier for the overlapping militias of ethnic Tuaregs and Al Qaeda-influenced Islamists to expand and defend the territory they have seized in northern Mali. Some of those rebels have recently arrived in Mali with arms from Libya, from where they fled after the North Atlantic Treaty Organization’s military intervention against Muammar Qaddafi and the accompanying popular Libyan revolution.

In the sanctuaries in Mali that these radicals have maintained, they have opened training camps for like-minded compatriots. It was in one such camp that the A.Q.I.M. squad prepared for its spectacular assault on the gas field in Algeria. In the age of Barbary, as today, the most predictable aspect of war—and, for that matter, of American capacity-building—is that it will have unintended, unpredicted consequences.

Photograph by Anis Belghoul/AP.