President Obama outlined his plan to end America’s longest foreign war during a visit here Tuesday colored by election-year politics and economic uncertainty, declaring that “this time of war began in Afghanistan, and this is where it will end.”

“We have traveled through more than a decade under the dark cloud of war,” the president said at a U.S. military base. “In the pre-dawn darkness of Afghanistan, we can see the light of a new day on the horizon.”

Obama delivered his address at the end of an unannounced visit here to sign a long-term partnership agreement with the Afghan government and to mark, alongside American troops at Bagram air base outside this capital city, the first anniversary of the raid that killed Osama bin Laden.

Less than two hours after Obama left the country, insurgents killed at least seven people in a suicide bombing outside a heavily guarded housing compound for foreigners, news services reported. The Taliban claimed responsibility for the attack, which led to a prolonged gun battle between insurgents and Afghan soldiers who rushed to the scene, the Associated Press reported.

Obama’s trip came amid criticism at home that the president is using the anniversary of bin Laden’s death to advance his reelection prospects — featuring his decision to launch the mission in campaign videos and other political settings, for example. As Republican critics have called his leadership abroad weak, Obama has held up the bin Laden operation as evidence that he is willing to make risky decisions to protect U.S. interests.

His arrival here was timed to make the “strategic partnership agreement” official before an important NATO summit this month — and, in the words of one senior administration official traveling with Obama, to take advantage of “a resonant day for both our countries on the anniversary of the death of bin Laden.”

Obama used his time with the troops to emphasize the sacrifices they and their families have made over more than a decade of conflict, saying that in doing so they made the bin Laden mission successful and put the long war on a path to its conclusion.

The hours-long visit was directed almost entirely toward an American audience, unfolding while most Afghans slept. It also served to promote detente after some of the tensest months in U.S.-Afghan relations.

Since February, American service members have inadvertently burned Korans at a U.S. military base, Army Staff Sgt. Robert Bales allegedly murdered 17 civilians in Kandahar province, and at least 18 NATO troops have been killed by their Afghan counterparts. In addition to straining ties and infuriating Afghans, the incidents have contributed to rising war fatigue at home.

Opinion polls show most Americans no longer believe the war is worth fighting. But the strategic agreement and the troop withdrawal schedule allow Obama to say that he has ended the war in Iraq and is winding down the one in Afghanistan, a position even a majority of Republicans favor.

“The Iraq war is over. The number of our troops in harm’s way has been cut in half, and more will be coming home soon,” Obama said Tuesday. “We have a clear path to fulfill our mission in Afghanistan while delivering justice to al-Qaeda.”

Obama campaigned in 2008 on a pledge to end the Iraq war, which he did in December, and to strengthen the U.S. effort in Afghanistan at a time when the Taliban appeared resurgent and al-Qaeda was active in the regions along the Pakistani border.

With opposition to the Afghanistan war building within his party, Obama announced the beginning of the end of the U.S. mission last year by adopting a withdrawal timeline more rapid than some of his commanders recommended.

The decision drew criticism from some of his GOP rivals, including the presumptive presidential nominee, Mitt Romney, that Obama was calibrating his war strategy to the election calendar. Romney, who on Tuesday gave Obama a share of the credit for bin Laden’s killing, has said the U.S. goal should be to defeat the Taliban on the battlefield.

But Obama on Tuesday laid out a different ambition.

“Our goal is not to build a country in America’s image or to eradicate every vestige of the Taliban,” he said. “These objectives would require many more years, many more dollars and many more American lives. Our goal is to destroy al-Qaeda, and we are on a path to do exactly that.”

The last of the 33,000 troops Obama dispatched to Afghanistan in 2009 will head home at the end of September. Senior administration officials said Tuesday that, though no specific future troop levels have been determined, a “steady reduction” will follow over the next two years.

