Counterterrorism officials regard the Libyan branch as the Islamic State’s most dangerous affiliate, one that is expanding its territory and continuing to mount deadly attacks, including several this month. But to stop its advance, the United States and its European allies have been forced to court unreliable allies from among a patchwork of Libyan militias that remain unaccountable, poorly organized and divided by region and tribe.

Image Gen. Khalifa Hifter in Libya in March 2015. His Libyan National Army is one of several armed factions vying for power. Credit... Mohammed El-Sheikhy/Associated Press

The search carries particular risks for the Obama administration, which once relied on local militias to help protect the American diplomatic compound in the northeastern city of Benghazi. They failed to provide protection when militants overran the compound in September 2012, an attack that led to the deaths of the United States ambassador, J. Christopher Stevens, and three other Americans.

Analysts also warn that any foreign effort to empower individual proxy forces could fuel new rivalries as the United Nations tries to bring together Libya’s warring factions after years of civil war that followed the toppling of Col. Muammar el-Qaddafi in 2011.

The mix-up at the base, about 70 miles from Tripoli, came during a time of growing alarm in Washington and in European capitals about the rise of the Islamic State’s wing in Libya. American spy agencies say the Islamic State, also known as ISIS or ISIL, has in the past few months redirected several hundred foreign fighters originally bound for Syria to its camps in Libya.

In November, an American airstrike killed the Islamic State’s senior leader in Libya, Abu Nabil, an Iraqi national who led Qaeda operations in western Iraq from 2004 until 2010, American officials said.