Not all the derivative contracts American International Group Inc wrote on complex securities backed by now-toxic mortgages have ended up being a disaster.

In fact, dozens of interest rate swaps that AIG put together -- for many of the very same troubled collateralized debt obligations that drove the giant insurer to the brink of bankruptcy -- are generating north of $1 billion in value for the firm, according to people familiar with the deals.

But AIG has been at risk of losing that value ever since the fall of 2008, when the U.S. government stepped in with a massive bailout and the insurer's once mighty credit rating was slashed.

That's because the CDOs were written in a way that enabled the managers of those mortgage-related securities to cancel the interest rate swaps with AIG if the insurer's credit rating fell below a stipulated level.

However, after many months of negotiations with the CDO managers, the rating agencies and one big bank, AIG has come up with a solution that will enable it to continue getting the monetary benefit from those interest rate swaps, said people familiar with the situation.

AIG has struck a deal with Barclays in which the British-based bank will stand in as an intermediary for AIG in more than three dozen of these derivative transactions, said people familiar with the situation.

FLASHBACK

For an undisclosed fee, Barclays is essentially renting out its stronger credit rating to AIG to enable the insurer to remain a participant in these currently lucrative interest rate swaps.

Barclays declined to comment on its arrangement with AIG, which is known as a novation in the world of derivatives.

The solution crafted by AIG looks a little bit like a flashback to the halcyon days of the credit market, when it was common for big financial institutions to let weaker firms essentially piggyback on their credit ratings in order to get access to better deals.

An executive with AIG Financial Products, the much-maligned AIG division that engineered the interest rate swaps as well as the controversial CDO guarantees, said the arrangement is in the best interest of stockholders and U.S. taxpayers.

This is another example of how the employees of FP have used their experience and understanding of these complex securities to preserve value, said Gerald Pasciucco, head of AIG Financial Products.

The move to get Barclays to stand in for AIG in these derivatives deals is part of the fallout from AIG's near-collapse in September 2008. The interest rate derivatives also are reminder of the many moving pieces inside a CDO, the troubled subprime mortgage-related securities at the center of the global financial crisis.

Meanwhile, Pasciucco's division continues to draw heat for its role in selling tens of billions of dollars of credit default swaps, an insurance-like derivative, to 16 big banks that owned a bunch of troubled CDOs. On Wednesday, Treasury Secretary Timothy Geithner blasted AIG for paying a new round of bonuses to employees of AIG Financial Products.

In fall 2008, the U.S. government cobbled together a $180 billion bailout package for AIG to stave off a potentially crippling bankruptcy filing. The most controversial part of the bailout remains the decision by the New York Federal Reserve Bank to create an entity to buy up the CDOs that AIG insured against default.

The irony is that many of those same troubled CDOs that AIG had written credit default swaps on also had embedded within them the AIG-engineered interest rate swap that the insurer is trying to preserve.

The interest rate swaps were designed to minimize the risk to the CDO of investing in underlying mortgage-related securities that are pegged to different interest rates. The contracts required the CDO and AIG to make payments to each other depending on the direction of a series of fixed-rate interest rates and Libor.

Right now, with Libor rates near historic lows, the terms of the swaps favor AIG and are said to be, in the parlance of Wall Street, in the money.

In all, AIG has transferred nearly three dozen of these interest rate derivatives to Barclays. The negotiations between Barclays and AIG to transfer the swap contracts have been going on since early 2009.

The insurer has reached a similar agreement with a division of Bank of America Corp on two interest rate swaps.

The deals have been slowly but surely announced over the past few months in press releases from the major credit rating agencies.

It is not known how many other similar deals AIG is working on with banks other than Barclays.

(Reporting by Matthew Goldstein; editing by John Wallace)