NAIROBI, Kenya — The Kenyan government revealed on Wednesday that its extensive military foray into Somalia this month to battle Islamist militants was not simply a response to a wave of recent kidnappings, as previously claimed, but was actually planned far in advance, part of a covert strategy to penetrate Somalia and keep the violence in one of Africa’s most anarchic countries from spilling into one of Africa’s most stable.

For several years, the American-backed Kenyan military has been secretly arming and training clan-based militias inside Somalia to safeguard Kenya’s borders and economic interests, especially a huge port to be built just 60 miles south of Somalia.

But now many diplomats, analysts and Kenyans fear that the country, by essentially invading southern Somalia, has bitten off far more than it can chew, opening itself up to terrorist reprisals and impeding the stressed relief efforts to save hundreds of thousands of starving Somalis.

Somalia has been a thorn in Kenya’s side ever since Kenya became independent in 1963, and the two countries have followed wildly different paths. Somalia has become synonymous with famine, war and anarchy, while Kenya has become one of America’s closest African allies, a bastion of stability and a favorite of tourists worldwide.