Skip to main content

Utility blocks

Some jobs come up in nearly every AI app: summarize this, sort this message into a category, merge these results, title this conversation. Each takes a model prompt, an output schema and a few lines of wiring, and writing them again in every flow leaves you with a dozen slightly different copies. Utility blocks are those jobs written once, as factories that return an ordinary block. You call the factory, give the block a name, and place it in a sequencer, router or flow like any block you wrote yourself.

Most call a model with a fixed prompt, so you set options such as the model and the shape of the output, and never write the prompt. A few call no model at all.

This page uses one app, a support desk. Its support flow sorts each ticket, a { message }, with routeTicket and hands it to a reply block. Every utility is utility.<factory>, imported from the root of core. There is no @flow-state-dev/core/utility subpath.

src/flows/support/blocks/route-ticket.ts
import { utility } from "@flow-state-dev/core";
import { billingReply } from "./billing-reply";
import { technicalReply } from "./technical-reply";
import { humanReview } from "./human-review";

export const routeTicket = utility.intentRouter({
name: "route-ticket",
categories: {
billing: { description: "Charges, invoices, refunds", handler: billingReply },
technical: { description: "Errors, outages, how-to questions", handler: technicalReply },
},
confidenceThreshold: 0.7,
fallback: humanReview,
});

The three handlers are ordinary blocks, each in its own file beside this one. Minimal versions, to make the example run:

src/flows/support/blocks/billing-reply.ts
import { handler } from "@flow-state-dev/core";
import { z } from "zod";
import { draftReplyFor } from "../../../lib/replies";

export const billingReply = handler({
name: "billing-reply",
inputSchema: z.object({ message: z.string() }),
execute: async (input) => ({ reply: await draftReplyFor(input.message) }),
});
src/flows/support/blocks/technical-reply.ts
import { handler } from "@flow-state-dev/core";
import { z } from "zod";
import { draftReplyFor } from "../../../lib/replies";

export const technicalReply = handler({
name: "technical-reply",
inputSchema: z.object({ message: z.string() }),
execute: async (input) => ({ reply: await draftReplyFor(input.message) }),
});
src/flows/support/blocks/human-review.ts
import { handler } from "@flow-state-dev/core";
import { z } from "zod";
import { queueForHuman } from "../../../lib/queue";

export const humanReview = handler({
name: "human-review",
inputSchema: z.object({ message: z.string() }),
execute: async (input) => {
await queueForHuman(input);
return { reply: "A person on our team will reply shortly." };
},
});

Find a block​

You want toUse
Merge a list of artifacts without a modelcombiner
Reshape any other value without a handler.map() on a sequencer. No utility does it
Run one of several blocks chosen by a string you computekeyedRouter
Run one of several blocks chosen by what a model saysintentRouter, or cascadingRouter for several questions or a confidence floor
Create or update an instance in a resource collectionupsertResource
Name a session from what was said in itsessionTitleGenerator

All blocks​

Returns is the kind of block the factory builds. Every factory takes a name: string. It is required except on historySummary and consultSessionHistoryTool, where it is optional.

FactoryReturnsCalls a modelDefault model
contextReducer, memoryExtractor, summarizer, analyzer, intentClassifiergeneratoryesintent/utility
decomposergeneratoryesopenai/gpt-6-luna
combiner, upsertResourcehandlernonone
keyedRouterrouternonone
FactoryReturnsCalls a modelDefault model
intentRouter, consultSessionHistoryToolsequenceryesintent/utility
cascadingRoutersequencerthe evaluators you passnone, each evaluator carries its own
sessionTitleGeneratorsequenceryesopenai/gpt-6-luna
historySummarynot a block: { capability, fold, resource }through the summarize block you passnone

intent/utility is an intent name your app maps to a real model. See Models.

Generator utilities​

These six build generators. Input is any value: a string goes to the model as it is, anything else as 2-space-indented JSON, and none declares an inputSchema. All six take model, outputSchema and itemVisibility, and none takes prompt.

  • outputSchema replaces the default output schema, and the block's output type follows it.
  • itemVisibility is { client: boolean; history: boolean }. What each setting does is on Items and the stream.

The table lists the options each takes beyond those three.

