∛ Cube Root Money must move once, even when machines fail.
ContactTry the IDE in your browser →
← Articles

An AI agent is an ordinary while loop

Ask the model; while it asks for tools, run them all at once and ask again. Durable across crashes, with a person where it matters.

Cube Root · · 3 min read

Strip an AI agent down to what it does and it is a loop. Send the model the conversation so far. If it answers with tool calls, run the tools, add their results to the conversation, and ask again. When it answers without asking for a tool, that answer is the result.

Agent frameworks wrap this loop in their own concepts: graphs, nodes, state objects, checkpointers. Durable execution engines can host it too, with the loop written against their SDK. In Cube Root it is written the way it reads.

The flow

This agent judges the fraud risk of a money transfer, with three tools it may call: the payee's history, the customer's usual amounts, and the device the transfer came from.

public class RiskAgentFlow {
    private Flow<String, Reply> model;
    private Flow<Call, String> payeeHistory;
    private Flow<Call, String> usualAmounts;
    private Flow<Call, String> device;

    public RiskVerdict invoke(Transfer t) {
        String transcript = Prompt.of(t);
        Reply reply = model.invoke(transcript);
        while (reply.calls().length > 0) {
            int n = reply.calls().length;
            String[] out = new String[n];
            parallel: for (int i = 0; i < n; i++) {
                Call call = reply.calls()[i];
                String r = "";
                switch (call.name()) {
                    case "get_payee_history" -> { r = payeeHistory.invoke(call); }
                    case "get_usual_amounts" -> { r = usualAmounts.invoke(call); }
                    case "get_device" -> { r = device.invoke(call); }
                    default -> { r = "unknown tool " + call.name(); }
                }
                out[i] = r;
            }
            transcript = transcript + "\n" + String.join("\n", out);
            reply = model.invoke(transcript);
        }
        return reply.verdict();
    }
}

What each part does

In the code What happens
model.invoke(transcript) a call to the model; which model (Claude, Gemini, ChatGPT) is chosen when the flow is deployed
while (reply.calls().length > 0) the agent's loop: as long as the model asks for tools
parallel: for ... the tools the model asked for, run at the same time
switch (call.name()) each call goes to its tool, a typed service
transcript = ... the conversation grows with the tools' results
return reply.verdict() the model's final answer, read as a verdict

One tool or three, the model's choice: the same code.

When the machine dies mid-conversation

Model calls are slow and cost money, and an agent may make many of them. If the machine running it dies after the third model call, starting the conversation over means paying for three calls again and, worse, maybe getting different answers.

Every call here is a step, and every finished step is a row in PostgreSQL. The transcript is kept like any other value of the flow. After a crash, the work continues from the rows: the three model calls already answered are not made again, and the conversation resumes where it stopped.

The tools are services

Each tool is a typed service, Flow<Call, String>, the same kind of thing as a payment rail or a database lookup. That has two consequences. The tools are durable steps like everything else, retried when they fail and recorded when they finish. And the agent fits into a larger flow the same way any service does.

A flow calls an agent the way it calls any other service. In a bank's transfer, it is one call among others:

RiskVerdict risk = riskAgent.invoke(t);
ScreeningVerdict screening = sanctions.invoke(t);
String decision = "approved";
if (risk.level().equals("high") || screening.result().equals("hit")) {
    decision = review.invoke(t);
}

The agent and the sanctions screening run side by side, because neither reads the other. A high risk waits for a person, for minutes or for days, with no thread waiting. Then the money moves, in a try that undoes the debit if the rail refuses.

Dynamic where it helps, checked where it matters

The agent decides which tools to call and how many times to loop. That freedom is the point of an agent. What it does with that freedom, the calls it makes and the money it moves, runs as durable, recorded, undoable steps, checked by the compiler before anything runs.

Let the agent be as dynamic as it likes. Make what it does trustworthy.

Try it

The risk agent and the transfer around it are samples in the browser IDE at cube-root.com.

Next in this series: a version never changes.


cube-root.com · Articles · Contact