In the twilight of Treasury Secretary Timothy F. Geithner’s tenure in public service, he is closing the book on the company that has probably caused him more angst than anything else in his career.

Treasury announced on Monday that it is completing its exit of American International Group, the insurance behemoth that nearly imploded four years ago, almost dragging down the entire financial system with it.

It plans to sell about 234 million shares, raising about $8 billion and leaving Treasury with a $5 billion profit on its investment. The Federal Reserve, which also invested in the firm, has already unloaded virtually all its holdings, for a profit of $18 billion.

The news comes as Geithner plans to step down next month, after five years fighting financial fires. His final task is as the senior negotiator for the White House in its talks with Congress over striking a deal to avert the fiscal cliff.

The three-letter AIG was the source of many four-letter words in Geithner’s office, first at the Federal Reserve Bank of New York, where he was president when the firm nearly collapsed, and then at Treasury, where he faced a huge controversy related to the company.

In September 2008, Geithner was part of the three-person team — which also included Treasury Secretary Henry M. Paulson Jr. and Fed Chairman Ben S. Bernanke — who led the bailout of AIG just a little more than a week after Lehman Brothers went bankrupt.

The initial bailout proved insufficient as the financial crisis worsened and losses grew at the firm, which had made wildly speculative and disastrous bets. In total, the Fed and Treasury together committed $182 billion worth of aid to AIG.

But the gargantuan size of the bailout was only one problem for Geithner when he joined President Obama’s Cabinet. The Treasury’s watchdog criticized the bailout of AIG for overly being generous.

And outraged exploded when word spread that AIG was planning to pay more than $100 million in bonuses to employees — including at the division that had made the bad bets that led to the firm’s undoing.

Some lawmakers took aim at Geithner, who still stood atop the New York Fed when officials concluded that AIG had contractually agreed to give the bonuses and that not awarding them could make things worse by driving key employees to leave.

At the time, people close to Geithner said he was not intimately familiar with the details of the compensation agreements. Still, Obama was stuck in the difficult spot of both expressing anger at the bonus payments and affirming that contracts were inviolable.

In the end, amid intense public pressure, AIG agreed to reduce the bonuses.

A year into his tenure at Treasury, Geithner’s fortunes shifted. AIG looked as if it was going to stabilize.

In the summer, Jim Millstein, an investment banker assigned to oversee Treasury’s rescue of AIG, walked into his office with good news: Taxpayers were going to get their investment in AIG back, and maybe a profit.

“Get the [expletive] out,” Geithner told Millstein. “I haven’t had any good news on AIG.”