Washington (CNN) As Rep. Nancy Pelosi took the stage in a noisy hotel ballroom in Washington on election night she was greeted with chants of "Speaker! Speaker! Speaker!"

Her party had just won back the House of Representatives, and the longtime Democratic leader and first woman in history to hold the speaker's gavel was poised to do it again.

But many of the Democrats who made that victory possible had also made a big promise: They wouldn't support Pelosi for speaker.

Those dynamics set the stage for an internal party dispute that would soon play out in the halls of Congress, as Pelosi flexed the muscle she'd built over years as a relentless negotiator to once again keep her caucus in line.

Less than 24 hours after she gave her victory speech on election night, roughly a dozen incumbent Democrats joined a conference call to game out ways to bring change to party leadership for the first time in more than a decade.

They would need a relatively small number of Democrats to prevent Pelosi from getting 218 votes come January, when the full House holds its speaker election. Given the small group of incumbents lining up to oppose her -- plus the incoming freshmen already in the anti-Pelosi column -- the group of detractors felt good about their numbers.

"I am 100% confident we can forge new leadership," Rep. Filemon Vela of Texas told CNN a week after the election.

Publicly, Pelosi was unfazed by the opposition. She went on a media barnstorm around Election Day, repeating in interview after interview that she felt confident she would be speaker again, despite the math problem that lay before her.

"This is not a day at the beach, this is politics," Pelosi said in an interview with CNN's Chris Cuomo. In nearly the same breath, she, too, asserted she was "100%" confident she would win.

The next month would be a battle over who was right: the powerful, veteran lawmaker from California known for never backing down, or the small group of Democrats desperate for change at the top. They were ready to duke it out over the long haul, even if that meant an ugly floor fight on the first day of the new Congress.

Meanwhile, Pelosi was busy on another negotiating front trying to avert a partial government shutdown. On Tuesday, she sat down for the now-famous meeting with President Donald Trump and the Senate's top Democrat, Sen. Chuck Schumer of New York, in the Oval Office.

The public got a rare look at the Pelosi who Democrats have talked about for years -- the fearless, unyielding leader who's commanded respect from so many, even if in an intimidating way.

Pelosi's performance instantly went viral online, particularly when the President suggested she might be weakened in the negotiations because of her fight back at home for speaker.

"Mr. President, please don't characterize the strength that I bring to this meeting as the leader of the House Democrats, who just won a big victory," she shot back.

While Trump and Democrats aren't close to a deal on a spending measure, Pelosi and her Democratic challengers were ready to wrap things up.

Exactly five weeks after that conference call among the so-called rebels, the two sides announced an agreement on Wednesday, each giving concessions that would all but guarantee Pelosi would become speaker -- but only for four more years, at most.

The transitional pitch

It was preordained that Pelosi would have a speaker fight. With Republicans spending tens of millions of dollars in the midterms to paint her as the villain, Democrats were asked in races across the country whether they would support her for the job.

According to a CNN count leading up to the election, close to 30 Democratic incumbents and new candidates who had solid chances of winning in November were firmly on the record opposing Pelosi. While she enjoyed support from the vast majority of her caucus, it would take only a small number to thwart her speakership chances in January.

In October, Pelosi told the Los Angeles Times she would be a "transitional" speaker. It wasn't too surprising a statement -- Pelosi is 78 -- but it was one of her first serious nods that she wasn't planning to be in the top spot indefinitely.

"I have things to do. Books to write; places to go; grandchildren, first and foremost, to love," she said.

As to when she would be leaving Congress, Pelosi didn't budge. "Do you think I would make myself a lame duck right here over this double espresso?" she said in the LA Times interview. Over the next several weeks, Pelosi would continue to keep her timeline a mystery.

With no clear end in sight, the promise of a transition wasn't enough to ease the concerns of those wanting new leadership -- and wanting it soon. A week after the election, the group of rebels started collecting signatures on a letter vowing to oppose Pelosi for speaker, eventually releasing it with 16 names. They claimed others were also a "no" but declined to publicly sign the letter.

The lobbying effort of the anti-Pelosi freshmen also kicked into full gear. The detractors were in constant communication, educating them on the speaker vote process and reminding them it would be their first vote in Congress -- and would they really want to break a campaign promise on their first day in office?

