If you need to use the value of a function inside the step() function you need to implement your optimizer as requiring that closure argument. Once you call the closure it will return you the loss and you can inspect its value. You can take a look at how L-BFGS is implemented 28.