Philadelphia District Attorney Seth Williams abruptly resigned his office and agreed to plead guilty Thursday in a surprise move that ended a federal bribery trial that had dragged messy details about his personal life into the open, and had cast a deepening shadow over the final days of his once-promising political career.

But minutes after accepting Williams' decision, the judge presiding over the case made a startling announcement of his own — ordering the city's top prosecutor handcuffed and immediately hauled off to prison.

"I have a guilty plea from the highest law enforcement officer in the city," U.S. District Judge Paul S. Diamond said. "He betrayed his office and he sold his office. I am appalled by the evidence that I have heard."

The news hit the courtroom — and the city — like a bomb.

As U.S. marshals surrounded Williams, his ex-wife, Sonita — one of the few family members in the room — broke down in tears. Jurors, abruptly dismissed after two weeks of damaging testimony, were flummoxed, some saying they were disappointed they wouldn't have a chance to render a verdict.

And downstairs, the district attorney's driver stood beside Williams' idling car, waiting for a man he hadn't yet learned wouldn't be leaving the building.

Under order from Diamond, the district attorney's resignation letter — hastily scrawled on a piece of paper moments before the hearing — was couriered to Mayor Kenney's office, as the city's Board of Judges scrambled to schedule a special meeting to name an interim replacement, something it hasn't had to do in nearly three decades.

"I'm just very sorry for all of this, your honor," a choked-up Williams said.

>> READ MORE: What's next for the Philly District Attorney's Office?

Those last public words, uttered moments before he was led away, effectively ended Williams' career in office — which had begun in 2010 amid widespread accolades for sweeping changes he promised to implement within the city's justice system, only to be overshadowed more recently by a series of scandals involving his money, his dating life, and his personnel decisions.

Even as he faced indictment on 29 counts of bribery, fraud, and honest services fraud, Williams had clung to his position and its $176,000 annual salary, vowing he would be vindicated at trial.

It wasn't clear Thursday what exactly prompted Williams to change his mind.

In fact, he had turned down offers from prosecutors both prior to his indictment earlier this year and again days before his trial began.

His lawyer, Thomas F. Burke, said that Williams had struggled with the decision over several days and was up late into the night Wednesday, wrestling with what he would do.

The ultimate deal with prosecutors was struck just after 1:30 a.m. Thursday, the defense lawyer said. Kathleen Martin, Williams' first assistant, said she was notified via a text message from her boss early Thursday.

"I think this was a very hard decision for a proud man," said Burke. "What the district attorney wanted most was closure for his family, for himself, and for the city. He's been humbled by the experience."

During nearly two weeks of testimony, Assistant U.S. Attorneys Robert A. Zauzmer,  Eric Moran, and Vineet Gauri had painted Williams as a shameless moocher who repeatedly sought others' money to support a lifestyle he couldn't afford.

Two wealthy businessmen testified that they showered Williams with gifts of cash, luxury goods, and all-expenses-paid travel to an upscale Dominican Republic resort and other vacation spots, hoping that he would repay their generosity by using his office to remove various legal hurdles they faced.

When that largesse wasn't enough to support Williams' high-end tastes, government witnesses said, the district attorney raided his own campaign accounts and money set aside for his aging mother's nursing home care.

One juror, Julie Dedic, 59, of Bethlehem, scoffed at the district attorney's assertion that campaign money he'd spent on deep-tissue massages and facials had helped him become more electable. "You've got to be kidding me," she said Thursday.

Meanwhile, years of text message exchanges, including one in which Williams described himself to a benefactor as "merely a humble beggar,"  piled on humiliation each time they were shown.

Yet, as the gobsmacked Williams pleaded guilty to one count of violating the Travel Act — a crime that could keep him in prison for up to five years — it soon became clear that the man brought low by his own costly pursuit of the high life had lower still to fall.

Williams beseeched Diamond to let him remain free until his Oct. 24 sentencing date, citing his three school-age daughters and his inability to flee even if he wanted to.

"He has no means to go anywhere," Burke told the court. "He has no support. He's deeply in debt and he doesn't even have a car."

Williams' eyes welled up with tears as he took the witness stand to plead his own case. Asked how much money he currently had in his bank account, he responded: "I'm always afraid to look because it's so small — probably about $150 to $200."

He said he had no idea how he would have supported himself had he been allowed to remain free. "Help [from] friends?" he guessed. "I haven't really thought beyond that."

But Williams' already dim financial prospects are only likely to grow more dire in the coming months.

What Seth Williams May Be Forced to Pay

Seth Williams on Thursday told U.S. District Judge Paul S. Diamond that he has $150 to $200 in the bank and no retirement savings beyond his city pension. But City Controller Alan Butkovitz has taken action to revoke WIlliams’ pension, and the fines and fees that the former district attorney may be forced to pay are considerable.

Under the terms of his deal, Williams agreed to forfeit nearly $65,000. He could be fined up to $250,000 more and likely will face additional restitution orders from the court.

And within hours of his plea, City Controller Alan Butkovitz moved to revoke his city pension and freeze the additional $117,859 he had paid into it — a fund Williams had said represented his only retirement savings.

Williams also remains indebted to the city's Ethics Board after agreeing in January to pay a $62,000 fine for his belated disclosure of more than $175,000 in gifts he received over five years — the largest penalty assessed on a single candidate in the board's 11-year history.

His plea deal all but ensures that his law license will be permanently revoked and that he'll face a prison term at the high end of the five-year sentencing range.

Although prosecutors agreed to dismiss all the charges they brought against him except the one count to which Williams admitted Thursday, the agreement also required him to confess to the other alleged crimes so that his admissions could be used against him at sentencing.

Sources close to Williams' defense said he had turned down an earlier offer just hours before his indictment in March that would have required a guilty plea to the same count but would have prevented his other crimes from reaching the sentencing judge's desk, substantially reducing the prison time he now faces.

Federal authorities declined Thursday to discuss any prior offers they might have made to Williams.

Instead, U.S. Attorney William Fitzpatrick, whose New Jersey office oversaw the case, focused on the future, saying:

"Hopefully, today's conviction will allow the people of Philadelphia and the dedicated employees of the District Attorney's Office to close this unfortunate chapter in the city's history."

Staff writers Chris Brennan, Joseph A. Slobodzian, Alfred Lubrano,, and Julie Shaw contributed to this article.

Read a recap of Philadelphia District Attorney Seth Williams' trial with our day-by-day updates and learn more with our explainer on everything you need to know about the case.