Non-Relativistic Electron Hamiltonian I have determined a Hamiltonian for an electron using an appropriate Lagrangian of the form
$$ L=\frac{1}{2m}\left(m\overrightarrow{v}+\frac{q}{c}\overrightarrow{A}\right)^2-\frac{q^2}{2mc^2}\overrightarrow{A}\cdot\overrightarrow{A}+q\phi.\tag{1}$$
then by relating the Lagrangian to the Hamiltonian using the identity
$$H=\overrightarrow{v}\cdot\overrightarrow{p}-L=\overrightarrow{v}\cdot\left(m\overrightarrow{v}+\frac{q}{c}\overrightarrow{A}\right)\\-\frac{1}{2m}\left(m\overrightarrow{v}+\frac{q}{c}\overrightarrow{A}\right)^2+\frac{q^2}{2mc^2}\overrightarrow{A}\cdot\overrightarrow{A}-q\phi.\tag{2}$$
I've then simplified this and made $q=-e$ so that is describing an electron.
$$ H=\frac{1}{2m}\left(\overrightarrow{p}+\frac{e}{c}\overrightarrow{A}\right)^2+e\phi.\tag{3}$$
This is as far as I have managed to go however I have read that, if we have an electron in a purely magnetic field there is an additional interaction such that
$$H_I=\frac{g}{2}\frac{e\hbar}{2mc}\overrightarrow{B}\cdot\overrightarrow{\sigma},\tag{4}$$
where $\overrightarrow{\sigma}=2\overrightarrow{s}$ this makes our Hamiltonian the following
$$H=\frac{1}{2m}\left(\overrightarrow{p}+\frac{e}{c}\overrightarrow{A}\right)^2+\frac{g}{2}\frac{e\hbar}{2mc}\overrightarrow{B}\cdot\overrightarrow{\sigma}.\tag{5}$$
Note: $g=2$.
How do I derive the factor $H_I$? is the $H_I$ factor $\phi$ when the electron is in the magnetic field?
 A: There is no way you can derive the spin interaction term $H_I$ from non-relativistic mechanics and using $\mathbf p,\mathbf r$ only. The spin is an intrinsic property of the electron and you have to postulate it. Here I propose three ways to convince yourself:

*

*One option is to take it as it is, an interaction with an angular momentum. You can convince yourself that if you expand $(\mathbf p +e \mathbf A)^2$ with the gauge $\mathbf A = \mathbf B \times \mathbf r /2$ you get various terms, one of them is of the form:$$H_L=\frac{\mu_{\rm B}}{\hbar}\mathbf B \cdot \mathbf L\;.$$ As the spin $\mathbf S$ is also an angular momentum operator, to obtain the true Hamiltonian, you have to do the following replacement: $$H_L\to H_{L-S}=H_L+H_I=\frac{\mu_{\rm B}}{\hbar}\mathbf B \cdot (\mathbf L+ g \mathbf S)\;,$$
where $g$ is some constant (the gyromagnetic factor).


*Another possibility is to give up the nonrelativistic origin. Take relativistic Dirac equation and work your way to a non-relativistic equation, you will find that the true Hamiltonian (in the non-relativistic limit) is
$$H = \frac12 \frac{[2\mathbf S \cdot (\mathbf p + e\mathbf A)]^2}{2m\hbar}\,$$ if you expand that you get $H_I$ with $g=2$.


