Charmbracelet
Looking In
I do think that gym leaders at the very least ask the visiting trainers how long they've been battling and how many badges they have, and that way they can pick out the pokemon that corresponds to that. We just don't see in the anime.
Have you ever seen that happening in the any of the gyms Ash has battled at?.