Keith Olbermann saw the handwriting on the wall with the new owner of NBC and cut a deal for his exit that will give him money and options -- sources tell TMZ.

Here's how it went down. Sources familiar with the situation tell us that Olbermann's agent recently went to NBC complaining that Keith -- who has the most popular show on MSNBC -- was underpaid at $7 million per year. NBC execs told Olbermann's agent they would not cough up any more money.

Network execs were well aware that Comcast wanted Keith gone because he was "a loose cannon that could not be controlled." It became clear to both sides that Olbermann's days were numbered and they began negotiating an exit.

We're told the exit deal wasn't completed until just before airtime Friday night.

Under the deal, sources say Olbermann will get money but cannot appear on television for a certain period time -- we don't know how long.