"Double agent" is a counterintelligence term for someone who pretends to spy on a target organization on behalf of a controlling organization, but in fact is loyal to the target organization. Double agents may be agents of the target organization who infiltrate the controlling organization, or may be previously loyal agents of the controlling organization who have been captured and turned by the target; the threat of execution is the most common method of turning a captured agent into a double agent. Compare to defector.
Double agents are often used to transmit disinformation or to identify other agents as part of counter-espionage operations. They are often very trusted by the controlling organization, since the target organization will give them true, but useless, information to pass along.
The term "double agent" is often used in popular media erroneously to refer to someone acting simply as a spy or secret agent. A spy simply relays information from a target to his or her controlling organization.
A triple agent pretends to be a double agent for the target organization, but in fact is working for the controlling organization all along. Usually, he keeps the trust of the target organization by feeding information to them that apparently is very important but is in fact misleading or useless.
A quadruple agent pretends to be a triple agent for the controlling organization, but actually works for the target organization. Mostly used in fiction and comedy, and rarely known in reality.
The idea of multiple levels of loyalty and deceit is parodied in the Tom Stoppard play The Dog it was that Died, where the agent Rupert Purvis is confused as to whether his ultimate loyalty lies with the British or the Soviets.