THE LAST hours of the endless showdown vote on contra aid Thursday night came down to the story of Claude Pepper and Lindy Boggs. He forgot he was a Democrat. She remembered.

Pepper, the 87-year-old czar of America's elderly, made the closing . speech for the Republicans, who were delighted to rub Speaker Jim Wright's nose in the Democrats' division. Pepper knows that Wright is totally exposed on the contra issue and needed every last vote. But he knows his own needs better. He represents a heavily Cuban district in greater Miami and lives in fear that a Cuban will challenge him for his seat. He declared eloquently that Reagan is right.

As Pepper spoke, Tony Coelho of California, the Democratic whip, stirred resentfully in his seat. Wright leaned over and patted him on the shoulder. The speaker knows that Pepper, a Roosevelt Democrat, was defeated for the Senate years ago by charges that he was soft on communism, and since he obviously feels there is no life after Congress, he cannot content himself with simply voting Republican. He must speak. He has done it before -- he was the Republicans' show speaker on another Red-related issue, aid to Angola.

When Pepper finished, Republicans leaped to their feet and gave him a standing ovation. The Democrats joined in. They did not wish to give the Republicans the satisfaction of knowing that they were wounded. Besides, they may need Pepper in their districts at campaign time. He will travel, and he is dynamite with senior citizens.

Wright, who had made the closing Democratic speech -- a quiet, unadorned plea for support of the Arias peace plan, joined the line of people who were anxious to wring Pepper's hand for one reason or another.

"If it was anybody else but Claude," says Rep. Brian Donnelly (D-Mass.) "he'd lose his seat as chairman of the Rules Committee -- I mean if we had lost."

During the endless hours of debate, while the White House was offering bridges, as one wag suggested, to members who didn't even have rivers, it seemed that Reagan might win again. The pressure was heavy. In the cloakrooms, Democrats talked about Lindy Boggs -- gone, they said.

Boggs, a gracious southern gentlewoman, was having an awful time about the vote. "I'm in a mess," she told a friend during the supper hour in the House dining room. She has voted against aid before, and been chastised by her rabidly pro-contra local newspaper. She faced the prospect of being the only holdout against Reagan in the Louisiana delegation. Rep. Buddy Roemer, her newly elected governor, flew in to make an impassioned, early speech about the folly of trusting Daniel Ortega. Wright, whose own future was on the line, did his best with her, but understood. "I couldn't fuss with her," he said.

Boggs had been invited to the White House and given a tete-a-tete with President Reagan about the importance of keeping the heat on the Sandinistas. Secretary of State George Shultz called her.

And then she heard from an old friend. At the Thursday morning whips' meeting, the final one before the vote, they listed Boggs as lost. Rep. Mary Rose Oakar (D-Ohio) mentioned that she had seen former speaker Thomas P. O'Neill on the "Today" Show. "Tip's back in town," she said. "Couldn't we use him?"

Ever quick off the mark, Coelho called O'Neill, who is recovering from grave cancer surgery. He didn't have to ask. Tip offered to help. He was given Boggs' name.

O'Neill called her up, and the conversation went in the old familiar way. He and Boggs are special friends. When Boggs' husband Hale, then the majority leader, disappeared on a flight over Alaska and months went by without a trace of the victims, O'Neill asked Lindy Boggs about the propriety of announcing his own candidacy for the post. Boggs urged him to go for it. O'Neill never forgot, and when she came to the House as a member, he watched over her.

Last Thursday O'Neill walked her through her worries. Her constituency, a majority of which is black, could care less about Reagan's war. As a Catholic, O'Neill said, she should think of the morality of the U.S. position. The Maryknoll sisters -- his beloved Aunt Eunice had been one -- told him what was really going on in Managua, and it was nothing like Reagan's version. (O'Neill always took his Central American briefings from nuns, the State Department used to complain.) He did not have to tell her that they are both Democrats.

When the electronic roll-call began well after 10 p.m. and the names went up on the wall, a red star, indicating a "no" vote appeared beside Boggs' name. The reporters were astonished. Afterwards in the halls, when Democrats were hugging each other and the lobbyists, Boggs, looking exhausted, said she had been won over by the argument that the Arias plan could bring peace. No one knows what else changed her mind. It could have been seeing Pepper going up against the Democrats. It could have been conscience.

Mary McGrory is a Washington Post columnist