The House last night handed President Reagan a narrow and modest victory, voting 205 to 203 for a new four-year farm bill that pleased almost no one.

The vote on the final conference measure came after months of bitter and divisive debate and added appropriately dramatic suspense to an end of the 1981 session of Congress.

As time ran out, the roll was deadlocked at 202 and the well of the House was aswarm with latecomers and members attempting to switch their votes.

Speaker Thomas P. (Tip) O'Neill Jr. (D-Mass.) quickly gaveled the vote over and it ended with a squeaker win for the White House, which had put heavy pressure on House conferees to give in to budget demands and rein in support programs.

The $11 billion version adopted last night will cost about the same as the bill passed by the Senate, and about $6 billion less than one passed earlier by the House. Unlike the earlier House bill, the compromise provides a price support program for sugar and extends peanut supports and production controls.

The conferees' acceptance of sugar and peanut programs generated a wave of opposition from consumer-oriented members, who, along with members aligned with the powerful dairy lobby, almost derailed the conference report.

The costly dairy program was curbed, albeit not as sharply as the White House wanted.

On the other hand, the bill retains so-called target price payments or direct income supplements for grain farmers that the president opposed.

But reduced price support and target price levels for the other major commodities--wheat, corn, feed grains, rice, cotton, wool--generally were in line with the administration's demands.

Thus in general, the administration failed in its efforts to restructure farm programs and in some cases abolish them, but succeeded in restraining their cost.

Agriculture Secretary John R. Block said last night that the president will sign the measure into law. Block said the bill provides "an adequate safety net and a framework for prosperity in agriculture."

But contentious debate last night on the House floor indicated that few members agreed with the Secretary. Some criticized it as too costly; a vast majority denounced it as either treachery to the farmer or an affront to consumers.

Rep. E (Kika) de la Garza (D-Tex.), chairman of the Agriculture Committee, typified the sentiment. He said he didn't like it, but then unwound a fervent appeal for passage of the compromise.

He warned that with defeat of the bill, farm programs would revert to a 1949 authorizing law that would bring higher costs to taxpayers and chaos to agriculture.

And, de la Garza said, neither the Senate conferees nor the administration would settle for anything but the conference report. "This is the best we can do under the circumstances," he said. "I have seen the crystal ball. It is not there."

Another key figure who apparently peered into the same crystal ball , Democratic Whip Thomas S. Foley (Wash.), was credited by Block for providing "critical help" in getting the bill passed.

For weeks, Foley, a former chairman of the Agriculture Committee, had adamantly opposed the administration's demands for lower grain price supports and targets. But he reversed himself this week and yesterday told the House that the alternative--the 1949 act--would mean only bad news for farmers and taxpayers.

Floor discussion yesterday was largely a repetition of arguments that have resounded for months in the most heated farm bill debate in recent times.

The tone was set early by the budget-cutting administration, which proposed a measure that would have cost about $2.5 billion less than the version adopted yesterday and which laid siege to the dairy program and the target price system.

Farm-state legislators contended that the austerity approach would severely limit farmers' dwindling incomes and force farm prices to new lows.

Consumer-oriented legislators argued that dairy, sugar and peanut programs were costly assaults on consumers' pocketbooks and could not be justified in an era of restrained federal spending.

"We ought to wear an armband of mourning for farmers," said Rep. Pat Roberts (R-Kan.) who wanted more aid for wheat producers in his district.