Code as a Tool
The analyst agent.
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.

- 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.
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())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.textAgents 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.
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")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.
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