*There is still a third possibility. If you have ever heuristically derived Dirac equation you'll know that  it involves the linearization of the relativistic dispersion relation $E^{\rm rel}= \sqrt{p^2c^2+m^2c^4}$ imposing that you obtain an equation that only has linear derivatives ($p$ and not powers of it, $p^2,p^4,\cdots$). The alternative derivation of $H_I$ consists on linearizing, not the relativistic $E^{\rm rel}$, but your usual non-relativistic kinetic energy $E=p^2/2m$ instead. The resulting formula is called the Lévy-Leblond equation and naturally leads to Pauli equation (Schrödinger+$H_I)$ when you try to solve it. A step by step derivation can be found in Greiner's Quantum Mechanics. Interestingly, it also provides $g=2$.
Which is the best? In my opinion the derivation should start from the most general Lagrangian and not a non-relativistic one. However, when one does not know the most general Lagrangian it is good to have different approaches to be convinced of the final result.
A: I assume that the analogous situation in classical mechanics is
the following. If you are describing the motion of an object, its
Hamiltonian looks one way if you treat your object as a
mass-point. However, if you start considering the object as a
rigid body and not a point, your model starts to include
description of the time-evolution of the body's center of mass and
the evolution of it's angular momentum. The situation you are
encountering seems to be analogous. Your first Hamiltonian
describes the motion of a charged particle treated as a charged
point. However, the addition of the second term includes
description of its magnetic moment, i.e. your charged particle is
considered as a dipole (or something that has a magnetic moment,
like a rotating charged sphere) rather than a point. So the
second, extended Hamiltonian describes the evolution of both the
position of the electron and the evolution of its magnetic moment.
I hope this explanation makes sense.
The factor $H_I$ has nothing to do with the electric potential
$\phi$. It has to do with the dynamics of  the magnetic moment.
Here is how I see the "derivation" you seek. When describing classically your charged particle, say the electron, you think of it as follows: it is a
little magnetized segment, not a point. The center of the segment
is its center of mass. When describing the dynamics of this
magnetized segment, you would like to know where it is in space
and how it is oriented in space. Thus, to describe fully the
configuration of the segment, you want to know the position $x \in
\mathbb{R}^3$ of its center, where $x = (x_1, x_2, x_3)$ is a
vector in three space, and the orientation of its axis (which is
aligned with the segment and is oriented according to the magnetic
field of the segment) determined by a vector $\mu = (\mu_1, \mu_2,
\mu_3) \in \mathbb{R}^3$ in three space, so that the vector $\mu$
is aligned with the segment. The vector's orientation and length
are determined in accordance with the segment's own magnetic field.
Now, you want to know how $x = x(t)$ and $\mu = \mu(t)$ evolve in
time under external static (i.e. time independent) magnetic field,
given by a magnetic vector potential $$A = A(x) = \big(A_1(x),
A_2(x), A_3(x)\big)$$ Observe that the magnetic field doesn't
change with time (hence static), so $\phi \equiv 0$. However, the magnetic field
could be inhomogeneous in space, i.e. it may vary from point to point. The vector field of the external magnetic field is
determined by $B = B(x) = \nabla \times A(x)$. As it is usually
the case in classical dynamics, in order to track the time
evolution of your object's configuration $x(t), \, \mu(t)$, you
have to track, in the Lagrangian picture, the time evolution of
the quantities $x(t),\, v(t), \, \mu(t)$ where $v(t) =
\frac{dx}{dt}(t)$ is the velocity of $x(t)$. In the Hamiltonian
picture, you have to track  $x(t),\, p(t), \, \mu(t)$ where $p(t)$
is the conjugate momentum of $x$ and carries more or less the same
information as the velocity $v$. The Lagrangian and the
Hamiltonian functions consist of the terms that determine the
dynamics of $x, p$ plus the terms that determine the dynamics of
the magnetic moment $\mu$. Thus, the  Lagraingian for $x, v$ is
$$L_0(x,\dot{x})=\frac{1}{2} \, m {v}^2+
{q}\, {A}(x) \cdot v = \frac{1}{2} \, m {\dot{x}}^2+ {q}\, {A}(x)
\cdot \dot{x}$$ where $v = \dot{x} = \frac{dx}{dt}$. The
Lagrangian term responsible for the time evolution of the magnetic
moment $\mu$ is the magnetic potential energy $$U = U(x, \mu) = -
\, B(x) \cdot \mu$$ In these notations $ \, \cdot \,$ is the dot
product of the three dimensional space $\mathbb{R}^3$ and $v^2 = v
\cdot v$. To form the total Lagrangian, we need to subtract the
potential energy $U$ from the rest of the lagrangian $L_0$,
obtaining the full lagrangian that determines the time evolution
of the position $x$, the velocity $\dot{x}$ and the magnetic
moment $\mu$:
$$L = L_0 - U$$
$$L(x, \dot{x}, \mu) = L_0(x, \dot{x}) - U(x, \mu)$$
$$L(x, \dot{x}, \mu) =\frac{1}{2} \, m {v}^2+
{q}\, {A}(x) \cdot v +  B(x)\cdot \mu$$ Analogously, in the
Hamiltonian picture, the Hamiltonian for $x, p$ is
$$H_0(x,p)=\frac{1}{2m} \big( p -
{q}\, {A}(x) \big)^2$$ The Hamiltonian term responsible for the
time evolution of the magnetic moment $\mu$ is the magnetic
potential energy $$H_I = U = U(x, \mu) = - \, B(x) \cdot \mu$$ To
form the total Hamiltonian, we add the potential energy $U$, i.e.
the magnetic moment potential Hamiltonian $H_I$, to the rest of
the Hamiltonian $H_0$, obtaining the full Hamiltonian $H$ that
determines the time evolution of the position $x$, the momentum
$p$ and the magnetic moment $\mu$:
$$H = H_0 + U$$
$$H(x, p, \mu) = H_0(x, p) + H_I(x, \mu) = H_0(x, p) + U(x, \mu)$$
$$H(x, p, \mu) =\frac{1}{2m} \big( p -
{q}\, {A}(x) \big)^2 -  B(x)\cdot \mu$$ Consequently, the
evolution equations for $x, p, \mu$ are
\begin{align*}
\frac{dx}{dt} &= \frac{\partial H_0}{\partial p}(x,p)\\
\frac{dp}{dt} &= - \frac{\partial H_0}{\partial x}(x,p) -
\frac{\partial U}{\partial x}(x,\mu)\\
\frac{d\mu}{dt} &= \frac{\partial U}{\partial \mu}(x,\mu) \times
\mu = - \, B(x) \times \mu
\end{align*}
Observe the form of the third equation. It looks like that because
$\mu$ is a type of quantity very closely related to angular
momentum and its dynamics is driven by torques not by forces.
Double check all the signs (pluses and minuses), because I did not
check any of these derivations carefully, I simply derived them
off the top of my head, relying on general principle and
philosophy.
Finally, if you think about the magnetic potential $U = - \, B(x)
\cdot \mu$, it makes sense. Whenever the magnetic moment $\mu$ is
aligned with the magnetic vector field $B(x)$ the potential $U$
attains its minimum value, and if you look at the third equation,
you see that $B(x) \times \mu = 0$. Thus, no precession of $\mu$
will occur as long as $\mu$ is aligned with the magnetic field.
However, if $\mu$ is not aligned, the potential $U$ increases, and
precession starts to emerge because $B(x) \times \mu \neq 0$. In
the special case when $\mu$ is perpendicular to the magnetic
field, the potential $U=0$ which is its largest value, so the
precession of $\mu$ is the strongest.
