[Physics] When should I use $U=QV$ as opposed to $U=\frac{QV}2$

chargeelectrostaticspotentialpotential energyvoltage

In my electricity course, I am having trouble understanding the difference in between $U=QV$ and $U=\frac{QV}2$ when talking about energy stored in a system.
My idea was that when the potential is created by the charges arriving to the system, we would use $U=\frac{QV}2$, as the charges themselves are building the system potential as they arrive; on the other hand, when a potential is imposed from the outside, we would use $U=QV$.

As you said, if you have for example a particle of charge $q$ in an external electric potential $V$, then its energy is given by

$$E=qV$$

On the other hand, take a capacitor for example. The charge $Q$ that accumulates and the voltage across it $V$ satisfy the relation

$$Q=CV$$

where $C$ is the capacitance of the capacitor - merely a proportionality constant. Then if you charge the capacitor from $Q=0$ to $Q=q$, the energy you get is an integral over the infinitesimal contributions

$$E=\int_{0}^{q} V{\rm d}Q=\frac{1}{C}\int_{0}^{q}Q{\rm d}Q=\frac{q^{2}}{2C}=\frac{qV}{2}$$

I hope those concrete examples made it clearer.