Meanwhile, Pelosi supporters were furious at the idea that Democrats would deny a woman the speakership after record numbers of women won on Election Day. Pelosi helped raise $135 million for Democrats, and she was widely regarded as an effective leader, even by some of her critics. The most common complaint was she had simply been there too long and amassed too much power over the caucus.

Pelosi backers were also swarming the incoming freshmen with texts and phone calls, making it clear that Pelosi had most of the caucus behind her. Freshmen were told that a vote against Pelosi would be a vote for the Republicans, which wasn't entirely true, as a member can vote for anyone for speaker. But it was a talking point that took hold in November and would be repeated by members as they weighed their options.

JUST WATCHED Internet lights up over Pelosi's sunglasses Replay More Videos ... MUST WATCH Internet lights up over Pelosi's sunglasses 02:03

Can you beat someone with no one?

Amid all the lobbying and public posturing, the detractors had one escalating problem: They couldn't find anyone to run against Pelosi.

Rep. Tim Ryan of Ohio, who had challenged Pelosi two years ago for minority leader and won nearly a third of the caucus, toyed with the idea of another run but didn't step forward. Rising Democratic stars like Reps. Hakeem Jeffries of New York and Cheri Bustos of Illinois sought lower-level leadership positions. Reps. Steny Hoyer of Maryland and Jim Clyburn of South Carolina, who've long been ranked No. 2 and No. 3 after Pelosi, would consider a speaker run only if there were a vacancy at the top.

As pressure mounted for a challenger to step forward, Rep. Marcia Fudge, a Democrat from Ohio and part of the group of detractors, considered the idea shortly before Thanksgiving. But within just a few days -- and after a private sit-down with Pelosi -- Fudge announced she was not only holding back from a challenge but also that she would be supporting Pelosi. At nearly the same time, it was also announced Fudge would chair a subcommittee on voting rights, an issue she's passionate about.

And that's how Pelosi would slowly start picking off incumbents -- one by one.

Soon after Fudge, Rep. Brian Higgins of New York, one of the 16 Democrats who had signed the anti-Pelosi letter, announced he would support Pelosi after she gave him reassurances on two of his pet issues -- infrastructure and Medicare. Rep. Stephen Lynch of Massachusetts, another signatory, also flipped after private conversations with Pelosi.

Higgins said others who had signed the anti-Pelosi letter were looking for an "out," and he urged them to press Pelosi for policy commitments in exchange for support (like he did).

"Life is about leverage," Higgins said.

The detractors saw Pelosi's powerful network in full force back at home. Protesters rattled Rep. Seth Moulton, one of the most vocal Pelosi critics, at one of his town halls in Massachusetts. Rep. Kathleen Rice, another outspoken critic, was getting calls from prominent donors and political figures in her home state of New York asking her to back down.

Pelosi supporters also fueled a successful social media campaign that became known as #fivewhiteguys , framing the anti-Pelosi faction as a group of white men -- even though it had multiple women and included a few members of the Congressional Black Caucus and Congressional Hispanic Caucus.

One aide close to the rebels said the sexism campaign was unfair but effective and expressed regret that Fudge, a woman and member of the Congressional Black Caucus, didn't run, as her challenge would "have at least tamped down the sexism cries."

"No one wants to be called sexist. It had nothing to do with that," the aide said. "If it was Steny Hoyer at the top, it would be the same conversation about wanting new leadership."

Flipping 32 Democrats

Soon after Thanksgiving, the caucus held its internal leadership election on a secret ballot to nominate its candidate for speaker. Pelosi won overwhelmingly, losing only 32 votes out of a caucus of 235 voting members.

For Pelosi allies, flipping enough of those 32 Democrats seemed entirely doable. She still had plenty of leverage and plenty of time.

But the detractors maintained they still had the numbers on their side and, even with no challenger, could prevent her from getting to 218 on the floor.

Ryan, Moulton and Rice, on the same day as the caucus vote, had a meeting with Pelosi in her office. They requested that she leave before the end of her first term as the next speaker. The meeting went nowhere.

Meanwhile, another leader in the small group, Rep. Ed Perlmutter of Colorado, had been privately negotiating with Pelosi. The two had a long-running relationship, as Perlmutter was not an outspoken member frequently in the headlines criticizing leadership.

