When should a president say he’s sorry?

President Obama’s apology to his Afghan counterpart, Hamid Karzai, has resurrected the politically vexing issue of national contrition at a delicate moment in both the war in Afghanistan and in the presidential campaign at home.

The president’s “I’m sorry,” for U.S. military involvement in the burning of copies of the Koran, has resonated in Kabul and on the campaign trail, where Republicans have been using it to support their claim that he is more interested in apologizing for American mistakes than in defending American power.

But Obama’s decision to apologize sprang from a mix of principle and pragmatism, the hallmarks of presidential apologies over the years.

The mostly partisan outcry over Obama’s apology shows the challenge he faces as both a candidate for reelection and as a wartime commander in chief, roles whose motivations are sometimes at odds. In this case, his attempt to assuage angry Afghans with an apology he hoped would protect American troops allowed some conservatives to question the strength of his leadership.

Speaking loudest from the Republican field, former House speaker Newt Gingrich called the apology an “outrage,” noting that on the day it was announced, two U.S. soldiers were killed in eastern Afghanistan during rioting over the incident. He said that if Karzai did not apologize for those deaths, then “we should say goodbye and good luck.”

For much of the past year, Republican candidates have mauled Obama’s management of foreign policy, claiming that he has humiliated the country on the world stage. Former Massachusetts governor Mitt Romney has said that Obama “went around the world and apologized for America.” Romney even called his campaign-style book “No Apology” to draw the contrast.

Much of the criticism stems from the more humble tone Obama has sought to bring to American foreign policy after the swaggering approach of his predecessor, George W. Bush. He has banned the harsh techniques that the International Committee of the Red Cross called torture from U.S. interrogation policy and made clear that he believes living up to American values is an essential source of the nation’s power. Repairing U.S. relations with the Islamic world has been one of Obama’s foreign policy priorities.

Polls show that most of the country approves of his handling of foreign affairs, and White House press secretary Jay Carney called the criticism of the Karzai apology a “fallacious and ridiculous narrative” not supported by the facts.

“There’s the risk of opening yourself to political attack, but obviously for a president they have to make that calculation,” said Robert Dallek, a presidential historian, who in defending the apology cited President James Monroe’s declaration that “national honor is the national property of the highest value.”

“When someone is in the presidency, they own that property,” Dallek said. “And it’s incumbent on them to address any national embarrassment. It’s not as if this country is flawless, without blame or sin.”

When and why a president has chosen to say “I’m sorry” has varied over the years, and Obama’s apology to Karzai, a longtime ally in a decade-old war, is among only a few in recent decades that have been delivered in real time.

In 1988, President Ronald Reagan, held up by today’s Republican field as the embodiment of American self-assurance, apologized for the internment of Japanese Americans during World War II.

He signed legislation that eventually disbursed $1.6 billion in reparations to those affected by the policy, which the legislation said was the result of “race prejudice, war hysteria, and a failure of political leadership.”

“The trick is always to apologize for something the country did when you weren’t president,” said Stephen Hess, a presidential scholar at the Brookings Institution. “What is remarkable is apologizing for something that has happened while you were president.”

That same year, for instance, Reagan declined to apologize after missiles fired from the U.S.S. Vincennes downed an Iranian passenger jet, killing all 290 people onboard.

He expressed regret for the loss of life — a common presidential formulation over the years that offers a quasi-apology for something associated with the event, but not for the event itself. But Reagan did not apologize to Iran, a nemesis, showing that who is on the receiving end often determines whether an apology is issued.

Although no one political party has a monopoly on the presidential apology, Republicans, more often than Democrats, have equated a lack of public regret with strength, particularly in conducting foreign policy.

Reagan’s vice president, George H.W. Bush, who was running for president in 1988, said at the time of the U.S.S. Vincennes incident that he would “never apologize for the United States of America. Ever. I don’t care what the facts are.”

Bush was known as a moderate Republican, experienced in foreign policy. But he lived up to his pledge, a track record his son broke from after he took office.

A man of few expressed regrets, George W. Bush nonetheless said sorry several times as president.

In November 2002, he apologized, through the U.S. ambassador in Seoul, several months after a U.S. military vehicle hit and killed two South Korean girls.

According to news reports at the time, he also said he was sorry — privately — to then-Crown Prince Abdullah of Saudi Arabia for published articles saying Bush was unhappy with the kingdom’s help on his declared war on terrorism.

The king of presidential contrition, though, was Bill Clinton, who apologized repeatedly over his two terms in office for national policy, past and present, and his own behavior.

Clinton apologized for historic mistakes including slavery, the Tuskegee syphilis study and U.S. support for a Guatemalan government that carried out human rights atrocities during decades of civil war.

He also apologized for ones that happened on his watch, such as the deaths at an Italian ski resort after a low-flying U.S. warplane severed a gondola cable; his lies about his dalliance with Monica Lewinsky; and his failure to act as the genocide in Rwanda unfolded. He delivered that last one tacitly during a visit to Rwanda nearly four years after the killings.

He, too, was pilloried by conservatives for conducting an “apology tour,” the words former George W. Bush political adviser Karl Rove used to describe Obama’s approach several months after Obama took office.

Obama declined to apologize in November after NATO warplanes killed two dozen Pakistani soldiers near the border with Afghanistan, an incident that a Pentagon investigation concluded was the result of mutual mistrust and miscommunication.

U.S. military responsibility for the burning of the Korans was more clear cut than in the NATO airstrikes, and there were more urgent security factors at stake in Afghanistan. White House officials say Obama’s decision to apologize to Karzai was, in part, an attempt to soothe Afghan feelings and protect American troops.

On Friday, Peter Lavoy, a senior Pentagon official, visited a mosque in Northern Virginia to apologize for the incident as violent protests spread in Afghanistan.

In the past, Obama has invoked his responsibility as commander in chief to protect U.S. forces in explaining controversial decisions.

Early in his administration, for example, he reversed his position by declining to release photos showing U.S. soldiers mistreating prisoners in Iraq and Afghanistan after U.S. commanders said they would inflame public opinion in those countries. The administration had previously agreed to do so to meet the demands of a Bush-era Freedom of Information Act lawsuit.

But the criticism over his apology to Karzai has been confined largely to Republicans running to replace him. Other conservatives have defended Obama’s decision given the importance of maintaining a working relationship with Karzai as U.S. forces begin winding down the war.

“It was an important demonstration of respect for the Afghan people and their religious faith,” said Lisa Curtis, a senior research fellow at the conservative Heritage Foundation who specializes in South and Central Asia. “I think the United States dealt with this appropriately, but I also don’t think any additional apologies are necessary.”

Researcher Alice R. Crites in Washington and correspondent Kevin Sieff in Kabul contributed to this report.