Air on the suction side of the lift pump will cause erratic idle.
If you have a Caterpillar secondary fuel filter/housing with manual primer pump, next time this happens, turn off the engine. Go back and turn the manual primer pump kural nut counter-clockwise to open. Pump it. If easy to pump (air is compressible), you likely have air in the system. If hard to pump, all is well (fuel is NOT compressible). Be sure to turn the knural nut clockwise to lock.
If not Caterpillar secondary fuel filter/manual primer pump, do you have a clear bowl primary filter? Look for air in it, indicating a problem between tank and primary filter.