Despite some tension in the group over messaging and strategy, the Perlmutter talks soon became the clear avenue to any potential deal, and the Colorado lawmaker frequently kept the group up to speed on his conversations.

Or as one Democratic aide put it: "In any war, there's a role for soldiers and diplomats. You need soldiers to do fighting, and then you need diplomats to end the fighting. Rarely are those two roles filled by the same person."

Over the coming weeks, Perlmutter and Pelosi would have roughly a dozen conversations behind the scenes, but it wasn't until December 4 that the two reached a turning point.

It was a quiet day on Capitol Hill. President George H.W. Bush was lying in state just down the hallway from Pelosi's office, and Washington was preparing for his funeral as story after story ran about Bush's kindness and decency.

JUST WATCHED Fact check: Trump's heated Oval Office meeting Replay More Videos ... MUST WATCH Fact check: Trump's heated Oval Office meeting 04:59

During a sit-down in her office with Perlmutter, Pelosi expressed openness to term limits for committee chairs and senior leadership. The moment became a game changer for the group of detractors.

Over the next week, the conversation would narrow to focus solely on term limits for the party's top three leaders, which would affect Pelosi, Hoyer and Clyburn. The two men were aware of the negotiations but were not a part of them and were staunchly opposed to term limits.

Perlmutter later reflected on tense conversations with the longtime leaders.

"This place is based on relationships and (when) you start changing those relationships, you can strain some things," he told reporters. "Different folks have different views of all of this. That's just the nature of life, but it certainly applies here."

As word spread that term limits were now on the negotiating table, frustration grew among many in the caucus, especially among Congressional Black Caucus members, who have a history of opposing term limits.

"This whole thing about these guys needing something so that they can land this damn plane is getting silly," said Rep. Cedric Richmond of Louisiana, chair of the Congressional Black Caucus. "Land the plane. Vote for her. And let's please get on."

A final deal

Perlmutter then had a final sit-down with Pelosi. This time he was joined by Rep. Linda Sanchez of California, a member of leadership who made news last year when calling for a new generation of leaders, and Rep. Bill Foster of Illinois, another quiet leader among the so-called rebels.

The trio turned heads as they had to walk through a bustling holiday reception for reporters to enter Pelosi's office.

It was then that they locked in the deal. Pelosi would support a limit of three terms for senior leaders, with the option of running for a fourth and final term but only with approval from two-thirds of the caucus, rather than the traditional simple majority.

The deal would be retroactive. Since Pelosi, Hoyer and Clyburn had already served two terms as speaker, majority leader and majority whip when Democrats last held power, they would be entering their third terms in 2019 and could run for one more term at most.

The concession at first seemed striking, given how reluctant Pelosi had been to place any sort of timeline on her political future. The reality, though, is she was unlikely to stay for very long, anyway.

In return, she won over seven of her detractors, all but guaranteeing she would get the 218 votes on the floor -- yet still make it possible for some of the freshmen to uphold their campaign promises to vote against her on the floor.

The deal was not warmly received by Hoyer and Clyburn. The caucus still has to vote on the term limits for the rules to officially change, and it's unclear whether they'll be approved.

Pelosi has promised to abide by the term limits, anyway.

"I feel very comfortable about what they are proposing, and I feel very responsible to do that, whether it passes or not," she said at her weekly news conference Thursday.

Some in the group notably didn't sign on to the deal. Rice, along with Reps. Kurt Schrader of Oregon and Jim Cooper of Tennessee, were three signatories of the original letter who still maintain their opposition. For some, the deal didn't go far enough; Pelosi would still be holding the reins -- and possibly for another four years.

But many in the group felt they got what they were looking for: a timeline for a transition and a commitment to training more future leaders. "These pieces put into place are going to create the churn," Sanchez said.

Could Pelosi have gotten to 218 on her own, without striking the deal? "She may have," Perlmutter conceded. "People were going one at a time in some respects. And her skills are real, so that's possible."

But as a brutal, embarrassing floor fight on January 3 remained possible, the reticence was growing.

"There was a recognition ... is that how we wanted to start things?" Perlmutter said. "And the answer was at the end of the day: probably no."