Prince William spent three weeks secretly shadowing Britain’s top spies.

The Duke of Cambridge spent his placements with agents from MI5, MI6 and GCHQ to get a better understanding of how the UK’s security and intelligence agencies work.

William spent his first week with the Secret Intelligence Service, also known as MI6, which deals with foreign intelligence and protects the UK from risks abroad.

He learned about the risks to the UK’s national security, military effectiveness and economy, Kensington Palace said.

He then shadowed the Security Service (MI5) for a week, where he saw counter-terrorism teams analysing intelligence and conducting investigations on UK soil.

Prince William becomes Duke of Cambridge Show all 22 1 /22 Prince William becomes Duke of Cambridge Prince William becomes Duke of Cambridge Britain's Prince William kisses his wife Kate, Duchess of Cambridge AP Prince William becomes Duke of Cambridge Kate Middleton and Britain's Prince William kneel at the altar AP Prince William becomes Duke of Cambridge Prince William kisses his wife Kate, Duchess of Cambridge on the balcony of Buckingham Palace after the Royal Wedding AP Prince William becomes Duke of Cambridge A man dressed as a town crier waits with other royal enthusiasts outside Buckingham Palace ahead of the Royal Wedding Rui Vieira - WPA Pool/Getty Images Prince William becomes Duke of Cambridge Leader of the Labour Party Ed Miliband arrives to attend the Royal Wedding of Prince William to Catherine Middleton at Westminster Abbey Getty Images Prince William becomes Duke of Cambridge Prime Minister David Cameron waves as he arrives to attend the Royal Wedding wearing a morning coat Getty Images Prince William becomes Duke of Cambridge Samantha Cameron, wife of Prime Minister David Cameron, arrives at the Royal Wedding wearing blue Getty Images Prince William becomes Duke of Cambridge Charles Spencer, 9th Earl of Spencer, brother of the late Princess Diana, right, arrives at Westminster Abbey for the Royal Wedding AP Prince William becomes Duke of Cambridge Spectators dressed as brides pose for picture on the grass at Hyde Park Corner where big screens have been setup to watch the wedding ceremony on April 29, 2011 in London, England. Getty Images Prince William becomes Duke of Cambridge Royal fans stand on crates to get a better view on Parliament Square before the wedding of Britain's Prince William and Kate Middleton in central London April 29, 2011. Getty Images Prince William becomes Duke of Cambridge Britain's Deputy Prime Minister Nick Clegg and his wife Miriam, left, arrive at Westminster Abbey AP Prince William becomes Duke of Cambridge Former British Prime Minister John Major and Norma Major arrive to attend the Royal Wedding of Prince William to Catherine Middleton Getty Images Prince William becomes Duke of Cambridge Elton John and David Furnish arrive to attend the Royal Wedding of Prince William to Catherine Middleton Getty Images Prince William becomes Duke of Cambridge The Mayor of London Boris Johnson and Marina Johnson (R) arrive to attend the Royal Wedding of Prince William to Catherine Middleton at Westminster Abbey. Getty Images Prince William becomes Duke of Cambridge House of Commons Speaker John Bercow and his wife Sally Bercow arrive to attend the Royal Wedding of Prince William to Catherine Middleton. Getty Images Prince William becomes Duke of Cambridge Earl Charles Spencer arrives to attend the Royal Wedding of Prince William to Catherine Middleton. Getty Images Prince William becomes Duke of Cambridge A military band perform as they march on the Mall, in central London, on the day of the royal wedding of Britain's Prince William and Kate Middleton. Getty Images Prince William becomes Duke of Cambridge David and Victoria Beckham have a quiet moment soon after arriving at Westminster Abbey for the Royal Wedding. Getty Images Prince William becomes Duke of Cambridge David and Victoria Beckham arrive to attend the Royal Wedding of Prince William to Catherine Middleton. Getty Images Prince William becomes Duke of Cambridge The Right Reverend Dr John Hall, Dean of Westminster greets the crowd before the Royal Wedding of Prince William to Catherine Middleton. Getty Images Prince William becomes Duke of Cambridge Megan Walker from Lancashire, waits for the royal couple along The Mall ahead of the Royal Wedding of Prince William to Catherine Middleton at Westminster Abbey. Getty Images Prince William becomes Duke of Cambridge Spectators wait for the arrival of Prince William and Catherine Middleton along the procession route near Westminster Abbey for the Royal Wedding. Getty Images

Finally, he worked at GCHQ, the Government's listening centre in Cheltenham, Gloucestershire, which monitors communications to look for potential security threats to the UK.

After finishing his placements on Saturday, the Duke said: “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.

Support free-thinking journalism and attend Independent events

“These agencies are full of people from everyday backgrounds doing the most extraordinary work to keep us safe.

“They work in secret, often not even able to tell their family and friends about the work they do or the stresses they face.

“They are driven by an unrivalled patriotism and dedication to upholding the values of this country.

“We all owe them deep gratitude for the difficult and dangerous work they do.”

The GCHQ head of counter-terrorism operations, known only as David, said: “Having the Duke of Cambridge spend time with our teams was an incredible opportunity.

“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.