The discrepancy could be due to "Back Linking for Tags" being Enabled in the query.
The Total cost should match after changing the code in the query "needBackLinkingForTags" from true to false. Note that AWS does not support tag backlinking so that will need to be disabled when comparing Cost Explorer datasets in the FlexReports.