It was not the way Pervez Musharraf would have wanted to go. Hugely unpopular at home and spurned by his traditional allies in the military, the Pakistani leader will be unlamented domestically.

Although his fall is due partly to his own bumbling errors, there is little doubt that his support for America's war on Islamist militants accounts for most of his unpopularity. Musharraf has been in the same position as a number of leaders around the world who have spent the years after 9/11 struggling to balance demands for cooperation with the US and the cost this exerts on their standing at home.

Take the Kenyan president, Mwai Kibaki, for example. A little-remarked facet of his travails at the ballot box late last year was the fact the Muslim population in that country solidly rejected him, thus tipping a close election in the opposition's favour.

The president's Party of National Unity (PNU) was routed in areas with a large Muslim population. It did not secure a single parliamentary or civic seat in the predominantly Muslim coastal island of Mombasa and fared equally dismally in other Muslim areas. In all these regions, the Kibaki administration's solid cooperation with the US in confronting Islamic militancy was the central election issue.

The Americans have long viewed Kenya as an important ally in their efforts to tackle extremism in the Horn of Africa. The US embassy bombings in Nairobi and Dar es Salaam in 1998 highlighted this threat. And after the September 11 attacks, the Bush administration ramped up its efforts to use Kenya as a launching pad for its anti-terror operations in the region.

Like Musharraf's regime, the Kibaki administration received millions of dollars in national security funds. And just as Pakistan gave the green light for US air force sorties in its restive North-West Frontier province, Kenyan bases are believed to have been used for repeated air raids in neighbouring Somalia, including one earlier this year in which a leading militant was killed.

Kenya, like Pakistan, has also handed over a number of militants to the US, such as Abdul Malik, a suspect in the 2002 attacks in a coastal resort in Mombasa, who was shipped off to Guantánamo Bay in March last year. This cooperation has come at considerable cost to the government.

The most recent developments in Pakistan – and more pertinently the negative reaction at the ballot box last year – could well lead to a reappraisal of engagement with the US.

In Kenya, local politicians have long counselled that a broader US investment in the capabilities of the corrupt and inefficient local police force would be more welcome than the current sole focus on terrorism. Few Kenyans lose sleep over terror attacks in the country but insecurity is a constant worry.

American partnership in helping set up a forensic laboratory for local police – a longstanding request held up by lack of budgetary support – is one example that would have benefits for both the US and Kenya.

Greater investment in training, and improved benefits such as insurance and healthcare, might help to curb corruption in the forces. This would simultaneously mean a more efficient anti-terror operation and possibly check the mysterious disappearing acts of suspects such as one of al-Qaida's most wanted men in Africa, Fazul Abdullah Mohammed, who has repeatedly eluded capture despite being located several times by local police. Inevitable whispers of complicity have accompanied every reported escape.

In the end, the US may have no real option but to refashion its relationship with allies if it is to expect continued cooperation in its anti-terror campaigns.

Musharraf's ignominious exit and, to a lesser extent, Kibaki's troubles at the ballot box could mean that self-interest drives leaders to conclude that the unpopularity resulting from engagement with America is too high a price to pay.

Sign up for the Guardian Today

Our editors' picks for the day's top news and commentary delivered to your inbox each morning.