Optimization.jl’s callbacks do this. You always return false to keep going, or true to stop the optimization. For example:
callback = function (state, l; doplot = false) #callback function to observe training
return l < 0.5
end
would make it halt when the loss is less than 0.5