GCHQ director Robert Hannigan has announced that he will step down from the spy agency after more than two years in the post.

In a letter to the Foreign Secretary, Mr Hannigan said he would remain director of the surveillance agency until a successor was found.

Without giving an explicit reason for standing down, he appeared to cite pressures on his personal life, saying the job "demanded a great deal of my ever-patient and understanding family, and now is the right time for a change in direction".

GCHQ in Cheltenham (Getty)

The memo to Boris Johnson read: "I have had the great privilege of leading the men and women of GCHQ since 2014. I am proud of what we have achieved in those years, not least setting up the National Cyber Security Centre and building greater public understanding of our intelligence work.

"I am equally proud of the relentless 24-hour operational effort against terrorism, crime and many other national-security threats.

"While this work must remain secret, you will know how many lives have been saved in this country and overseas by the work of GCHQ."

The 24-hour Operations Room inside GCHQ, Cheltenham (Getty)

He said he had overseen the "greatest internal change within GCHQ for 30 years", and said the agency was now "well on the way to being fit for the next generation of security challenges to the UK in the digital age".

The letter continued: "After a good deal of thought I have decided that this is the right time to move on and to allow someone else to lead GCHQ through its next phase.

"I am, like you, a great enthusiast for our history and I think it is right that a new director should be firmly embedded by our centenary in 2019.

"I am very committed to GCHQ’s future and will of course be happy to stay in post until you have been able to appoint a successor.

"I have been lucky enough to have some extraordinary roles in public service over the last 20 years, from Northern Ireland to Number 10, the Cabinet Office and the Foreign Office. But they have all demanded a great deal of my ever-patient and understanding family, and now is the right time for a change in direction."

Mr Johnson responded: “Following your successful tenure, and thanks to the work of thousands of excellent GCHQ staff, the organisation is well placed to play its part continuing to protect our nation.”

GCHQ said in a statement: “In line with normal practice there will be an internal competition within Government to identify candidates to succeed Mr Hannigan, for onward recommendation to the Foreign Secretary and the Prime Minister.

“In the meantime the director and board will continue to oversee all the department’s work.”

First footage from inside GCHQ

Last April, Mr Hannigan apologised for the espionage service's "horrifying" treatment of Alan Turing and historic prejudice against LGBT people.

Turing, the pioneering mathematician, whose code-breaking skills are said to have shortened World War Two by two to four years, lost his job with the secret service following a conviction for indecency and was forced to undergo chemical castration.

Following demands for an apology, Mr Hannigan said: "I am happy to do so today and to say how sorry I am that he and so many others were treated in this way, right up until the 1990s when the policy was rightly changed.

"The fact that it was common practice for decades reflected the intolerance of the times and the pressures of the Cold War, but it does not make it any less wrong and we should apologise for it.”

A year earlier he was caught up in an incident with Downing Street after a hoax caller was put through to David Cameron's phone after posing as the director of GCHQ.

A call to Britain's eavesdropping agency, during which a mobile phone number for Mr Hannigan was disclosed to the caller, was followed by a hoax call to Downing Street, during which the caller was connected to Mr Cameron.

The then-Prime Minister ended the call when it became clear it was a hoax and no sensitive information was disclosed, Downing Street said at the time.