How do I calculate cost of a VM execution?

I am running client’s code on VM, and in order to track whether it is profitable, I need to know how much exactly it costed me to run that payload – from the time VM started, to the time VM shutdown.

I can attempt to calculate that based on the time machine was on + hard coded cost (I am using the same machine for everything). However, is there a better way?

We don’t have a great way to do this right now. Your best bet is to use the Machines API (assuming you’re using Machines) to get a list of events. This will have the complete log of start / stop events: Machines · Fly Docs