During the female titan arc, how did Erwin specifically know that the traitor would be from the 104th training squad?
After the death of Sawney and Bean, Erwin then was able to narrow down the list of suspects, as only a soldier was able to kill the titans with 3DMG. Because of this, he went around asking cryptic questions to try to weed out liars and traitors, making people second guess themselves.
Then comes the night of the ceremony where the cadets join the Survey Corp. That night, Erwin instructed Levi to take Eren out on a ride to recon the area. Why exactly would he do this? It's clear it wasn't just a normal exercise, as Levi was rushing Eren and seemed frustrated and stressed. This was Erwin's way of narrowing down the suspects. By having Eren leave and the ceremony on the same night, he could deduct that if the enemy did not attack Eren or at least show up to the castle to kidnap him when it would have been strategically best to do so, then it must mean that they were otherwise preoccupied somewhere else. Where else? The ceremony. If someone was missing from the ceremony, it would be suspicious and draw attention to them. So this narrowed down the suspects to the 104th Trainee Corp.
Captain Erwin is known for his cunning. He is always steps ahead of the situation:
1. Expecting spies/traitors in a war is common
2. The appearance of the Colossal Titan and the Armored Titan seemed more than just "random". They always appeared out of nowhere, but perfectly timed - i.e. when the Coordinate power was lost from the bloodline, when the Colossal Titan appeared in front of Eren after the survey corps success etc.
3. During the battle of the Trost District, they see Marco dead on the floor where half of his body is eaten. However, it was pointed out that his maneuver gear was missing, leading to think that someone took it. But the majority (or only) of the survivors were members of the 104th squad. Later, when Sawney and Bean were killed, they did a thorough check on all members' gears but all were fine because Annie (the killer) used Marco's gear