Use an autocommand using the vim API:
vim.api.nvim_create_autocmd("InsertLeave", { callback = function() print("Exited insert mode!"); end });