BlockWhat it doesOther optionsOutput
contextReducerShrinks text in one of three modesmode: "distill" (default), "denoise", "compress"By mode, next table
memoryExtractorProposes durable facts from a conversation. It stores nothingnone{ memories: { type, content, confidence?, source? }[] }
decomposerSplits a broad request into tasks an agent can finish unaidedcontext, history{ tasks: { id, title, goal, context, deps?, priority? }[] }
summarizerSummarizes at a chosen lengthgranularity: "brief" (default), "detailed", "executive"; objectives: string or string list{ summary, keyPoints? }
analyzerAssesses the input against criteriacriteria: string list, default ["quality", "risk", "coverage", "confidence"]{ findings: { criterion, assessment, severity?, evidence? }[], score?, recommendation? }
intentClassifierPicks one label for the inputcategories: label to description, required, at least 2{ category, confidence, reasoning }

memoryExtractor type is "fact", "preference", "constraint" or "decision", and confidence runs 0 to 1. It returns an empty memories array when nothing durable is found. Storing the candidates is a block you write.

decomposer turns on the provider's web search, the only utility that does. title and context are strings or null and are always present. deps and priority ("high", "medium", "low") are optional. context carries concrete values the task needs, because the block that works the task sees only its own task. The model is asked to avoid tasks that wait on a human and to order tasks so dependencies come first. The output is not checked for either. history lets it resolve references to earlier turns.

analyzer severity is "critical", "warning" or "info". The schema enforces no range on score.

summarizer fits historySummary's summarize slot as it is.

intentClassifier throws at construction with fewer than two categories: intentClassifier requires at least 2 categories in the "categories" map. A category that is not one of the labels fails output validation with Category must be one of: <labels>. A custom outputSchema gets the same check and must produce an object with a string category, or it fails with Output must include a category field. confidence runs 0 to 1, and reasoning defaults to an empty string.

contextReducer modes​

ModeAsks the model forOutput
distillDecisions, constraints and implications, not wording{ distilled: string, keyPoints: string[] }
denoiseThe text without irrelevant or repeated content, in the original order{ cleaned: string, removedCategories?: string[] }
compressThe text cut to a length the model judges fits, keeping facts, identifiers and constraints{ compressed: string, compressionRatio?: number, dropped?: string[] }

compress has no budget option. The model decides how far to cut.

combiner​

Deterministic merge, no model. Same input, same output. Options: name, outputSchema.

Input is an array of artifacts or { artifacts: unknown[] }. Output is { combined: unknown, mergeNotes?: string[] }.

Inputcombined
[][], with a note that no artifacts were provided
Every artifact an arrayThe arrays concatenated, then deduplicated
Every artifact a plain objectMerged key by key in order. A key in both merges recursively, two arrays under one key concatenate and deduplicate, two different scalars keep the later one
Anything elseThe artifacts in order, exact duplicates removed, with a note that types were mixed

Deduplication compares values with object keys sorted, so key order does not matter. mergeNotes is always present for empty and mixed input. For arrays and objects it appears only when a note was written: <path>: deduplicated array entries during merge. when something was removed, and <path>: conflicting values resolved by taking the later artifact. when two scalars differed. <path> is combined or combined.<key>.<key>.

upsertResource​

Writes one instance of a resource collection and returns nothing. It calls no model.

How the state is written, and where the instance lives, are on Resources and collections.

OptionMeaning
nameBlock name
inputSchemaZod schema of the input
collectionKeyKey of the target collection on ctx.resources
key(input) => string, the instance key
state(input, ctx) => Record<string, unknown>, the state to create or patch with
OptionalMeaning
descriptionBlock description
content(input) => string | undefined, text written after the state. undefined writes nothing
resourcesResource declarations the block registers, so the block installs its own collection
sequencerStateSchemaThe outer sequencer's state schema, when the block reads or writes sequencer state

What .tap(block) and .step(block) pass on after a block that returns nothing is on Sequencer methods.

keyedRouter​

Runs blocks[select(input, ctx)]. It calls no model and does not adapt the input: the chosen block receives the router's input as it is. To hand a block a different shape, call block.connectInput(...) on it before you put it in blocks. More on what each branch receives is on Sequencer methods.

OptionMeaning
nameRouter name
blocksRecord<string, Block>. Two keys may name the same block
select(input, ctx) => string
fallbackBlock to run for an unregistered key
inputSchema, outputSchemaOptional

An unregistered key runs fallback. With no fallback, it throws when the request runs: [keyedRouter] no block registered under key "<key>" in router "<name>". Available: <keys>, with the registered keys comma-separated.

Only own properties of blocks count. If select returns "toString" (or "constructor", "valueOf", "__proto__"), that key is unregistered like any other: the fallback runs, or the error above names "toString".

An error thrown by select propagates unwrapped, with its own message.

intentRouter​

A sequencer that classifies the input, then runs the block for the category. Input is any value. Output is whatever the chosen block returns. Takes no outputSchema or itemVisibility.

