VISITS by heads of government are rare in Kathmandu. So the four-hour stopover by China's prime minister, Wen Jiabao, on Saturday stirred much debate and was analysed minutely. It comes at a time when Nepal's relations with its two giant neighbours, India and China, are under more scrutiny than usual.

The plot's variations can be so subtle that it can be worth looking back over the slow history of foreign relations in the region. Kathmandu has received influences from both north and south since the first millennium AD, but its primary orientation has long been towards India. During the 17th and early 18th centuries the city's spectacular monuments were built on the proceeds of trade between India and China, trade which was to wither in later centuries.

The modern state of Nepal was formed by conquest in the mid-18th century. In 1775 the conqueror, the king Prithvi Narayan Shah, dictated a few pages of advice to his heirs from his deathbed. He described the country's situation as like that of “a yam between two boulders”. It had good reason to feel vulnerable. The British East India Company was gobbling up independent kingdoms on the Indian plains to the south.

Nepal has never taken its independence for granted. “Like a yam between two boulders” has been on the lips of Nepali commentators and politicians ever since King Pritvhi's time. Meanwhile every other Himalayan state has been consumed by the two giants—save for Bhutan, which is in most ways controlled by India.

In this context modern Nepal is often characterised as a “small” country, notwithstanding its medium-sized population of around 30m people. Nepal has traditionally used relations with China to balance India's often domineering influence.

For long periods during the past few centuries Tibet—immediately to the north—has been under some form of control from Beijing. In 1792, for instance, when Nepal invaded Tibet it provoked a powerful counterblow from China. William Kirkpatrick, the British officer sent to mediate, put it like this at the time:

“The court of Pekin [Beijing], resenting certain encroachments which had been made by the Government of Nepaul upon the rights of the Lama of Tibet, whom the Emperor of China had, for some time past, taken under his protection, or, in other words, had subjected to the Chinese yoke, came to the resolution of chastising the aggressor, or the Robber, as the Rajah of Nepaul was contemptuously styled in the Chinese dispatches.”

Chinese troops almost reached Kathmandu. And Chinese power in Tibet is no less of an issue today. Mr Wen's visit had been postponed since December, apparently due to Chinese concerns over protests among Kathmandu's large Tibetan community. In the event the visit was kept secret until the last moment, and hundreds of “Tibetan-looking” people were arrested as a precautionary measure. It proceeded to pass without incident.

Nepal co-operates closely with China over Tibet. The Tibetan community in Kathmandu is said to be infiltrated by Chinese intelligence and Nepali police frequently suppress protests against China. Anyway, many Nepalis tire of Tibetan activism. “We've got enough problems,” says one middle-class professional. It is often said that Tibetan exiles in Nepal are relatively prosperous, and should refrain from causing trouble with the neighbours.

The agreement signed during Mr Wen's visit includes $1.18 billion worth of budgetary aid to Nepal over three years, plus various other, smaller chunks of cash to support the security sector and the peace process. There was talk of “soft loans” and Chinese involvement in major infrastructure projects. Six cargo terminals will be built at road crossings along the border. There will be 30km cross-border grazing rights granted to pastoral communities. The joint statement proclaimed that relations between the two countries had reached “new heights”.

Nepal's prime minister, Baburam Bhattarai, is pursuing a still grander ambition. He thinks Nepal can find a better place in the emerging world order by reprising its historical role as “a vibrant bridge” between India and China. In contrast to its neighbours, economic development has so far eluded Nepal. Mr Bhattarai asked the Chinese to extend their railway network through Kathmandu and as far as Lumbini, close to the Indian border. He also wants to develop special economic zones with transport links to both countries.

It has long been understood that modern India feels threatened by Nepal's links with China. (For example, Nepal's road network has been partly constrained by Indian planning for a Chinese invasion, and there is concern in some quarters in India over the possible strategic value of a Chinese rail link.) It would have been with these sensitivities in mind that the Nepali press made much of Mr Wen's reported remarks to Mr Bhattarai: “We [China] and India have been developing very cordial relations in the recent times and it would be better and fruitful for Nepal to maintain good relations with India.”

At the height of their power in India the British questioned whether Nepal was to be regarded as fully independent. Since the British left in 1947 independent India has also intervened frequently in Nepal's affairs. Nepal lies south of the main Himalayan ridgeline and Indian officials see it, strategically and culturally, as falling within their sphere of influence. Nepali politicians have long invited Indian involvement by turning to the Indian government for support against their rivals. There are significant Nepali-speaking communities native to India and well over 1m Nepali citizens who work there. The two countries enjoy an open border. In 2009 a prime minister of Nepal, Pushpa Kamal Dahal, lost his job partly through insensitivity to India's strategic concerns.

Yet economic ties between India and China are growing rapidly. For Nepal, one of the poorest and most politically turbulent countries in Asia, reviving its ancient, formerly profitable role as a link between the two giants might offer economic growth and political stability. And growth and stability in the Himalaya would surely be a win-win-win. India's prime minister, Manmohan Singh, is expected in Kathmandu in the coming months—the first visit by an Indian premier since the 1990s.