Build
Models and configuration
Models & optionsChoose a model and configure your messaging agent's runtime behavior.
Configure the model and createAgent options in src/agent.ts. The starter already connects
the agent to its tools, test people and hosting environment.
Models
Section titled “Models”openAICompatible works with OpenAI-compatible APIs. With no explicit apiKey or baseURL,
it uses the first non-empty key in this order:
| Order | Environment variable | Provider |
|---|---|---|
| 1 | OPENROUTER_API_KEY | OpenRouter |
| 2 | OPENAI_API_KEY | OpenAI |
| 3 | ANTHROPIC_API_KEY | Anthropic |
| 4 | GEMINI_API_KEY | Gemini |
| 5 | FLOW_KEY | Flow’s hosted models |
With no key, it stops immediately and names the supported variables.
import { openAICompatible } from "@flow-engineer/flow";
const model = openAICompatible({ model: process.env.MODEL });import { openAICompatible } from "@flow-engineer/flow";
const model = openAICompatible({ baseURL: "http://localhost:8000/v1", model: "your-model",});Use your local server’s address and model id in the second example. A server that needs a key
can also receive apiKey explicitly.
MODEL sets the default model id. The starter passes CAFE_MODEL when set. Otherwise each
provider has a default in
src/model.ts.
Set contextTokens to tell Flow the model’s context-window size.
Limit reply length and reasoning
Section titled “Limit reply length and reasoning”Available in Framework 0.18.0.
const model = openAICompatible({ model: "anthropic/claude-haiku-5.5", maxTokens: 4000, // sent as max_tokens reasoning: "low", // "minimal", "low", "medium" or "high"});| Option | What it does |
|---|---|
maxTokens | The most tokens one reply may use, sent as max_tokens. Unset, providers such as OpenRouter take the model’s maximum and hold credit for it on every call |
reasoning | How hard a reasoning model thinks before it answers, sent as OpenRouter’s reasoning: { effort }. Unset, the model’s default |
Expected result: each model request carries max_tokens: 4000 and reasoning: { effort: "low" }.
Leave reasoning unset for a model that does not reason.
Use Flow’s model key
Section titled “Use Flow’s model key”flow setup writes FLOW_KEY, which can use the account’s $2 of trial model credit. A provider
key takes priority over it. flow status reports the remaining credit without printing the key.
flowModel({ model }) explicitly selects Flow’s hosted models; its default is
anthropic/claude-haiku-5.5. GET /models on the configured Flow model server lists the models
it offers. Set FLOW_MODEL_URL to use another Flow model server. On Flow’s hosting, pass
model: flowModel(): a provider key sent as a secret would otherwise win over FLOW_KEY
(what the hosted agent gets).
Rotation and exhausted trial credit
flow setup --rotate key replaces FLOW_KEY. The old key works for another 24 hours;
--now ends it immediately. Neither command prints the full key.
Exhausted credit returns trial_credit_used with a next step. Add your own provider key to
continue with that provider.
Transcribe a voice note
Section titled “Transcribe a voice note”For a local audio file named voice.ogg:
import { readFile } from "node:fs/promises";import { flowTranscribe } from "@flow-engineer/flow";
const audio = await readFile("voice.ogg");const { text } = await flowTranscribe(audio, { filename: "voice.ogg", mimeType: "audio/ogg",});console.log(text);This uses the same Flow key and model-server address. It accepts a Blob, File, Buffer,
Uint8Array or ArrayBuffer, up to 15 MB. language is optional and detected when omitted.
Errors include a message, next step, code and HTTP status.
Videos people send
Section titled “Videos people send”From Framework 0.17.1 the agent reacts 👀 to a video the moment it arrives, before the video is downloaded, described or watched, and the turn that answers it shows no typing on that channel (none on arrival, none while the agent works or writes). It is on by default and needs no code. Where the channel has reactions, the person sees 👀 on their video at once, then the reply once the agent has seen or read it. To have a video type on arrival instead:
import { createAgent } from "@flow-engineer/flow";
const agent = createAgent({ // ...your dir, model, tools and channels videoReaction: false,});With it off, a video types on arrival, kept up while it is described. Photos, voice notes and documents type on arrival either way.
Channels, failures and events
- WhatsApp and web: the reaction is sent. On WhatsApp the blue ticks still show on arrival: the read receipt goes by itself (no typing indicator) with the reaction.
- Telegram: only Telegram’s own bot reaction emoji, 👀 among them. A string
videoReactionis another emoji; on Telegram one outside that set is not sent, and that turn types instead. - iMessage: iOS 18 and later show 👀 as a reaction; iPhones on iOS 17 or older may show it as a line of text. The read receipt still goes when the turn takes the message.
- SMS, and channels without reactions: no reaction; the turn types as before.
- A reaction that fails is logged as a warning and never fails the turn; that turn types instead.
- It is sent as the
reacttool’s reaction is ({ type: "reaction", emoji, messageId }), with areactionevent markedarrival: true. If the agent reacts to the same message itself later, its reaction replaces this one where the channel allows.
A model that can’t watch videos gets a description of each one instead. describeVideoWith has
a video model describe it through the agent’s model key (FLOW_KEY, an OpenRouter key or a
Gemini key; OpenAI’s and Anthropic’s APIs take no video):
import { createAgent, describeVideoWith } from "@flow-engineer/flow";
const agent = createAgent({ // ...your dir, model, tools and channels describeVideo: describeVideoWith("google/gemini-3.8-flash"),});From Framework 0.17.0 the agent can also ask about a video later, with the built-in
ask_about_video tool. When a buyer asks “is there a scratch near the charging port?” about a
video sent earlier, the agent calls it and a video model watches the video again:
{ "question": "Is there a scratch near the charging port?", "message": "m3"}{ "video": "m3", "answer": "Yes, a short scratch just left of the charging port."}message is the id of the message the video came in; without it, the latest video. The answer
comes from the describer’s ask(video, question) (describeVideoWith’s has one), or else, for a
model that watches videos (openAICompatible({ videos: true })), from the agent’s own model.
When it is offered, limits and your own describer
- Offered only while the conversation has a video the agent can’t see now (one described, or
one its model saw turns ago), and its channel can fetch the file again: Flow Messaging, the
simulators,
flow test, or your own channel withfile(ref). - The video is fetched again for each question, up to 10 MB, and never kept. 3 questions a turn
and 10 a video. A video the channel no longer keeps, or a video model that fails, is a
ToolErrorthe agent reads (“Ask them to send it again”) and anattachment.failedevent. - Each question is a video model call on your key. To keep descriptions only, pass a describer
without
ask:const described = describeVideoWith(...), thendescribeVideo: (video) => described(video). - Your own describer answers questions when it has
asktoo:Object.assign(async ({ mime, data }) => text, { ask: async ({ mime, data }, question) => text }). - A tool of your own named
ask_about_videois hidden while the built-in one is offered: rename it.
createAgent options
Section titled “createAgent options”The tables below cover common options. The TypeScript
AgentOptions definition
is the complete contract.
Instructions and tools
Section titled “Instructions and tools”| Option | Purpose |
|---|---|
dir | Folder containing AGENTS.md and skills/ |
files | Supply those files as text when there is no folder |
model | A Model, usually from openAICompatible |
tools | Your tool definitions |
groups | Tool groups offered only on their channels (toolGroup). See one channel only |
inlineSkills | Include every skill in the prompt rather than loading on demand |
Channels and identity
Section titled “Channels and identity”| Option | Purpose |
|---|---|
channels | whatsapp(), sms(), customUI(), webChat() or provider channel definitions |
resolve | Resolve a channel identity to your account key |
testPeople | People the playground may play, as [{ name, phone }] |
profile | Account data fetched on demand through get_user_profile |
context | Small facts added before every turn; keep slow lookups out of it |
timeZone | The business time zone, used when the person’s is unknown |
quietHours | Hours to hold business-started messages; false disables them |
transcribe | Speech-to-text for incoming voice notes |
describeVideo | Text for videos the model can’t watch, and answers to ask_about_video (videos) |
videoReaction | 👀 on a video the moment it arrives, and no typing for its turn; on by default, false types on arrival instead (videos) |
See messaging and people for identity and time-zone behavior, and reminders and nudges for delivery rules.
State and hosting
Section titled “State and hosting”| Option | Purpose |
|---|---|
database | Durable storage; defaults to an in-memory SQLite database |
store | Supply your own store instead of a database |
records | Collections of the agent’s own small records, kept in the same store (records) |
wakeUps | false removes the model’s wake_me_up and cancel_wake_up tools and their list from its prompt; wake-ups set by code still run (wake-ups) |
host | Hosting options passed as host: options.host from buildAgent |
routes | Your HTTP handlers, keyed by exact path, answered by agent.fetch(request); on Flow’s hosting they are served at the agent’s public URL (webhooks and callbacks) |
events | Event sinks receiving runtime events |
traces | Trace export settings; defaults come from the environment |
Self-hosting covers persistence and restart handling. See Deployment for Flow hosting and Traces for observability.
Turn timing and history
Section titled “Turn timing and history”| Option | Default | Purpose |
|---|---|---|
debounceMs | 300 | Quiet time after a message before a reply starts |
deadlineMs | 60,000 | How long a reply waits for a tool before moving it to the background |
partialAfterMs | 5,000 | How long a finished tool result waits for other tools in that response |
maxSteps | 8 | Model steps per turn |
history.maxTokens | 60% of contextTokens, else 50,000 | History carried into each model call |
history.toolResultChars | 12,000 | Limit for a tool result in history |
Durations are milliseconds. A tool’s own deadlineMs overrides the agent’s value for that tool.
Message hooks and templates
Section titled “Message hooks and templates”| Option | Purpose |
|---|---|
beforeSend | Change or refuse the final message before validation, sending and recording |
intercept | Answer an incoming message without a model call |
templates | Your declared WhatsApp templates |
fallbackTemplate | The template used when a nudge meets a closed WhatsApp window |
shareUpdates | Enable, disable or customize live status guidance in web and app interfaces |
writing | Replace (a string) or leave out (false) the guidance on how messages read (details); from 0.12.0 |
beforeSend return values
beforeSend(message, { channel, address, info, origin }) runs for messages, carousels and
templates on every channel. Return nothing to send the input, another message to replace it,
or { refuse: "why" } to tell the agent to try again.
It runs before platform content checks. History, transcripts and test judges see what was actually sent.
An edit’s new text runs through it too, as a message with only text, and
context.edit.messageId names the message being edited. A replacement is what goes out; a
refusal sends nothing.
Edits, unsends and message ids
On channels whose info has edits and unsends (Telegram and iMessage on Flow Messaging, or
a custom UI with editMessage and unsendMessage), the agent is offered edit_message
({ message_id, text }) and unsend_message ({ message_id }). Its send results there name
each message ({"delivered":true,"channel":"imessage","message_id":"msg_..."}). WhatsApp and
SMS offer neither tool.
The platform decides how late an edit or unsend may come. Flow Messaging refuses a late one
when it is made (422 unsupported_content with edit_window_closed, unsend_window_closed,
first_message_not_editable or not_on_sms), and the agent gets that refusal as its tool
result, for example Not edited: unsupported_content (edit_window_closed, on imessage): iMessage allows edits within 15 minutes of sending; .... Nothing is sent instead.
Your own code gets the id too: a send’s SendReceipt.messageId, the messageId on
message.out events, and a sent nudge’s NudgeResult.messageId. message.edited and
message.unsent events record each change. Requires @flow-engineer/flow 0.7.0 or later.
intercept and keyword messages
intercept(conversationId, text, message) sees the incoming channel and any STOP, START or
HELP classification in message.keyword.
Its answer is stored in history and the transcript. STOP and START are enforced even if the hook returns an answer or throws. See keyword handling.
Thanks, we read every comment.