Future expat confused by foreign tax credits

I am considering relocating to a foreign country with a tax treaty with the US which in theory prevents being double taxed but am having a hard time understanding how this works.

 

Background...I am retired, receiving social security and annuity payments; the annuity was purchased (invested in?) with money from my 401k so in the US both the annuity and SSA are taxable income.  I also have dividends and gains on non-retirement money (taxed when I earned the principal, but earning/gains are subject to tax.  All of my income is generated in the US and is passive income (at least as I understand it).

 

As I understand it, if I get residency in a foreign country and live there for 183 days or more, that country expects me to pay taxes to them.  The US will also expect me to pay taxes.  Do I do the foreign filing first, and if so, does the amount I paid to the foreign country reduce my US income on a dollar-for-dollar basis?  Is there a maximum amount of the credit that can reduce my taxes due to the US?