By Jeremy Bowen
Middle East Editor, BBC News
The Livni camp had a long wait before they were certain of victory
In the end, only a few hundred votes separated Tzipi Livni and Shaul Mofaz. But Ms Livni has won, and now she gets the chance to form a government.
The day before the vote, one of her strategists, full of confidence, sat back and predicted that if it was going to be a big victory, he would know by 1730 on the afternoon of polling day.
But the winning margin was narrow, and Ms Livni's camp had to wait until the middle of the night to be sure they had it.
For hours during the evening, the hall in Tel Aviv that Kadima, Israel's ruling party, had hired to unveil its new leader was full of journalists and TV crews.
It took much longer for Kadima's grandees to start to arrive.
They had been watching their televisions, not wanting to put themselves in front of a microphone until they had a better idea of who the new boss was going to be.
Ms Livni's first job will be to build a new governing coalition.
Israel's electoral system makes it fairly easy for a small party to win seats in parliament.
You might argue that at least votes are not wasted, as they sometimes seem to be in "winner-takes-all" democracies like Britain.
But it also means that narrow interest groups hold the balance of power. They can extract - some would say extort - big financial or political favours in return for their support.
Israeli voters care about economic and social issues as well as security
That makes government-forming difficult.
Ms Livni has a maximum of 42 days to get it done.
One of her advisers said she ought to know within a week if a deal was going to be possible.
If she cannot form a government, Israel's president, Shimon Peres, could ask someone else to try. But more likely as a next step would be a general election, after another three months.
If Ms Livni does form a government, she faces big challenges.
Every country is feeling the gale blowing through the world's financial markets.
Israel's economy has been doing well, on the back of highly successful technology companies.
But the gap between rich and poor has been widening, and a country that once saw itself as socialist and egalitarian has learned to love capitalism and has turned itself into a land of haves and have-nots.
Israeli voters tell pollsters that they care more about these financial and social questions than the perpetual issues of war and peace.
All the same, war and peace always top the agenda of Israeli leaders.
Differences between politicians here are often measured by the amount of occupied Palestinian land that they are prepared to give up in return for a peace deal.
Tzipi Livni was born into the aristocracy of the Israeli right wing, to a family that believed that Gaza and the West Bank, at the very least, should be part of the Jewish state, with an undivided Jerusalem at its centre.
But she has moved away from that.
Would-be Prime Minister Livni believes it is in Israel's interest to have a Palestinian state next door.
A Livni government would continue the current talks with the Palestinians, which she supported strongly as foreign minister.
They were supposed to produce agreement on the establishment of a Palestinian state by the end of this year. She is said to be ready to give up territory, to move Jewish settlers and, if she wants a deal, she will also have to allow Palestinians their own capital in Jerusalem.
Mr Mofaz has talked tough on the Iranian threat
The outgoing Prime Minister Ehud Olmert, ending his career in disgrace after a series of investigations for corruption that could land him in the courts, said he, too, wanted a deal. He was not able to deliver it.
Assuming he meant what he said, a fundamental truth of Israeli politics reasserted itself: the deals that Israeli prime ministers have to strike to form a government tie their hands.
HAVE YOUR SAY
Livni can negotiate with the Syrians and Palestinians. Israel is in for a new era
Even if Tzipi Livni were able to make an agreement with the Palestinians, there is no guarantee that she could sign it and stay prime minister long enough to deliver her side of the bargain.
Most Palestinians anyway are deeply sceptical about any Israeli idea of a peace deal.
The expansion of Jewish settlements in the West Bank, illegal under international law, continues.
So does bureaucratic pressure on Palestinians in Jerusalem to move out.
Despite requests from its American allies, Israel has not since 2006 removed any of the so-called "outposts" which are also illegal under its own laws.
Fears over Iran
Indirect talks with the Syrians have also started. For an Israeli government, the issue is whether Syria will stop supporting its enemies in Lebanon, Gaza and Iran if it gets back the Golan Heights, which Israel seized 40 years ago.
Just keeping the talks going is likely to be a more realistic ambition in the foreseeable future.
Few Israelis believe Iran's claim that it does not want nuclear weapons
But a Livni government's biggest agenda item is going to be the threat Israel believes come from Iran's nuclear ambitions.
A senior official at the Israeli Ministry of Defence told me that, as far as he was concerned, Iran represents as much of a danger to Israelis as Hitler did to the Jews of Europe in the 1930s.
Like every other Israeli I have talked to on the subject, he did not believe Iranian denials that they do not want nuclear weapons.
He is convinced that Iran is developing a bomb which could be used to threaten, or even to destroy Israel.
Ms Livni, as foreign minister, has been trying to persuade Israel's Western friends that it is their problem, too.
It is not clear whether or not she shares the belief of the defence ministry in Tel Aviv that diplomacy cannot end the crisis.
The question her opponent Shaul Mofaz, a hawkish retired general, might be asking himself the morning after his narrow defeat, is whether Ms Livni would be prepared to go to war with Iran if it looked as if the Islamic Republic was going to get a nuclear weapon.
Because that is the decision that many Israelis believe their next prime minister is going to have to take.