Anticipate the best, prepare for the worst. Carrying a digital voice recorder that is running always helps. Remembering that disagreements with the local cops are better handled in the courtroom than face down in the street wearing handcuffs is always good.
"Am I free to go?" If yes, do so - Get! Be gone! Go away!
If not - "I want to speak with an attorney before answering any questions." And then KYBMS until a lawyer shows up and you have conferred with them.