The modulus operation $\pmod p$ is performed at each step and reduces the result into $\bmod p$. And a clever implementation can use the Fermat's Little Theorem instead of taking the power than reducing to modulo $p$. After that it's possible to use the modular version of repeated squares algorithm or similar.
Example 1) code used from sublimerobots
sharedPrime = 23 # p
sharedBase = 5 # g
aliceSecret = 600 # a
bobSecret = 1500 # b
Alice Sends Over Public Chanel: 8
Bob Sends Over Public Chanel: 4
Privately Calculated Shared Secret:
Alice Shared Secret: 2
Bob Shared Secret: 2
Example 2)
sharedPrime = 23 # p
sharedBase = 5 # g
aliceSecret = 6000000 # a
bobSecret = 15000000 # b
Alice Sends Over Public Chanel: 8
Bob Sends Over Public Chanel: 4
Privately Calculated Shared Secret:
Alice Shared Secret: 2
Bob Shared Secret: 2
I think you are confusing the mathematical representation and the actual value.
In the sense of optimization, the code from sublimerobots
is not good. Actually. instead of
bobSharedSecret = (A**bobSecret) % sharedPrime
a faster version
bobSharedSecret = pow(A,bobSecret,sharedPrime)
which uses modular binary exponentiation.