Here is how I approach this rather confusing topic, with a slightly different notation.
Let's define down
as $f^\downarrow =f'$ and up
as $f^\uparrow=\int f$ and
similarly double down
as
$f^{\downarrow\downarrow} =f''$ and double up
as $f^{\uparrow\uparrow}=\int\int f$ and so on.
Now to do one round of integration by parts use
$\int f g = f g^{\uparrow} - \int (f^\downarrow g^\uparrow)$
To do two rounds use
$\int f g =f g^{\uparrow} - f^\downarrow g^{\uparrow\uparrow}+ \int (f^{\downarrow\downarrow} g^{\uparrow\uparrow})$
in simple applications $f$ is a polynomial so its down
is a simpler polynomial.
You notice in each term the number of up
moves is one more than down
moves.
Also in the first and last integrals the number of ups and downs are equal, and the integral itself counts as one up.
To do three rounds use
$\int f g =f g^{\uparrow} - f^\downarrow g^{\uparrow\uparrow}+ f^{\downarrow\downarrow}g^{\uparrow\uparrow\uparrow}- \int (f^{\downarrow\downarrow\downarrow} g^{\uparrow\uparrow\uparrow})$.
For example to do $\int x^3 e^{2x}dx$ we notice that the fourth (and subsequent) derivatives of $x^3$ will be zero so we go that far and get a convenient zero in the last integral
$\int x^3 e^{2x} = (x^3) (e^{2x})^{\uparrow} -(x^3)^\downarrow (e^{2x})^{\uparrow\uparrow}+ (x^3)^{\downarrow\downarrow}(e^{2x})^{\uparrow\uparrow\uparrow}-(x^3)^{\downarrow\downarrow\downarrow} (e^{2x})^{\uparrow\uparrow\uparrow\uparrow}+\int (x^3)^{\downarrow\downarrow\downarrow\downarrow} (e^{2x})^{\uparrow\uparrow\uparrow\uparrow}$
$\int x^3 e^{2x} =(x^3) (e^{2x}/2)-(3x^2)(e^{2x}/4)+(6x)(e^{2x}/8)-(6)(e^{2x}/16)+C$
Now, in your example you are dealing with a tricky integral where you go so far on the right until the integral looks like the original one, just differing by a constant multiple. At which point you stop and bring the right integral to left, hence completing the circle so to speak. Here is how it goes: for cosine two rounds of either up or down brings us back to cosine so we use
$\int e^{ax} \cos bx = (e^{ax}) (\cos bx)^{\uparrow} - (e^{ax})^\downarrow (\cos bx)^{\uparrow\uparrow}+ \int (e^{ax})^{\downarrow\downarrow} (\cos bx)^{\uparrow\uparrow}$
$\int e^{ax} \cos bx = (e^{ax}) (\sin bx /b) - (a e^{ax}) (-\cos bx /b^2)+ \int (a^2e^{ax}) (-\cos bx /b^2)$
Now you bring the last integral to the left and factor it (postpone the constant to the last step)
$(1+a^2/b^2)\int e^{ax} \cos bx = 1/b e^{ax}\sin bx + a/b^2 e^{ax}\cos bx $
Now clean up to get
$\int e^{ax} \cos bx = e^{ax}(b\sin bx + a \cos bx)/(a^2+b^2) +C $