OptionMeaning
nameRouter name
categoriesRecord<string, { description: string, handler: Block }>, at least 2
fallbackBlock for a low-confidence or unknown category
confidenceThresholdMinimum classifier confidence
modelPassed to the classifier. Default intent/utility

Routing runs in this order:

  1. With confidenceThreshold set and the confidence below it, run fallback. With no fallback, throw intentRouter "<name>" classified "<category>" with confidence <c>, below threshold <t>, and no fallback handler was provided.
  2. Otherwise run the handler for the category.
  3. A category with no handler runs fallback, or throws intentRouter "<name>" produced unknown category "<category>" and no fallback handler was provided.

The comparison is strict: a confidence equal to the threshold passes. Without a threshold the top category always wins, at any confidence.

The routing errors are raised when a request runs, not when you call intentRouter(...). Say routeTicket had no fallback, and a ticket is classified billing at 0.62. The request fails with intentRouter "route-ticket" classified "billing" with confidence 0.62, below threshold 0.7, and no fallback handler was provided. Fewer than two categories is the one construction error: intentRouter requires at least 2 categories in the "categories" map.

The chosen handler, or the fallback, receives the router's original input, not the classification. A handler's own connectInput applies to that original input. To see the classification itself, use intentClassifier.

cascadingRouter​

Walks a tree of evaluator questions and sends anything the model is not confident about to one ambiguous block. Options: name, ambiguous (required block), root (a level). The router has no model. Each evaluator named in ask carries its own.

TypeShape
CascadeLevel{ ask: evaluator block, on: string, branches: Record<optionKey, CascadeBranch> }
CascadeBranch{ minConfidence?: number, block: Block } or { minConfidence?: number, next: CascadeLevel }

The router's input is what the root evaluator accepts. The router returns what the chosen block returns, so every leaf and ambiguous should return one type.

Checked when you compile: ask accepts the router's input, on names a choice question of ask, each branch key is one of that question's options, and each leaf accepts the router's input.

How a level decides​

The modelResultReason
Chose an option with a branch, reported a confidence at or above that branch's minConfidence (or the branch has none)The branch opensnone
Reported a confidence below minConfidenceambiguousbelow-floor
Reported no usable confidenceambiguous, with or without a minConfidenceno-confidence
Chose an option with no branch, gave no answer to the routed question, or the answer is not a choiceambiguousno-branch

A usable confidence is a finite number from 0 to 1. A floor is inclusive. A branch with no minConfidence opens on any reported confidence. There is no default floor and none for the whole tree. A choice's probabilities and a boolean's probability are never read as confidence. The branch check runs before the confidence check, so an option with no branch is no-branch even when no confidence came back.

How it fails on a provider error, a refusal or a cancel is on Errors.

Refused at build time​

cascadingRouter(...) throws when called. Each message begins cascadingRouter "<name>": . <path> is root, then root/<optionKey> for each nested level.

CauseMessage ends
No ambiguousambiguous is required: the block every edge that doesn't open runs.
A level's ask is not an evaluatorthe level at "<path>" must ask an evaluator block.
on is not a question the evaluator asksroutes on "<on>", which evaluator "<name>" doesn't ask.
on is not a choiceroutes on "<on>", which is a <type> question, not a choice.
A branch key is not an option of onbranch "<path>/<key>": "<key>" is not an option of "<on>".
CauseMessage ends
A branch has both block and next, or neitherbranch "<path>" needs exactly one of block or next.
block is not a blockbranch "<path>": block is not a block.
minConfidence outside 0 to 1, NaN or not a numberbranch "<path>": minConfidence must be between 0 and 1 (got <value>).
A level contains itselfthe level at "<path>" is already inside itself; a tree can't loop.

Where an evaluator's questions is a function of the input, the option and question checks happen at the type level only.

In the trace​

Each level appears as a sequencer named <name>/<path>. Each level has three steps: the evaluator, a handler named <name>/<path>/gate that records the level's decision, and a router named <name>/<path>/route. The gate's recorded output is { input, verdict }. A level on a branch that was not taken never runs and has no rows.

Outcomeverdict
An edge opened{ level, on, edge, confidence }
ambiguous{ level, on, ambiguous, choice? }, where ambiguous is the reason from the table above and choice is present only when the answer was a choice with a string value

For example, { level: "root", on: "team", ambiguous: "no-confidence", choice: "billing" }. The evaluator's own row records its answer and any confidence. A leaf that suspends resumes on the same route without asking the evaluators again.

Conversation utilities​

