AI Hints
Home›Agentic AI Tutorial›Agents That Build
Agentic AI · Lesson 66 of 100

Code as a Tool

The analyst agent.

⏱ 2 min read▶ Video at 1:57:27🎓 Free
Watch this lesson in the Agentic AI courseWatch on YouTube ↗Full Agentic AI course

The founder asks, which product makes the most profit? You could give the model every possible tool. Or you could give it one, and let it write code to answer anything. That's how the analyst modes in modern assistants work.

Code as a Tool
From the video: Code as a Tool
  • Which product makes the most?
  • Or one tool that writes code
  • That's how analyst modes work

An agent that writes analysis

We've got a small sales file. The model never sees the data. It writes a program, and we run that program. Run it. There's the answer, worked out by real code, not by the model's memory. Exact numbers, every time. And in production, this step runs inside the sandbox that you built.

analyst.pypython
import subprocess, sys
import pandas as pd
from llm import ask

data = pd.DataFrame({"item": ["Chain", "Helmet", "Wheelset", "Chain", "Helmet"],
                     "profit": [12, 30, 210, 14, 26]})
data.to_csv("shop.csv", index=False)

code = ask("Write Python with pandas that reads shop.csv and prints the item "
           "with the highest total profit, and that total. Return only code, no fences.")
code = code.replace("```python", "").replace("```", "")

r = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True, timeout=20)
print("Program output:", r.stdout.strip())
Output
Program output: Wheelset 210
Also uses llm.py
from google import genai
from google.genai import types

client = genai.Client()

def ask(prompt, system=None, schema=None):
    config = types.GenerateContentConfig(temperature=0.2, system_instruction=system)
    if schema:
        config.response_mime_type = "application/json"
        config.response_schema = schema
    r = client.models.generate_content(model="gemini-flash-latest",
                                       config=config, contents=prompt)
    return r.parsed if schema else r.text

Agents that use websites

Agents can also use websites. A raw page is huge, and full of junk. So browser agents convert it into a short list of things that you can click, each with a number. The model then says, click three. Run it. On a real page, this turns megabytes of code into a few hundred tokens. Most browser agents work this way, sometimes with screenshots too. And remember, every web page is untrusted content, so the lethal trifecta applies in full.

browse.pypython
import re

PAGE = """<h1>Velo Parts</h1>
<a id="1" href="/chains">Chains</a>
<a id="2" href="/helmets">Helmets</a>
<button id="3">Add to cart</button>
<script>track()</script>"""

def snapshot(html):
    items = re.findall(r'<(a|button) id="(\d+)"[^>]*>(.*?)</', html)
    return [f"[{i}] {kind}: {text}" for kind, i, text in items]

print(f"Raw page: {len(PAGE)} characters")
print(*snapshot(PAGE), sep="\n")
Output
Raw page: 154 characters
[1] a: Chains
[2] a: Helmets
[3] button: Add to cart

Using computers like people

Computer use agents go further, by looking at screenshots, and by clicking and typing like a person. That works with any software, even old systems with no API. But they're slower and less reliable than a real API, so treat them as a last resort. Keep them away from sensitive accounts, and watch them closely.

Screenshots and clickslike a person
Works with anythingeven legacy software
Slowerand less reliable
Prefer APIsuse this last

Summary

Code as a tool gives exact answers, and page snapshots let agents use the web. You've built many kinds of agent. But how do you know if any of them really works? Next, evals.

Key takeaways
  • Models write code, code computes
  • Pages become short lists
  • APIs beat screen clicking
Next up: Why Evals Matter — you can't improve what you can't measure