How to count the number of function evaluations in NIntegrate

A very simple modification (adding ?NumericQ) achieves the result you expected.

i = 0;
f[x_?NumericQ] := (i += 1; Tanh[x] Sin[Exp[x]] Exp[-0.55 x^2 Exp[x^2]])
Print[NIntegrate[f[x], {x, -2, 1}]]
Print[i]

(* 0.0901049*)

(*122*)

The issue is that NIntegrate tries to evaluate f[x] symbolically. In your version, it is called only once and is replaced by Tanh[x] Sin[Exp[x]] Exp[-0.55 x^2 Exp[x^2]. In my version, the f[x] is returned unchanged until specific numerical values are given to x.


Try the option EvaluationMonitor

Block[{k = 0}, {NIntegrate[f[x], {x, -2, 1}, EvaluationMonitor :> k++], k}]

{0.0901049, 121}

Without using EvaluationMonitor you can do

ClearAll[f, ff]
f[x_] := Tanh[x] Sin[Exp[x]] Exp[-0.55 x^2 Exp[x^2]]

i = 0;
ff[y_?NumberQ] := Block[{x = y}, i++; f[x]]

{NIntegrate[ff[x], {x, -2, 1}], i}

{0.0901049, 121}