BlockWhat it doesOptionsOutput
sessionTitleGeneratorWrites a session title from recent turnsmodel (a model id string, default openai/gpt-6-luna), messageLimit (turns, default 4){ title }
consultSessionHistoryToolAnswers a question from another of the user's sessionsname, model, budgetTokens, description, targetA reading or a refusal
historySummaryKeeps a running summary of turns that left the history windowsummarize, name{ capability, fold, resource }

sessionTitleGenerator​

A sequencer. Wire it into your chat sequencer with .sideChain(block), so it runs after the answer and the reply is not delayed. Input is any value. Takes no outputSchema or itemVisibility.

messageLimit counts conversational turns, not messages. One turn is a user request and its answer, tool calls included, so the default of 4 reads the last four exchanges. The model is asked for 3 to 8 words, in sentence case.

The title is trimmed, then written with ctx.session.setMetadata({ title }), but only when it is non-empty and differs from the current title. Otherwise nothing is written. A write sends a session.metadata.changed event, and useSession in @flow-state-dev/react updates the session's title live.

consultSessionHistoryTool​

Returns a tool for a generator's tools, a sequencer named by name (default consultSessionHistory).

OptionDefaultMeaning
nameconsultSessionHistoryThe tool name the model calls
modelintent/utilityModel that writes the answer
budgetTokens24,000Hard cap on history tokens read
descriptionBuilt inWhat the model reads about the tool
targetBy session id{ inputSchema, resolve(input, ctx) } for a wrapper that names the session its own way. inputSchema must carry question. resolve returns { ok: true, sessionId, of } or { ok: false, error }

Default input is { sessionId: string, question: string }, strict, question 1 to 2,000 characters. Exported as utility.consultSessionHistoryInputSchema. Output is one of:

OutputShape
A reading{ reading, of, asOf, turnsRead, earlierTurnsLeftOut, cut, running }
A refusal{ ok: false, error }

asOf is { requestId, at } for the newest settled turn read, or null. earlierTurnsLeftOut is true when older turns were not read. cut is true when turns were dropped or the newest trimmed to fit budgetTokens. running is true when a turn is in progress there. A running, suspended or waiting turn is never in the reading. The reading is capped at 8,000 characters.

Refusals, none of which runs a model:

errorWhen
self: that is this conversation, and its history is already yours.The id is the asking session
no_session: no conversation "<id>" of yours.The session does not exist or belongs to another user, org or tenant. The cases read alike. The host adds (This host can't read another conversation.) when it cannot read session history
What target.resolve returnedresolve returned { ok: false, error }

A session with no finished turns returns a reading of That conversation has no finished turns to read yet. with turnsRead: 0, and no model runs. Turns are read newest first until the budget is full. The answering generator's items stay out of the asking session's history and client view. The tool reads only the calling user's sessions and writes to, runs and wakes nothing.

historySummary​

Not a block. utility.historySummary({ summarize, name? }) returns three parts.

PartUse
capabilityGoes in the answering generator's uses. Presets context (adds the summary in an <earlier-conversation> section) and history (reads history from after the last summarized turn) are both on by default. capability.presets({ history: false }) keeps the summary and reads the whole window
foldA step to run before the answering generator, as .tap(earlier.fold). A turn that reaches the oldest quarter of the history window is folded, up to a quarter of the window's turns at a time. An unsettled turn is folded once it settles
resourceThe session-scoped resource that holds the summary

summarize is a block taking { summary: string | null, turns: LLMMessage[] } and returning { summary: string, keyPoints? }. name is the resource's accessor name and storage key, default historySummary.

If summarize throws, the fold step catches it, logs a warning and keeps the previous summary, and the next turn tries again. Turns that leave the history window in the meantime mark the summary complete: false. If turns have left the window and no fold has run, the context preset throws historySummary "<name>": turns have left the history window and no fold has run. The stored summary is capped at 8,000 characters, key points under a Key points: header.

What is stored​

Only upsertResource, sessionTitleGenerator and historySummary keep anything. The rest return values and write nothing.

PartOwnerVisible toStoredHow many
upsertResource's instanceThe collection you declareWhat that collection's client config exposesWhere the collection's scope and store put itOne per key(input) value
sessionTitleGenerator's titleThe session, as metadata titleClients reading the session (useSession, session?.title)The session's metadataOne per session, replaced by later runs

historySummary's resource is session-scoped and holds the summary. Clients see only throughRequestId (the last turn summarized), complete and folds (how many folds have run), never the summary's text.

Next​

  • Blocks for the five block kinds these factories return.
  • Route with an evaluator for cascadingRouter built step by step.
  • Models for mapping intent/utility to a model.
  • Patterns for larger compositions built from blocks like these.