Gavin Barwell was Theresa May's chief of staff between 2017 and 2019. He was the MP for Croydon Central between 2010 and 2017 and served May as secretary of state for housing. He was made a life peer last year. This is a transcript of a speech he gave to the Institute for Government last night.

I became chief of staff to the prime minister in the immediate aftermath of the 2017 election. I had lost my seat, the result was declared in the early hours of Friday morning. I went back to bed because I'd been up for about 30 hours at that point. And then I did a media interview on the Saturday morning, talking about why I thought the election had gone wrong. As I came out of BBC New Broadcasting House, the No. 10 switchboard called me and said the prime minister wanted to talk to me. I assumed she was phoning around offering her commiserations to the 30 odd people who'd lost their seats. And indeed, that is how she started - and then she caught me completely by surprise by saying would I like to come and be her chief of staff. I obviously said yes.

But I quickly discovered there wasn't a sort of set job description. So I thought I might perform a little bit of a public service this evening by setting out what I think the role involves. Although I will start with a caveat, which is that it turns out that the reason that there is no set job description is it varies a lot depending on the particular circumstances.

I think there are probably four things [that affect how the job should be done]. First of all, the prime minister you're working for and what they want you to do. Secondly, the character of the principal private secretary and the cabinet secretary and how these three roles interact. Thirdly, the political situation. In my case, clearly we had a government that didn't have a majority was in a confidence and supply arrangement with the DUP. Even within the Conservative party, we were fundamentally divided on the key issue that the government was trying to achieve. So it was challenging at times.

I was the first person to do this job who'd been an MP and been a minister and I think that probably had some impact on the way I did it because it certainly meant that MPs on all sides knew me much more than was probably the case for previous chiefs of staff. But having said all that, I will outline for you seven elements to the job.

First of all, clearly you're the prime minister's most senior political adviser. You see nearly everything that comes to the prime minister. And you can attend nearly every meeting the prime minister has. There is a bit of the job which is just about telling truth to power. I can recall a few occasions where the prime minister would be in her office thinking about something and a whole array of people would say, you know, we really should tell the prime minister not to do that or she ought to do this. And everyone would look at me and say, 'Well, you go and tell her then'. Because ultimately, actually, it is very valuable to prime ministers that there is someone who can go into their room and say, 'Look, Prime Minister, no one wants to say this to you because they know you're not going to like it but this is the unanimous view of your officials and political advisers.'

The second role is to manage the other political advisers within No. 10 and, to a degree, the political advisors around the rest of government. Clearly, if you're a special adviser to the Chancellor, your directly reporting to the Chancellor of the Exchequer. But ultimately the prime minister appoints all political advisers. So you want to try and create a team culture amongst them.

The third job, and I would underline this very strongly, is to work really closely with the prime minister's principal private secretary, who's the most senior civil servant in No. 10. So, Bernard, for those of you who draw your experience based on the TV programme [Yes, Minister]. Together, your job really is to set the tone within the building. For whatever reason, I inherited a situation where - from the conversations I had with people over the first few days - it had clearly not been a particularly happy environment in the previous few months for whatever reason. Clearly, the election result made that even more difficult. So I thought a key part of the job was to try and set the tone. No. 10 is always going to be an incredibly stressful place to work. But it should be an enjoyable place to work, despite that stress, and getting the atmosphere right is clearly important.

The fourth role is a sort of gatekeeper role. You physically sit right outside the prime minister's office. So as long as you occupy your space for all waking hours of the day, no one can get in to see the prime minister without going past you. But you're not just physically a gatekeeper. A lot of people in the civil service and a lot of politicians can't easily get to the prime minister, but they can definitely get to you. So they can come to you and say, 'I've got this cunning plan to solve X problem. Can you tell me that the prime minister will be okay if I go ahead and just do this now?' So you need to know the prime minister's mind.

The fifth job is to work with all of the other officials in private offices, all of the other private secretaries and the excellent SpAd and official team in the Policy Unit to try and get this enormous government machine to do what the prime minister wants to do. Certainly, in my time, quite a big chunk of my job was about brokering deals with other ministers. Clearly, the prime minister had been in a position of huge political power within the government prior to the general election and wasn't in such a strong position after the general election. Therefore, quite often you'd have to have some kind of trade-off. The prime minister wanted this, what could we do in return for that minister that would get her there? You can't, in doing the job, do that across all government departments. There are clearly areas that you prioritise.

The sixth job is to be a link with the opposition, and actually that can be quite rewarding on certain occasions, particularly I found when you had national security issues. So when the Prime Minister authorised the use of military force against the Assad regime over the use of chemical weapons, when we were working out the response to the attempted murder of the Skripals by the Russian state, ensuring the opposition leaders have Privy Council briefings, were aware of some of the underlying information that the government had at its disposal, tended to give a much better response in the House when the prime minister was then coming forward and taking action.

And the last job was to be like a human Swiss Army knife - just a little bit of everything. So I certainly did a little bit of speechwriting. I would sometimes help James Slack out who was the prime minister's official spokesmen with briefing on some of the complexity of Brexit. You're occasionally used as a link with other chiefs of staff for other governments that you're trying to negotiate with. I think it was partly a result of my greatest failing, which is I don't delegate enough. But nonetheless, you'll occasionally turn your hand to whatever is the most important issue of the day.

