Generators and tools
There is no agent() in Flow State. A generator is the block that calls a model, and a block is the framework's unit of work: a name, a typed input, a typed output and the work between. Give a generator tools, which are other blocks it may call by name, and it is what other frameworks call an agent. How much the model decides is up to you.
The example on this page is a support desk. A customer writes "Where is my refund?", and the model can look up the order and issue the refund.
If you know agents
In most frameworks an agent is a model, a prompt, some tools and a loop the framework runs. A generator with tools is that. The model calls a tool, the framework runs it and hands back the result, and the model is called again until it answers. You never write the loop.
The difference is what surrounds it. A generator is a block like any other, so it goes wherever a block can. Put it in a pipeline and your code decides when the model runs and what happens after. Hand it everything and the model decides.
A generator with a tool
A tool is a block. This one is a handler, a block that runs your code.
import { handler } from "@flow-state-dev/core";
import { z } from "zod";
export const issueRefund = handler({
name: "issue-refund",
description: "Refund an order. Use it when the customer asks for money back.",
inputSchema: z.object({ orderId: z.string() }),
outputSchema: z.object({ reply: z.string(), refundCents: z.number() }),
execute: async (input) => ({
reply: `Refund issued for order ${input.orderId}.`,
refundCents: 1200,
}),
});
import { generator } from "@flow-state-dev/core";
import { z } from "zod";
import { issueRefund } from "../tools/issue-refund";
import { lookUpOrder } from "../tools/look-up-order";
export const supportAgent = generator({
name: "support-agent",
model: "openai/gpt-6-luna",
prompt: "You answer customer support messages. Look up the order before you refund it.",
inputSchema: z.object({ message: z.string() }),
user: (input) => input.message,
tools: [lookUpOrder, issueRefund],
itemVisibility: { client: true, history: true },
});
lookUpOrder is a handler like issueRefund that returns where an order is. It lives in its own file beside it.
Look at tools. Nothing marks a block as a tool except being listed there. The model sees each block's name, description and inputSchema, so the description is what tells it when to call, and you write it for the model. When it calls, the framework checks the arguments against the schema, runs the block and returns the output.
A tool is a block in a role. The same block can be an action a client calls, a step in a sequencer, or a tool.
The loop the generator runs
Each trip is one model call. If the model asks for a tool, the framework runs it, adds the result to the conversation and calls the model again. A call that asks for no tool ends the loop. The model can call one tool, several, or none and answer straight away.
A tool that fails does not end the run. The model is told what went wrong and can try again or answer. The generators reference covers failed calls and the limits on the loop.
How much the model decides
The third column is the supportAgent you started with: the model picks which tool, in what order and when to stop.
Say no refund may go out without the policy check. The first two columns run the check before the refund. Both use the same check, a handler that looks up the order's amount in your own system, never taking it from the model, and throws when the refund needs a person. A block that throws stops its sequencer, so the refund after it never runs.
import { handler } from "@flow-state-dev/core";
import { z } from "zod";
// A stand-in for your order system.
const orderTotals: Record<string, number> = { "A-1001": 1200 };
export const checkRefundPolicy = handler({
name: "check-refund-policy",
inputSchema: z.object({ orderId: z.string() }),
outputSchema: z.object({ orderId: z.string() }),
execute: async (input) => {
const cents = orderTotals[input.orderId];
if (cents === undefined || cents > 5000) {
throw new Error(`Order ${input.orderId} needs a person to refund it.`);
}
return { orderId: input.orderId };
},
});
The first way puts your code after the model, in a sequencer, a block that runs blocks in order. Here supportAgent keeps only lookUpOrder and answers with the order to refund. Give it outputSchema: z.object({ orderId: z.string() }) so the answer is an object code can read instead of text. The generators reference says what happens to an answer that does not fit.
import { sequencer } from "@flow-state-dev/core";
import { z } from "zod";
import { issueRefund } from "../tools/issue-refund";
import { checkRefundPolicy } from "./check-refund-policy";
import { supportAgent } from "./support-agent";
export const handleTicket = sequencer({
name: "handle-ticket",
inputSchema: z.object({ message: z.string() }),
})
.step(supportAgent)
.step(checkRefundPolicy)
.step(issueRefund);
The model picks which order to refund. The check and the refund are your code, and the check always runs first.
The second way is a pipeline as the tool. A sequencer in tools reaches the model as one tool, with a name, a description and an input schema. The model never sees its steps.
import { sequencer } from "@flow-state-dev/core";
import { z } from "zod";
import { checkRefundPolicy } from "../blocks/check-refund-policy";
import { issueRefund } from "./issue-refund";
export const refund = sequencer({
name: "refund",
description: "Refund an order. Use it when the customer asks for money back.",
inputSchema: z.object({ orderId: z.string() }),
})
.step(checkRefundPolicy)
.step(issueRefund);
In supportAgent, refund takes the place of issueRefund. Whenever the model refunds, the check runs first. When it refuses, the tool call fails and the model is told why, so it can tell the customer.
What the model is told
A generator builds the model's input from four slots: prompt, context, history and user. prompt is the standing instructions you write, and it can live in a prompt file. context is what changes per turn. history loads earlier turns, and user is this turn's message.
Say the model needs the customer's plan. The plan differs per customer, so it goes in context, not prompt. Read it from state in a context function.
import { contextFn } from "@flow-state-dev/core";
import { z } from "zod";
export const customerPlan = contextFn(
{ session: z.object({ plan: z.string().optional() }) },
({ session }) => `The customer is on the ${session.plan ?? "free"} plan.`,
);
Add context: { plan: customerPlan } to supportAgent. Named like this, the plan reaches the model as data in a <plan> tag, and its text is escaped. A bare string or function in context is added to the instructions instead, so keep customer data in named entries. The generators reference lists the other forms and the order the slots are assembled in.
What it gets you
- You choose how much the model decides, in one place.
lookUpOrder,issueRefundandcheckRefundPolicyare the same in all three. Moving between columns changes how they are composed. - A tool keeps what a block has.
lookUpOrderhas typed input and output, and it can be a tool here and a step in another flow. - The model reaches only what you hand it. Declaring
tools, eventools: [], replaces the tools a capability would add. A capability's built-in controls, such as a skills loader, come through anyway; the generators reference lists them. - You can see what it did. Each tool call is recorded as an item, an entry in the run's stream, with its input and output, and the DevTool, the local debugging UI, shows the run as a tree.
How it fits with the others
A router also picks what runs next, but your code makes the pick. In a generator with tools, the model makes it. An evaluator asks a model a question and gets a typed answer, which is the shape for branching on a model's judgement. A pattern such as a supervisor is a ready-made composition of generators with tools and sequencers.
Next
- Generators reference lists every option, the loop limits, and how failed calls and bad output behave.
- Blocks covers the five kinds and how they nest.
- Composition covers sequences, routers, parallel steps and side chains.
- Models covers choosing and configuring the model a generator calls.