The Defense Department recently announced troop withdrawals by Jan. 15 that will reduce American forces in Iraq and Afghanistan to 2,500 each from their one-time highs of some 170,000 and 100,000 troops, respectively. This drawdown makes explicit what those of us who served in the military have long realized: We lost.

War is evil even when it is necessary but our inability to win has stolen even the possibility that the ends might justify the means. For the roughly 3 million service members whose boots touched soil in Iraq and Afghanistan over the past 19 years, our defeat is a uniquely personal loss.

When I was sent to Iraq in 2009 it was to safeguard our withdrawal. During our entire deployment in the once treacherous Sunni triangle we discovered and disposed of a single roadside bomb on the main highway outside Falluja, where they had once been as common as potholes. I returned home wishing I could have done more but was glad to see how much progress had been made by the regiments who'd fought so hard before me.

When I read a few years later that the Islamic State had overrun that same area I began to sense that our efforts had been in vain. But it was my Afghanistan deployment in 2010-11 that cemented their futility for me.

My company defended a labyrinthine cluster of mud-walled villages set amid fields of poppy and corn in the Musa Qala District of Helmand Province. As the northern tip of the Marine campaign in Helmand we held a line alongside battalion after battalion of Marines that extended south through the river valley to the district center, where the bazaar and the governor were, and then down past Sangin to the provincial capital, Lashkar Gah, and further to Marja and Garmsir.

People often ask me what Afghanistan was like, but I can never really answer: Each district might as well have been its own war for the Marines who fought, with victories and defeats known only to them.

I often think back on the moments in my deployments when the crack of a gunshot or the deep thud of a large roadside bomb suddenly infused my life at war with a clear and tangible purpose. I remember the kids lining up the first day after the school reopened, the first time the partners we trained in the Afghan Army took the initiative to patrol without our assistance, and the rare smile on a villager's face after we'd provided the first aid that had saved the life of his father, who had been shot in crossfire.

I try to remember those small decencies instead of the casualties and the killing but they do little to assuage the overwhelming senselessness of the greater war.

Shortly after I returned from Afghanistan in 2011, President Barack Obama announced that Osama bin Laden had been killed during a raid on his compound in Pakistan, where he was living after fleeing Afghanistan years before. As I watched people celebrating outside the White House and outside ground zero I hoped that the war was finally over, but even then it didn't feel like victory.

The conflict had grown so much bigger since the attacks of Sept. 11, 2001, that bin Laden's death felt like a footnote. The execution of a single dethroned sheikh suddenly paled in significance to my own recent experience at war. Later that night I tried to recall the circumstances surrounding the death of each man we'd killed and count how many there had been but there were too many to remember.

The Afghanistan war was finally lost for me in August 2015, several years after my own deployment ended, when the Taliban recaptured Musa Qala, which five men in my company had died defending. After the Taliban's seizure, allied airstrikes bombed the same government center we'd sacrificed so much to hold.

A member of Parliament from Helmand Province later described that building as "completely vanished from the earth." Along with it was buried any hope there might have been that the sacrifices I, and so many others, have made in service to our country would not be in vain.

The cost of these wars has been astronomical: Roughly $6 trillion in government spending, with the Defense Department spending alone costing each American taxpayer an estimated more than $7,000. Additionally, today's young veterans face a legacy of psychological and physical injury, as well as illness from our war's Agent Orange: the toxic burn pits whose smoke we inhaled.

Even more costly are the approximately 515,000 people killed in Iraq, Afghanistan and Pakistan, including more than 260,000 civilians. And for what? Iraq remains a tenuous democracy teeming with militias while Afghanistan is locked in a conflict with a resurgent Taliban, and peace talks are in deadlock.

Both countries fail to meet the objectives of freedom and democracy set when President George W. Bush started those wars. They fall short of President Obama's goals when he sent me and 30,000 other troops to Afghanistan and of the claims he made when declaring an end to combat operation in Iraq only to see the Islamic State undo those gains. President Donald Trump does not seem to even have a purpose for those 5,000 troops who will remain in Afghanistan and Iraq.

Like many service members, I wrote a letter in case I was killed during my deployment. It began with an assurance to the friends and family I would have left behind: "It was worth it." I believed then that we had a moral obligation to not only protect my fellow Americans but to leave the Afghan and Iraqi people with a chance to live in peace.

That obligation remains even though it cannot be fulfilled. Instead I am resigned that these wars will finally enter the history books not only as defeats but as stains on our national honor.

The political theorist and philosopher Michael Walzer writes in "Just and Unjust Wars" that "it still seems important to say of those who die in war that they did not die in vain. And when we can't say that, or think we can't, we mix our mourning with anger." I would add that we also mix it with shame.

I recognize that shame is not a very American trait but with it comes humility. Sadly, my generation had to relearn the lessons of Vietnam in Iraq and Afghanistan. But in coming to grips with our defeat, we have a chance to ensure that we do not sacrifice future generations to such folly.

And by so doing we may yet salvage some purpose from this tragedy: to do everything in our power to avoid more wars, and to ensure that if and when the next war does come, it is worth it.

Timothy Kudo, a former Marine captain who served in Iraq and Afghanistan, is working on a novel about the Afghanistan war. He wrote this article for the New York Times.