Prompts and streaming

An agent keeps one conversation. Each prompt() starts a turn that streams events and ends with a result.

Replace the prompt loop inside the try block from Quick start, before closing the agent.

Read the reply

const turn = agent.prompt('Explain how a database index speeds up a query.')
for await (const event of turn) {
  if (event.type === 'text_delta') process.stdout.write(event.delta)
}
const result = await turn.result
console.log(result.stopReason, result.usage)

Consume the events before awaiting turn.result. If you only need the result, use for await (const _ of turn) {} to drain the stream. Waiting for the result alone can stall when output is unread.

Continue the conversation

After a turn finishes, call agent.prompt() again. The agent keeps the previous prompts, replies, and tool results in context. To send instructions while a turn is running, use steering.

Handle events

EventDescription
text_deltaA piece of the reply
reasoning_deltaReasoning text, when supplied by the model
tool_startA tool call has started
tool_endA tool call has completed or failed
user_messageSteering instructions have entered the conversation

Each turn has one event consumer. TurnEvent lists the fields.

Stop a turn

Call turn.cancel() or pass an AbortSignal to prompt(). Breaking out of the event loop also cancels the turn. Cancellation cannot undo completed tool actions.

libfx turns have no fixed model-step limit. Cancel turns that exceed your application's time or usage budget; the CLI's max_agent_steps setting does not apply to createFxAgent().

When you finish with the conversation, call await agent.close(). To continue it later, save a checkpoint before closing.