Skip to Content
WorkflowsAsking an agent

Asking an agent

A workflow can hand a judgement to an agent: triage some notes, summarise a scan, pick a next step. It imports the agent and calls ask.

The files

clinic/ index.ts workflow triage.ts agent

triage.ts:

import { chatAgent, model } from "@loops/sdk"; export const triageAgent = chatAgent({ id: "triage", execute: async ({ submit, messages }) => submit({ messages, instructions: "You triage intake notes for a clinic. Decide whether the patient needs to be seen today.", model: model("gpt-4o-mini"), }), });

Ask for a typed answer

Pass an output schema, and ask resolves to that shape:

import { Type, workflow } from "@loops/sdk"; import { triageAgent } from "./triage"; type Intake = { patientId: string; notes: string }; export const intakeWorkflow = workflow<Intake>({ id: "intake", execute: async ({ step, event }) => { const verdict = await step.do("Triage the notes", () => triageAgent.ask({ message: event.body.notes, output: Type.Object({ urgent: Type.Boolean(), reason: Type.String(), }), }), ); return { patientId: event.body.patientId, ...verdict }; }, });

The agent answers through the schema, so verdict is { urgent: boolean; reason: string }. If it doesn’t answer in that shape, the step throws and is retried.

Ask for the reply

Leave out output and ask resolves to the agent’s reply message:

const reply = await step.do("Summarise", () => triageAgent.ask({ message: "Summarise these notes in one line: ..." }), ); console.info(reply.content);

Inside a step

Call ask inside a step.do. Each ask starts a fresh conversation with the agent, named after the run and the step, so a retried step gets the answer it already had instead of asking the model again.

The agent has to be in the same project. Asking one in another project fails.

Last updated on