Step 3 · Close the loop
With file tools alone, the agent edits code and hopes. Give it a way to run commands and it can check its own work: run the tests, read the failures, fix, run them again. Add import subprocess at the top and one more tool:
def run_command(command: str) -> dict:
"""Runs a shell command in the project folder and returns its output.
Use it to run code and tests, e.g. "python -m pytest".
Args:
command: The shell command to run.
"""
try:
result = subprocess.run(
command,
shell=True,
cwd=WORKSPACE,
capture_output=True,
text=True,
timeout=30, # a hung command must not freeze the agent
)
except subprocess.TimeoutExpired:
return {"error": "Command timed out after 30 seconds."}
return {
"exit_code": result.returncode,
# Keep the end of long output: that's where errors and test summaries are.
"stdout": result.stdout[-4000:],
"stderr": result.stderr[-4000:],
}Then tell the agent to use it. One sentence in the instruction turns edit-and-hope into edit-and-check:
agents/step3_run/agent.pyroot_agent = Agent(
name="coding_agent",
model="gemini-3.8-flash",
instruction=(
"You are a coding agent working on a small Python project. "
"Look before you answer: list the files, then read the ones that matter. "
"When you change a file, write its complete new contents. "
"After every change, run the tests with `python -m pytest` and keep "
"fixing until they all pass. "
"Finish with a short summary of what you changed and the test result."
),
tools=[list_files, read_file, write_file, run_command],
)Put the buggy project back, then ask it to fix the tests:
uv run reset_workspace.py
The tests are failing. Fix them.A real run on gemini-3.8-flash while preparing this workshop:
list_files()run_command("python -m pytest"): 3 failed, 1 passedread_file("cart.py"), thenread_file("test_cart.py")write_file("cart.py", …)with both bugs fixedrun_command("python -m pytest"): 4 passedAbout 25 seconds from start to finish.
You didn't write a loop. ADK's runner keeps calling the model until it answers with text instead of asking for another tool. Try taking the "run the tests" sentence out of the instruction and compare.
The idea: an agent is a loop of act, observe, decide. ADK runs the loop; you give it ways to act and to observe.