WASHINGTON ― With Iraq and Syria ablaze, the oil-rich kingdom of Saudi Arabia seems almost an afterthought. But Riyadh will be a crucial, if quixotic, ally as the U.S. seeks to mobilize Sunni Muslims against the terrorist Islamic State.

The kingdom’s many critics argue that Saudi Arabia itself helped spread the toxic virus by bankrolling Islamist rebels and their extremist Salafist Muslim ideology. As if to insulate itself from such criticism, the kingdom recently donated $100 million to a new U.N. counterterrorism center, and its senior religious leader, the grand mufti, declared the Islamic State and its al-Qaida forebear “enemy No. 1 of Islam.”

Complicating Saudi Arabia’s pivotal role in containing regional instability is the fact that generational change is slowly coming in the kingdom, too. The stakes for the U.S. in this leadership transition are large, and the outcome is hard to predict.

King Abdullah remains in power, a generally popular and respected monarch. But at 90, his energy and attention span are limited. Tensions have surfaced at several Saudi ministries over the last year, suggesting a jockeying for power.

For a generation, Americans and Saudis have worried that the kingdom was a potential tinderbox, with Muslim and secular extremists vying to undermine the conservative monarchy. If anything, the kingdom seems slightly more stable now than a decade ago ― but Sunni and Shiite extremists, otherwise deadly adversaries, share a common dream of toppling the House of Saud.

The inner workings of the royal family remain all but impenetrable to outsiders. The senior princes are slow-moving, self-protective and resistant to foreign counsel ― traits that invite speculation about what’s happening behind the palace walls. But whatever their internal disagreements, the sons and grandsons of King Abdul Aziz, the kingdom’s modern founder, have been able to maintain the family consensus necessary to preserve their rule.

U.S. and Arab experts describe a kingdom that is worried about three dangers: the rise of Iran and its Shiite Muslim allies; the resurgence of Sunni extremism embodied by the Islamic State; and the reliability of the United States, the kingdom’s protector, which is seen by many Saudis as a superpower in retreat.

The unsettled situation is illustrated by the mercurial Prince Bandar bin Sultan. He was ousted as intelligence chief last April, then rehabilitated this summer with the honorific title of chairman of the national security council. The outcome is probably a net gain for Saudi stability: Khaled bin Bandar bin Abdul Aziz, the new chief of the spy service, is seen as a more reliable and professional operator; he works well with Prince Mohammed bin Nayef, the interior minister who is trusted by the U.S.

The new spy chief and the interior minister, accompanied by Bandar and Saudi Foreign Minister Saud al-Faisal, traveled to Qatar this week, presenting a common front to a regional rival that has often bedeviled Saudi and U.S. policy.

One question mark has been Crown Prince Salman, 78, the defense minister, who is reportedly in poor health. Speculation about succession was fueled by the appointment of Prince Muqrin as deputy crown prince last March. Meanwhile, Salman has struggled to run the defense ministry. Since assuming that post in November 2011, he has had four deputies, including two sons of his predecessor, Prince Sultan.

The wild card in the Saudi deck is Bandar, the flamboyant former ambassador to Washington. When he was head of Saudi intelligence and paymaster to Saudi allies in Syria and Lebanon, he was an unpredictable ― and in Washington’s eyes, sometimes untrustworthy ― operator.

Some Americans feared Bandar’s covert efforts in the Syrian civil war were unintentionally spawning al-Qaida terrorists. U.S. officials were relieved when Bandar was removed as steward of the Syrian opposition.

It has been Saudi Arabia’s recurring nightmare to fight external enemies by encouraging Sunni movements that turn extremist and threaten the kingdom itself. This happened in the 1980s, when the Saudis joined the CIA in sponsoring the mujahedeen in Afghanistan. The devout Muslim fighters drove out Soviet troops but evolved into the Taliban and al-Qaida.

The Saudis must worry that a similar process has happened again. Some of the Sunni fighters they backed against Iran have drifted toward the Islamic State. The Saudis didn’t intend the ensuing disaster, but they must now deal with it.

Western analysts credit Mohammed bin Nayef and Khaled bin Bandar for seeking to build more competent, professional security services at Interior and Intelligence. They’ll need that skill, and luck, too. For Saudi Arabia, big challenges lie just over the horizon.