Vercel AI SDKAI SDK 7 · TypeScript · Node.js 20+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
17 small wins to finish your pathNext lesson

Agents with ToolLoopAgent

ToolLoopAgent packages a model, instructions, tools and loop settings once, so every call site uses the same agent with generate or stream.

Exampleagent.ts
import { ToolLoopAgent } from "ai";
import { shopModel } from "./shop-model.ts";
import { lookupOrder } from "./tools.ts";

export const deskAgent = new ToolLoopAgent({
  model: shopModel,
  instructions: "You answer support tickets for an online shop. Look orders up before answering.",
  tools: { lookupOrder },
});
Examplerun-agent.ts
import { deskAgent } from "./agent.ts";
import { shopModel } from "./shop-model.ts";

const result = await deskAgent.generate({ prompt: "Where is my order A-1002?" });
console.log(result.text, result.steps.length);

const stream = await deskAgent.stream({ prompt: "I was charged twice" });
for await (const piece of stream.textStream) process.stdout.write(piece);
console.log();

console.log(shopModel.doGenerateCalls[0].prompt[0]);
Example
npx tsx run-agent.ts

generate ran the tool loop and returned after two steps; stream took the same settings and streamed. An agent's loop allows 20 steps unless you set stopWhen. The last line shows instructions reached the model as the system message.

An agent is a configuration object, not a running process: it keeps no memory between calls. Define it in one module and import it into routes, jobs and tests.

Try it yourself
  • Add refundOrder from lesson 9 to the agent's tools.
  • Pass messages with two turns to generate.
  • Print result.totalUsage.

Every expert started right here.