Start learning free

LESSON 93 OF 129 · AI ENGINEERING: BUILD YOUR OWN CODING AGENT

A Coding Agent Starts: Read the Project

Practice

A coding agent is a model with tools for looking at a project, plus a loop in your script. The model asks for a tool, and your script runs it and sends back the result. Then the model asks again, until it knows enough to answer. Here it gets two tools: list_files, which lists the files in the workspace folder, and read_file, which returns the text of one of them.

The model names a tool with a string, like "read_file". To run it, your script keeps a dictionary that maps each name to its function. You store the function itself, without parentheses:

def greet(tool_input):
    return "Hello, " + tool_input["name"]

functions = {}
functions["greet"] = greet
print(functions["greet"]({"name": "Ana"}))   # Hello, Ana

The loop runs while the reply's stop_reason is "tool_use", which means the model asked for a tool and is waiting for its result. Run the tools first, then ask again. If you ask again without running them, the model sees the same messages and asks for the same tool forever.

Asked "What does this project do?", your program must print:

Model: I'll start by looking at which files the project has.
Tool: list_files {}
Model: cart.py looks like the main file. I'll read it.
Tool: read_file {'path': 'cart.py'}
Answer: This is the cart of the Corner Shop. cart.py works in cents: line_total and cart_total add up the items, shipping adds $5 unless the order is big enough to ship free (the limit is $50), and order_total puts the two together. test_cart.py holds the tests.

The check then shows that the model was called 3 times. It also shows that the model saw the file list (README.md, cart.py, test_cart.py) and the 24 lines of cart.py.

Your task: the tool descriptions, run_tool and most of run_tools are given. Write list_files and read_file, and add both to functions. Print each tool call, and turn the single call to the model into a loop that runs the tools and asks again.


Assignment
Follow the Coding Tutorial to write the two tools and the loop that lets the agent read the project.


Hint
In functions, store each function itself, without parentheses. Inside the while, call run_tools(reply) before you ask the model again.


STEP 1 OF 12
    Ctrl + Enter to check