The last American troops will withdraw from Iraq in the next three weeks. President Obama and Iraq's Prime Minister, Nouri al-Maliki, will meet tomorrow in Washington so they can claim that the US emerges from the conflict unweakened and leaves behind an increasingly stable, democratic and prosperous Iraq.

This is misleading spin, carefully orchestrated to allow Mr Obama to move into the presidential election year boasting that he has ended an unpopular war without suffering a defeat. We already had a foretaste of this a couple of weeks ago, when Vice President Joe Biden visited Baghdad to laud US achievements.

Over the years, Iraqis have become used to heavily guarded foreign dignitaries arriving secretly in Baghdad to claim great progress on all fronts before scurrying home again. But even by these lowly standards, Mr Biden's performance sounded comically inept. "It was the usual Biden menu of gaffe, humour and pomposity delivered with unmistakable self-confidence and no particular regard for the facts on the ground," writes the Iraq expert Reidar Visser. Mr Biden even tried to win the hearts of Iraqis by referring to the US achievement in building hospitals in Baku, the capital of Azerbaijan on the Caspian Sea, a city he apparently believes is located somewhere in Iraq.

Republican candidates in the presidential election have been denigrated and discredited by gaffes like this. It is a measure of Mr Biden's reputation for overlong, tedious speeches that the US media did not notice his ignorance of Middle East geography. Dr Visser points out that "when Biden says 'we were able to turn lemons into lemonade', refers to 'a political culture based on free elections and the rule of law', and even highlights 'Iraq's emerging, inclusive political culture ... as the ultimate guarantor of stability', he is simply making things up." Sadly, Iraq is a much divided wreck of a country.

In reality, America's failure to get its way in Iraq and Afghanistan over the past decade, despite deploying large armies and spending trillions of dollars, has been extraordinarily damaging to its status as sole superpower. Whatever Washington thought it wanted when it invaded Iraq in 2003, it was not the establishment of Shia religious parties with links to Iran in power in Baghdad. Similarly, in Afghanistan, a surge in US troop numbers and the expenditure of $100bn a year has not led to the defeat of 25,000 mostly untrained Taliban fighters.

Great powers depend on a reputation for invincibility and are wise not to put this too often to the test. The British Empire never quite recovered in the eyes of the world from the gargantuan effort it had to make to defeat a few tens of thousand Boer farmers.

What makes the US inability to win in Iraq and Afghanistan so damaging is that US policy-making has been progressively militarised. Congress will vote the Pentagon vast sums, while it stints the State Department a few billion dollars. "The Department of Defense is the behemoth among federal agencies," noted the 9/11 Commission Report. "With an annual budget larger than the gross domestic product of Russia, it is an empire."

But it is an empire that has failed to deliver in recent years, though without paying a political price. A senior US diplomat asked me plaintively several years ago: "Whatever happened to popular scepticism about what generals say that we had after Vietnam? People seem to assume they are telling the truth ... they are usually not."

This is equally true of the British Army, though the British military record in Basra and Helmand was even more dismal than that of the Americans. (The system of embedding the media with the Army has played an important role in safeguarding the military from well-earned criticism.)

For all Mr Obama's agonising about sending more troops to Afghanistan in 2009, he never had much choice. Leon Panetta, then CIA chief and now Defense Secretary, was contemptuous about the time spent by the White House debating troop reinforcements. He said the political reality was that "No Democratic president can go against military advice, especially if he asked for it. So just do it." Mr Panetta believed that a decision on the extra 30,000 troops for Afghanistan should have been taken in a week.

The killing of Osama bin Laden and the failure of the military to defeat the Taliban has improved the administration's ability to disengage from Afghanistan. It does not look likely that in a presidential election year, after getting out of Iraq and hoping to do the same in Afghanistan, the US will launch a war against Iran. In the US and Israel there are few votes to be lost in talking tough about Iran, but voters are much less enthusiastic about actually going to war with a stronger opponent than the US ever faced in Iraq or Afghanistan, or Israel in Lebanon.

In the worst economic crisis since the 1930s, the rest of the world is not going to thank the US or Israel for starting a conflict that would close the Strait of Hormuz and send up the price of oil. It would also be difficult to de-escalate such a confrontation because it serves domestic electoral purposes in Washington, Tel Aviv and Tehran alike. Americans, Israelis and Iranians all define their self-image in terms of opposition to demonic enemies. Any compromise is vulnerable to being sabotaged by domestic political rivals as a deal with the devil.

Overall, US influence is ebbing in the Middle East. For all Mr Biden's talking up, the Iraq war was a disaster for the US. Similarly in Afghanistan, massive military force has produced meagre political dividends. Washington may rejoice that Muammar Gaddafi is gone and Bashar al-Assad may follow him. But the US has lost or is losing its paramount position in Turkey and Egypt as the military establishments of these countries lose control.

The political crisis provoked by the Arab Awakening across the Middle East is not dying away. If anything it is deepening as struggles for power intensify in Egypt and Syria. The outcome of the Libyan civil war may encourage limited foreign intervention, but the ongoing economic crisis makes it riskier for the US or European powers to become involved in wars they cannot see the end of.