This didn't work for me either, even with the AGI in place still wasn't getting the least cost being returned.
My C knowledge isn't good 'nuff to work out why but I think it's something to do with it not correctly working out which is actually cheapest and therefore just throwing the records into memory in the order they are read from the database.
Changing the SQL statement on line 1748 in file 'rate_engine.c' the one before 'table_cost);' from ....
'FROM %s ORDER BY prefix, (startcost + periodcost) / (firstperiod + periods) DESC'
seems to make it work!!!!
Perhaps not the most idea way or working out the cheapest but works for my purposes.