Media playback is not supported on this device
Rangers, who have already clinched the Championship title and promotion, have now gone three games without a victory.
Halkett's first goal of the season - the final piece of action in the first half - won the points.
And it will give second-bottom Livi renewed hope that they can avoid relegation via the play-offs.
This was only the fifth defeat of Rangers' league season, but Mark Warburton's side have now not won since beating Celtic in the Scottish Cup semi-final.
Livingston had gone into the game with only 17 fit players and gave a first start to 16-year-old Matthew Knox.
The highly talented midfielder has actually been training with Rangers, who had a bid in the region of £100,000 rejected for his services.
Knox is also interesting Chelsea, Everton, Manchester United and Tottenham Hotspur.
The fact that virtually the length of the division lies between the teams was underlined by the early possession and pressure.
It was all Rangers from the first whistle and Andy Halliday, Gedion Zelalem and Michael O'Halloran all had opportunities to score.
Incredibly, though, it should have been Livingston taking the lead. Zelalem lost the ball in his own half and Danny Mullen suddenly found himself clear, but the midfielder shot wide.
The warning bells were ringing for the division's champions when Sam Stanton also had a dancing run and a shot past the goalkeeper's left port.
The half did, indeed, come to a remarkable finish. In its dying seconds, young Knox whipped in a fabulous free-kick from the left and Halkett met it with the perfect header to flash the ball beyond diving goalkeeper Wes Foderingham.
Rangers supporters in the crowd of 5,021 looked like they could hardly believe the scoreline.
The second half picked up on the first half script. The Ibrox side continued to huff and puff and fluff chances.
Livingston grew in confidence as the league's champions produced one of their poorest showings of the term.
The West Lothian side should have had a second goal, but Liam Buchanan put the simplest of headers over the bar from inside the six-yard box.
The same player, one on one with Foderingham, was only denied by a block from the the goalkeeper.
Livingston were worthy winners. Rangers looked a shadow of the team that grabbed the Old Firm headlines and are preparing for the cup final against Hibernian.
Champions Rangers' slump in form continued thanks to Livingston defender Craig Halkett's first-half header against his former club.