The Duke of Cambridge has spent a “humbling” three weeks on work placements with three of Britain’s security and intelligence agencies.
MI5, MI6 and GCHQ were “full of people from everyday backgrounds doing the most extraordinary work to keep us safe”, Prince William said.
GCHQ’s head of counter-terrorism said the duke worked “exceptionally hard”.
The royal learned about risks to the UK’s national security and economy, Kensington Palace said.
He also observed counter-terrorism teams analysing intelligence and carrying out investigations.
The prince’s attachments came to an end on Saturday.
“Spending time inside our security and intelligence agencies, understanding more about the vital contribution they make to our national security, was a truly humbling experience,” he said.
Staff at the security and intelligence agencies “work in secret, often not even able to tell their family and friends about the work they do or the stresses they face”, he continued.
He added: “We all owe them deep gratitude for the difficult and dangerous work they do.”
Prince William’s attachment comes after the Queen celebrated GCHQ’s centenary earlier this year with a visit to its former top secret base, Watergate House in London.
The head of counter-terrorism operations at GCHQ, who is anonymous, said in a statement: “William worked exceptionally hard to embed himself in the team and comfortably held his own amongst some highly skilled analysts and operators.
“His Royal Highness asked some probing questions and demonstrated a real grasp of our mission.”
The threat to the UK from international terrorism is currently classed as severe, which means a terror attack is “highly likely”.
The head of MI6 warned in February the Islamic State group was preparing for more attacks, despite its military defeat in Syria.
And in January, the former head of MI5, Baroness Manningham-Buller, warned leaving the European Union without a deal would make the UK “less safe”.