Border security topped the agenda for Imran Khan’s visit to Tehran as he met President Rouhani and the supreme leader.

Islamabad, Pakistan – Pakistan and Iran will establish a joint border security force to respond to an uptick in violence along the countries’ shared border, the Iranian president has announced, as Pakistan’s prime minister concluded a two-day visit to the Iranian capital.

Last week, at least 14 Pakistani security forces personnel were killed in a bus ambush that the Pakistani government blamed on Iran-based armed groups.

The incident came after a February attack in eastern Iran that killed at least 27 Iranian security personnel that Iranian authorities said was carried out by the Pakistan-based Jaish al-Adl group.

Border security was top of the agenda for Pakistani Prime Minister Imran Khan’s visit to Tehran, where he met Iranian President Hassan Rouhani and Supreme Leader Ali Khamenei.

“Both countries [have] agreed to set up a joint rapid reaction force that will be operating on the borders of the two countries to fight terrorism,” said Rouhani at a joint press conference following the talks.

Khan pledged that his government would not allow Pakistani soil to be used by armed groups against any country.

“We will not allow any militant groups to operate from our soil,” he said. “This government for the first time in Pakistan’s [history] is dismantling any militant group in our country”.

Khan said he made the trip to Pakistan’s southwestern neighbour because he felt that increasing cross-border security incidents could drive a wedge between the two countries.

“I felt the issue of terrorism was going to increase differences between our countries, so it was very important for me to come here and come with our security chief, so that we resolve this issue.”

‘Border region easy for groups to be active’

On Thursday, Pakistan’s foreign office issued a strongly worded letter to Tehran accusing Iran of “inaction” against ethnic Baloch separatist groups allegedly based in Iran. An alliance of those groups, the Baloch Raaji Aajoi Sangar (BRAS), claimed responsibility for last week’s bus attack.

On Saturday, an organisation representing the families of Iranian victims of attacks issued a letter demanding that Khan ensure greater action is taken against armed groups operating in Pakistan.

“These actions have been continually going on for several years, and, unfortunately, no serious action has been taken on the part of Islamabad to prevent them,” read the letter from the Habilian Association, the Iranian Fars news agency reported.

In February, Major General Yahya Rahim Safavi, a senior Iranian security official, said Pakistan “must account” for attacks on Iranian security forces near the border, and accused Saudi Arabia, Iran’s regional rival and a key Pakistani ally, of supporting those groups.

Iranian authorities say the Jaish al-Adl and Jundullah armed groups have been operating from Pakistani soil with impunity, targeting Iranian security and government targets, mainly in Sistan-Balochistan province.

Analysts say Pakistani leaders would have worked hard during the two-day visit to establish cooperation with their Iranian counterparts on the issue.

“It cannot be in Pakistan’s interest, in any way, [for attacks to occur], even if Pakistan is very stupid,” said Zahid Hussain, an Islamabad-based security analyst. “This is not in any way helpful for anyone.”

Hussain said that Pakistan’s southwestern Balochistan province, which border’s Iran’s Sistan-Balochistan province, is a large, sparsely populated area that is difficult to police.

“It is a vast area and the control over the area is difficult, it makes it easy for groups to be active on both sides,” he said.

‘Delicate balance between Saudi and Iran’

Both countries also agreed to establish a barter committee to aid in a planned increase in trade between them, bypassing United States sanctions that punish direct financial transactions with most Iranian entities.

“We agreed to set up a barter committee so that the goods and needs of the two countries can be exchanged and a barter account is created,” said Rouhani, adding that plans were in the works to link the two countries new deepwater trade ports of Gwadar and Charbahar by rail.

Rouhani also said the two sides discussed the regional situation, where Iran has been locked in direct and indirect confrontations with regional rival Saudi Arabia for years.

“I am pleased that the prime minister has reiterated that his country will never take part in any military or war coalition and the two countries are determined to see peace and security prevail in the whole region,” he said.

Khan’s visit to Tehran comes months after Saudi Crown Prince Mohammed bin Salman (MBS) pledged $20bn in investments during a state visit to the Pakistani capital Islamabad.

Hussain said Pakistan had always walked a tightrope in its relations with Iran and Saudi Arabia, eager to maintain neutrality while staying close to both allies.

“Pakistan has maintained a delicate balance in relations between Saudi Arabia and Iran,” said the analyst. “That is one of the reasons why Imran Khan has gone, to keep this clear.

“Whether it can be achieved or not, that is to be seen. So far Pakistan has refused to be drawn into this Middle Eastern [fight].”

Asad Hashim is Al Jazeera’s digital correspondent in Pakistan. He tweets @AsadHashim.