WASHINGTON — As he described the factors that went into his decision to keep American troops in Afghanistan, the one word President Obama did not mention on Thursday was Iraq.

Four years ago, he stuck to his plan to pull out of Iraq, only to watch the country collapse back into sectarian strife and a renewed war with Islamic extremists. Facing a similar situation in Afghanistan, Mr. Obama has decided not to follow a similar course.

Whether keeping a residual American force in Iraq would have made a difference is a point of contention, but the president chose not to take a chance this time. In seeking to avoid a repeat of the Iraq meltdown by keeping 9,800 troops in Afghanistan next year and 5,500 after he leaves office, he abandoned his hopes of ending the two wars he inherited.

While not openly drawing lessons from the Iraq withdrawal, Mr. Obama drew an implicit distinction by emphasizing that the new Afghan government of President Ashraf Ghani, unlike the Baghdad government in 2011, still supported an American military presence and has taken the legal steps to make it possible.