Retreating armies make easy targets, as the British learned to their cost when falling back from Kabul in 1842. Barack Obama, unlikely heir to bumbling General "Elphy Bey" Elphinstone, now faces two not dissimilar challenges: how to get US forces out of Afghanistan and Iraq with the minimum of bloodshed and the maximum of dispatch. Avoiding descent into post-withdrawal chaos would be an added bonus.

Easy answers are not available. In Afghanistan, several factors suggest Obama may speed up the size and pace of troop reductions due to begin next month. The mind-boggling cost of the war, estimated at $2bn a week or $110bn a year, is increasingly insupportable as his 2012 re-election bid looms. Americans, like Britons, are also sickened by the continuing human cost.

The elimination of Osama bin Laden, who launched the 9/11 attacks from Afghanistan, has created a palpable sense of "job done" that Obama is finding hard to resist. "By killing Bin Laden, by blunting the momentum of the Taliban, we have now accomplished a lot of what we set out to accomplish 10 years ago," he declared this week. Washington's developing narrative is that last year's surge of 30,000 extra troops into the south has worked, even though this is not wholly true.

The US currently has about 100,000 troops in Afghanistan. Influential people such as Senator Carl Levin are demanding a minimum drawdown of 15,000 by year's end. Some Democrats want them all home well before the target date of 2014. Obama says he will give his decision in a speech later this month.

But behind closed doors in the White House and the Pentagon, a rearguard battle royal is raging. General David Petraeus, the Afghan commander tapped as the next CIA director, plainly believes withdrawing more than a few thousand troops this year would jeopardise his much vaunted counter-insurgency strategy. For his many fans, Petraeus, hero of Iraq, is little short of a saint. Cross him at your peril.

Robert Gates, the outgoing but still powerful defence secretary, sits in Petraeus's pew. "I would try to maximise my combat capability as long as this process [the war with the Taliban] goes on – I think that's a no-brainer," he said bluntly in Afghanistan last week. Against him are vice-president Joe Biden, who opposed the surge in the first place, the new national security adviser, Thomas Donilon, and Leon Panetta, Gates's successor.

Obama must factor in numerous other considerations. One is the ability of the Afghan army and police to take over from the Americans and Nato. General William Caldwell, who is in charge of training, pointed this week to continuing, significant problems over attrition rates, desertion, illiteracy and corruption. Another issue is the questionable reliability and longevity of President Hamid Karzai's government. Obama must also weigh the uncertain prospects for a viable peace process involving Taliban elements, and how to maintain post-withdrawal pressure on militants in Pakistan.

A Senate report this week warned of economic meltdown in Afghanistan if, as expected, US financial aid used for short-term military "stabilisation" operations – about 80% of Washington's $320m monthly aid total – effectively disappears when the troops leave. According to a separate study, billions of dollars' worth of US-funded reconstruction projects could grind to a halt after a withdrawal, simply because there are no plans for their continuance.

Iraq, where all 46,000 US troops are due to leave by 31 December, presents similar conundrums. Commanders are openly expressing concerns that withdrawal routes via Kuwait are vulnerable to attack from newly emboldened Shia and al-Qaida-style militants. As the pull-out nears, violence is on the up. This week, five US soldiers were killed in the deadliest such attack in two years. There is no agreement, meanwhile, about how to pay for, house, and protect the 17,000 American diplomats and civilians left behind.

Like Karzai in Kabul, Nouri al-Maliki's administration in Baghdad is weak, fractious and unloved. It faces a reviving challenge from Iran via Muqtada al-Sadr and his Shia Mahdi army. Key ministries such as defence and interior remained leaderless. And Maliki is far from immune from the Arab spring; renewed anti-government protests over jobs, electricity and corruption are expected this Friday. Little wonder, then, that some Kurdish and Sunni leaders would prefer the Americans to delay their departure, possibly indefinitely. The US says it is willing to "entertain" this idea.

And there's the rub. In Iraq, as in Afghanistan (where despite Karzai's hostile rhetoric, a long-term "strategic partnership" agreement is being negotiated), the prospect of a continuing presence of quasi-permanent US military bases in both countries, lasting far beyond the nominal exit dates, is a very real one. Behind all the talk of withdrawals lies this dirty little secret: the Yanks aren't going home.