I just want to make two other sets of points. I thought it'd say a little bit about what I found difficult about the job. The first thing is it is completely life-consuming.

The hours you do in the office are bad enough, but you never, ever get away from work because if there's a terrorist attack or if some other major event happens, you're going to get called just before the prime minister gets called. Ministers, even very senior ministers, will not generally want to pester the prime minister all weekend. But that doesn't stop them doing it to you.

My wife, who's in the audience tonight, will attest that I was generally home at weekends, but I wasn't present. Because I was almost constantly on telephone conference calls or reading speeches or dealing with individual calls. My phone has this great feature in my contacts where it tells you the five people in your contacts that contact you the most. And my wife used to monitor that and Julian Smith and Philip Hammond were often ahead of her, which was an issue.

The third thing, which I think is sort of a reflection, again, of my personality, is the contrast between being chief of staff and being housing minister in terms of dealing with 101 issues on any one given day. My natural inclination and preference is to study something in detail, understand it properly, take some time before reaching conclusions about what to do. You can't do that. You have to trust the people in the Policy Unit and the private secretaries that are providing you with very succinct advice about what the situation is and what the options are and just making quick decisions. Otherwise the whole government is going to ground to a halt. Again, I went from a situation where for seven years I'd been an MP and for four years I'd been a minister and I therefore had my own political voice and, if you like, was an actor on the political stage. Then you go to this job where actually you're much closer to the centre of power and what's actually happening, but you don't have a voice. That was something that I found difficult.

The fourth thing is obvious, which is that Brexit over the two and a bit years increasingly dominated everything and it got worse the longer that period went on for. That was hugely frustrating for everybody involved because there were so many other things that you would want to devote more of your time to.

The Fifth, and I think this is probably challenging for any No. 10, is how do you stop groupthink? You're in an office, and it's actually quite a small building, where there's a small group of people who are spending a very large amount of time together. How do you ensure that you have a diversity of voices among that group of people and you don't end up all thinking the same thing? And if I would recall one particular incident that I think illustrates that problem very well: there was an occasion when the prime minister, during the Brexit process, went out and gave a statement which vented her frustration with MPs about their failure to take any positive decision about what they wanted. And I still to this day think she was right in what she said. But it wasn't the right thing to do politically. It didn't help her cause of trying to get parliament to come to a conclusion because, you know, MPs were having a hard enough time from their constituents about what was happening and the prime minister saying it made them feel angry rather than encouraging them to resolve the matter. So that group think danger, I think, is always there.

Then a couple of reflections on the civil service. I found the quality of the civil servants that I dealt with at No. 10 exceptional. I'm not just saying that because as a few of them in the room tonight. The logistics they're there to support the prime minister, and therefore by proxy to support you, are Rolls Royce standard.

There was far too much focus on policymaking and not enough on implementation and whether actually either the things the prime minister had said wanted to happen was actually happening or if they were happening, were they having the desired effect that had been behind the thinking behind that policy? I don't think that's just the fault of the civil service. I think that is partly the pressure of politicians always wanting new announcements and the sort of media cycle that we have in this country always the need to generate news and new stuff rather than actually focus on what's working.

The two frustrations I would put down to the civil service. I think departmentalisation is a huge problem. And I talked about this a little bit in the 'ministers reflect' about housing. In terms of, you know, MHCLG doesn't have all of the housing policy levers within its area. It's actually very difficult to get all the people in one room to decide something. And there's too rapid a turnover of senior policy officials.

Four very quick final thoughts. An innovation that I had at least something to do with that I'm really proud of is the Legislative Affairs team at No. 10. I think it will obviously be less necessary for this government with the size of the majority it has. But there wasn't sufficient expertise in No. 10 in a hung parliament about actually how parliament works. I think the team that Nikki da Costa put together there was a huge success and I hope it's an innovation that will stick.

My experience was SpAds don't need the power to direct civil servants. If officials believe that (1) you know the prime minister's mind and (2) you are accurately representing the prime minister's view, not what you want to happen, then if you say, 'This is what the prime minister wants,' they will go ahead and do it, whether you have the power to direct them or not.

Third thought, this might be something that some people would criticise me over, having been a SpAd in a department years ago, I actually found this role less political because there were lots of SpAds in No. 10, there were lots of whose people whose job it was to be the political person. Actually, I thought my job most of the time was to make sure the prime minister was getting the right advice from politicians and officials and the whole process was well-run and there was good input from across the party.

Then my final thought comes from a book that I would encourage those of you who are interested. Given you've turned up here on a Monday night, you're all fairly interested, I guess. There is a book called The Gatekeepers by Chris Whipple, which is a history of the office of chief of staff to the president of the United States. He has one chapter on each person who occupied the office. And I read it in the first few weeks doing the job.

There's one thing that really stuck with me, which was I think was one of Obama's chief of staff said, 'Chief of staff. The important word in the job title is "staff", not "chief".' It is very easy doing this job to believe that you are a really important person because the civil service treat you incredibly well and you get this whole queue of people coming to you for decisions. But you are there to get what the prime minister wants done, done. You are not there as a political actor in your own right, and bearing that in mind will serve you in good stead.