Obama’s timeline calls on Afghan security forces to take the lead in combat operations by the end of next year. All U.S. troops are scheduled to leave by the end of 2014, except for trainers who will assist Afghan forces and a small contingent of troops with a specific mission to combat al-Qaeda through counterterrorism operations.

In his remarks, Obama emphasized that the United States will not seek permanent military bases in Afghanistan, a country that for centuries has fiercely opposed foreign interlopers.

Those U.S. trainers and Special Operations troops that remain beyond 2014 will live on Afghan bases. Senior administration officials said the agreement is meant to send a signal to the Taliban that they cannot “wait out” the international presence, which is supporting a fragile Afghan government.

“The goal I set to defeat al-Qaeda and deny it a chance to rebuild is now within reach,” Obama said.

Traveling overnight and landing in darkness, Obama arrived at Bagram air base, 35 miles north of Kabul, at 10:20 p.m. local time and boarded a helicopter for a flight into the capital. He arrived at the presidential palace just after 11 p.m. for a meeting with President Hamid Karzai, who has had a contentious relationship with Obama over the years.

“I’m here to affirm the bond between our two countries and to thank Americans and Afghans who have sacrificed so much over these last 10 years,” Obama said. “Neither Americans nor the Afghan people asked for this war, yet for a decade we’ve stood together.”

In signing the agreement after 20 months of difficult negotiations, Obama said that “the Afghan people and the world should know that Afghanistan has a partner in the United States.”

Karzai has long requested reassurance from Obama that U.S. support would not wane after 2014. The agreement commits Obama to ask Congress for money to support Afghanistan through 2024, but it does not specify the amount of annual aid.

The accord is designed to promote the training of Afghan forces, a reconciliation and reintegration process for Taliban fighters who leave the battlefield, and regional stability with a focus on improving relations with Pakistan. A second senior administration official, who also spoke on the condition of anonymity, called it “a crucial component to bring the war to an end responsibly.”

In speaking with troops after the signing ceremony, Obama sounded notes of praise and hope.

“I know the battle is not yet over; some of your buddies are going to get injured, some of your buddies may get killed. And there’s going to be heartbreak and pain ahead,” he said. “But there is a light on the horizon because of the sacrifices you made.”

Karzai has had a tempestuous relationship with American leaders in recent years, making demands that U.S. officials have seen as unrealistic and maligning Washington as trying to strong-arm reconciliation efforts with the Taliban.

At the heart of Karzai’s discontent were two issues that appeared to have the potential to obstruct a long-term partnership: night operations and a U.S. military prison at Bagram.

This year, at Karzai’s behest, the United States agreed to cede control of the night raids and the detention center to Afghan security forces — concessions that paved the way for the long-term partnership agreement.

But beyond the substantive reforms that Karzai has demanded, Afghan officials say their president has also longed for more access to Washington — a wish that Obama’s rare visit to Kabul may have sought to satisfy.

Administration officials said Obama wanted to sign the deal in Kabul to highlight Afghan sovereignty and the changing nature of the U.S.-Afghan relationship.

“Today, with the signing of the strategic partnership agreement, we look forward to a future of peace,” he said after signing the pact.

Americans have not outlined what the U.S. troop presence will look like beyond 2014, and NATO has yet to specify its long-term financial commitment to the Afghan security forces. That topic will be a focal point of the NATO summit in Chicago this month.

U.S. military officials say they have been impressed with the improvement of the Afghan forces — an assessment echoed Tuesday by administration officials traveling with Obama.

But the Taliban remains strong in the south and the east, penetrating key security barriers in Kabul and Kandahar — the country’s most important cities — within the past month. In a coordinated assault on April 15, more than 35 militants staged simultaneous attacks on high-profile targets in several cities across eastern Afghanistan, including the capital.

“Let us finish the work at hand,” Obama said Tuesday, “and forge a just and lasting peace.”

Wilson reported from Washington. Staff writers Karen DeYoung and Ed O’Keefe in Washington contributed to this report.