Kafes, literally "the cage", was the part of the Imperial Harem of the Ottoman Palace where possible successors to the throne were kept under a form of house-arrest and constant surveillance by the palace guards.
The early history of the Ottoman Empire is littered with succession wars between rival sons of the deceased sultan. It was common for a new sultan to have his brothers killed, including infants, sometimes dozens of them at once. This practice reduced the number of claimants to the throne, leading to several occasions where the Ottoman line seemed destined to end. The confinement of heirs provided security for an incumbent sultan and continuity of the dynasty.
When Ahmet I died in 1617, his eldest son was only 13 years old, and for the first time in 14 generations, the Imperial Council altered succession so that the late sultan's brother acceded to the throne as Mustafa I, aged 25 years. He was deposed (for the first time) the following year and became the first inmate of the Kafes although he and other princes throughout the preceding generations had been sequestered in various other places of comfortable confinement.
The next time there was a succession choice between a son or younger brother was in 1687 and the brother was again preferred. Thereafter, the "rule of elderness" was adopted as the rule of succession in the House of Osmanli so that all males within an older generation were exhausted before the succession of the eldest male in the next generation. This rule has also been largely adopted by other Islamic kingdoms.
It became common to confine brothers, cousins and nephews to the Cage, generally not later than when they left the harem (women's quarters) at puberty. This also marked the end of their education and many sultans came to the throne ill-prepared to be rulers, without any experience of government or affairs outside the Cage. There they had only the company of servants and the women of their harems, occasionally with deposed sultans.
The degree of confinement varied from reign to reign. Abdülaziz (1861–76) confined his nephews to the Cage when he succeeded his half-brother Abdülmecid I (their father) on the throne, but allowed them some freedom. He took his two eldest nephews with him when he traveled to Europe in 1867. At different times it was the policy to ensure that inmates of the Cage only took barren concubines. Consequently, some sultans did not produce sons until they acceded to the throne. These sons, by virtue of their youth at the time of their fathers' deaths, ensured that the rule of elderness became entrenched so that it sometimes happened that the son of a sultan was confined during the reigns of cousins and older brothers before acceding to his father's throne.