Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Closing the Agent Loop

00:00 Now, there’s one last thing we need to do. Let’s try it again. Summarize the content of all the Python files in the habits folder. So now I’m asking it to list all the files.

00:16 And it did. It said, okay, please run list_files. Good. There. And then the model came back saying, please run read_file. However, we stopped there.

00:28 Notice that the harness didn’t give me any information. Why? Because, and this is where we need our last step in the agent loop. Let’s go back to our harness, the while loop.

00:39 And you may recall that we said here, well, we can have many tool calls coming from one message. But what if the reply from one message is another tool call?

00:52 Well, all we need to do is change this to a while. So it was an if. Now we want to say while. Why? Because keep going.

01:01 If the response from one tool call that goes back to the model is, oh, now I’ve realized I need another tool call. That’s exactly what happened here, right?

01:09 First, there was a tool call. Run list_files. Once it got the response from that, which is the files, it said, okay, now I need another tool call. read_file.

01:18 That was an if. By having it as a while, this should go again. Famous last words. Let’s find out. I might copy and paste exactly the same, not just to be lazy, but just to make sure we’re doing same for same.

01:31 Summarize the content of all the Python files in the habits folder. And now you can see that we’ve gone into an agent loop. list_files.

01:39 So the model says, please run list_files. And the harness calls the tool. The tool gives an answer. The harness sends it back to the model. The model says, okay, I now know what the files are.

01:48 Therefore, I need to run the .py file. If there were more .py files in this folder, the model would have said, actually, I need to call this tool multiple times.

01:58 Once for habits.py and once for something else .py. But in this case, there’s only one .py file there. The tool gives it the content of that file.

02:07 The harness reads the whole conversation, including the response, and says, ah, good. There you go. I’ve got a text response for you. This is what an agent is.

02:17 The harness now uses while reply.tool_calls. So it keeps running tools for as long as the model asks for them. The model decides when it has enough information.

02:28 And only then does it send back a text reply. That loop is the agentic loop. And it’s what turns a model into an agent. In the summary, you look back at what you’ve built and at what real agents add on top.

Become a Member to join the conversation.