Is it possible to access the iteration/step number inside a Stan program?

The function is here. Note that the last line says return iter_count-24;. If the header section of the sample file has changed, 24 might no longer be the correct number here.

If you have it in the same folder as your original stan file, you just need to include

functions { 
   int get_iter(int id);
}

id is just a number used to identify the correct sample file, in case there are multiple in the folder. (in rstan i would use the option sample_file = paste0('tmp',id,'csv') )

When I used it to incorporate uncertainty about weights into a regression model, convergence was good, there were no divergent iterations, but there were BMFI warnings.

I should also say that I myself don’t think this was a 100% good way to do things, but my model was so large (high N) and I already needed to fit each model 50 times for 50 imputed data set, and so I decided against also fitting one model per set of weights.

All this just to say that in case this is about using weights, there is a way to do this without accessing iteration numbers, which I think is preferable when computationally feasible.