@ossobuco,
I agree with your choice of the word 'had' (over 'have').
Thus, it is acceptable to say: "So, how long had you lived in San Francisco?"
or
"So, how long had you stayed in San Francisco?"
To add another acceptable alternative, I'd say:
"So, how long was your stay in San Francisco?"
FYI, the use of 'stay' in this instance is a noun.