Gloucester were denied a valuable Amlin Challenge Cup draw in France following a last-gasp try by Agen.
Tongan number eight Opeti Fonua crashed over in the final seconds with the scores locked at 19-19, to complete an emphatic turnaround for Agen.
The try, awarded by match official Neil Patterson of Scotland, left Gloucester fuming after Fonua appeared to lose control of the ball as he crossed the line.
That was Fonua's second of the game as Gloucester let slip a 13-3 half-time lead at Stade Armandie. Nick Robinson kicked a brace while his opposite number Valentin Courrent scuffed his first two efforts before success with his third.
However it was the boot of Robinson that set up Gloucester's try in the 23rd minute. His kick to touch set up a perfect platform from which his pack could launch an attack and after Jim Hamilton collected the lineout, Scott Lawson scampered over from close range.
The tide began to turn when Gloucester's veteran flanker Peter Buxton was sin-binned 10 minutes into the second half, handing Courrent a penalty. It also left the Gloucester pack undermanned, and Fonua capitalised when he crashed over in the 54th minute.
Courrent converted to level the scores at 13-13 and added a penalty before trading penalties with Robinson to leave the scores locked with a minute remaining. But Mike Tindall was sin-binned for a dangerous tackle and Fonua settled the tie with his late burst.