Mr. Gates discloses that he almost quit in September 2009 after a dispute-filled meeting to assess the way ahead in Afghanistan, including the number of troops that were needed. “I was deeply uneasy with the Obama White House’s lack of appreciation — from the top down — of the uncertainties and unpredictability of war,” he recalls. “I came closer to resigning that day than at any other time in my tenure.”

Caitlin Hayden, the National Security Council spokeswoman, released a statement late Tuesday saying that “deliberations over our policy on Afghanistan have been widely reported on over the years, and it is well known that the president has been committed to achieving the mission of disrupting, dismantling and defeating Al Qaeda, while also ensuring that we have a clear plan for winding down the war, which will end this year.”

In response to Mr. Gates’s comments on Mr. Biden, she said, “President Obama relies on his good counsel every day.”

Mr. Gates is a bipartisan critic of the two presidents he served as defense secretary. He holds the George W. Bush administration responsible for misguided policy that squandered the early victories in Afghanistan and Iraq, although he credits Mr. Bush with ordering a troop surge in Iraq that averted collapse of the mission.

And he says that only he and Mr. Bush’s second secretary of state, Condoleezza Rice, pressed forcefully to close the detention center at Guantánamo Bay, Cuba, with little result.

Mr. Gates does not spare himself from criticism. He describes how he came to feel “an overwhelming sense of personal responsibility” for the troops he ordered into combat, which left him misty-eyed when discussing their sacrifices — and perhaps clouded his judgment when coldhearted national security interests were at stake.

Mr. Gates acknowledges that he initially opposed sending Special Operations forces to attack a housing compound in Pakistan where Osama bin Laden was believed to be hiding. Mr. Gates writes that Mr. Obama’s approval for the Navy SEAL mission, despite strong doubts that Bin Laden was even there, was “one of the most courageous decisions I had ever witnessed in the White House.”