Image copyright Reuters Image caption The US has had troops in Afghanistan since 2001

President Barack Obama has warned his Afghan counterpart Hamid Karzai that the US may pull all of its troops out of his country by the year's end.

Mr Obama conveyed the message in a phone call to Mr Karzai, who has refused to sign a security agreement.

The US insists this agreement must be in place before it commits to leaving some troops behind for counter-insurgent operations and training.

The US has had troops in Afghanistan since 2001 when it toppled the Taliban.

Analysis America and its Nato allies are increasingly frustrated at President Karzai's unwillingness to sign a security agreement. He says there has to be a peace process with the Taliban first. If he or his successor does not sign, then Washington will pull all US troops out in 2014. This should not be seen as an empty threat. But it probably is a negotiating ploy designed to put pressure on Mr Karzai or whoever succeeds him. The White House says the longer it takes to sign an agreement, the smaller and less ambitious any post-2014 mission would be. The prospect of a complete withdrawal would risk leaving a power-vacuum that would be filled by insurgent groups. It would weaken Kabul's already tenuous grip on power and even risk another civil war. The prospect of a bilateral security agreement is by no means dead but it is starting to look in danger.

Its forces went into the country following the 9/11 attacks on the US. With Afghan and Western allies, they quickly overthrew the Taliban authorities, but have faced insurgent attacks since then.

Correspondents say the disagreement over the bilateral security agreement (BSA) is the latest step in the long and deteriorating relationship between Washington and Mr Karzai, who was once seen as a key US ally.

The BSA, which offers legal protection for US troops and defines a post-2014 Nato training and anti-insurgent mission, was agreed by the two countries last year after months of negotiation.

It was endorsed at a national gathering (Loya Jirga) of Afghan elders in Kabul in November.

'Contingency planning'

But Mr Karzai has refused to sign the deal until a peace process is under way with the Taliban, adding that if he were to sign it, he would become responsible if Afghans were killed by US bombs.

"President Obama told President Karzai that because he has demonstrated that it is unlikely that he will sign the BSA (Bilateral Security Agreement), the United States is moving forward with additional contingency planning," the White House said in a statement.

"Specifically, President Obama has asked the Pentagon to ensure that it has adequate plans in place to accomplish an orderly withdrawal by the end of the year should the United States not keep any troops in Afghanistan after 2014.

"Furthermore, the longer we go without a BSA, the more likely it will be that any post-2014 US mission will be smaller in scale and ambition."

Unsigned security deal - main points Jurisdiction: US forces remaining after 2014 reportedly to receive immunity from Afghan courts

US forces remaining after 2014 reportedly to receive immunity from Afghan courts Sovereignty: In October 2013, President Karzai appeared to have secured US agreement not to carry out attacks on Afghan soil without first consulting the Afghan authorities

In October 2013, President Karzai appeared to have secured US agreement not to carry out attacks on Afghan soil without first consulting the Afghan authorities Security: The US said in October 2013 that it would not protect Afghanistan from external attack because it could get mired in a war with Pakistan Foreign forces in Afghanistan

While Mr Karzai has refused to sign the BSA, some candidates in April's Afghan presidential elections have indicated they would.

Mr Karzai, who has served two terms as Afghanistan's first and only president since the Taliban were ousted from power in 2001, is obliged by law to stand down after the next election.

Analysts say the US statement clearly implies that Mr Karzai's stance will harm his country's security long after he leaves office.

The White House statement came as US Defence Secretary Chuck Hagel flew to Brussels for a Nato meeting at which Afghanistan is due to be discussed.