Parallel agents & shared state
Use runAgent for one agent at a time, runAgents to run several in parallel, and ctx.state to share data across the workflow.
With observability: true, runs appear in the dashboard so you can inspect each step and the shared state over time.
Run agents in sequence
async function entry(ctx) {
ctx.state.stage = "draft";
const draft = await ctx.runAgent("content-creator");
ctx.state.stage = "edit";
const edited = await ctx.runAgent("clarity-editor", {
input: { content: draft.content },
});
return { article: edited.content };
}async def entry(ctx):
ctx.state["stage"] = "draft"
draft = await ctx.run_agent("content-creator")
ctx.state["stage"] = "edit"
edited = await ctx.run_agent(
"clarity-editor",
input_data={"content": draft.get("content")},
)
return {"article": edited.get("content")}Run agents in parallel
runAgents / run_agents starts multiple agents at once and returns their results when all finish.
const planning = await ctx.runAgents([
{ name: "outline-planner" },
{ name: "keyword-researcher" },
]);
const [outline, keywords] = planning.results;
const creation = await ctx.runAgent("content-creator", {
input: {
outline: outline.outline,
keywords: keywords.keywords,
},
});
const refinement = await ctx.runAgents([
{ name: "clarity-editor", input: { content: creation.content } },
{ name: "seo-optimizer", input: { content: creation.content } },
{ name: "social-snippets", input: { content: creation.content } },
]);
const [clarity, seo, social] = refinement.results;
return {
article: clarity.content,
seo,
social: social.snippets,
};You can also pass an object/map so results are keyed by name:
Share data with ctx.state
ctx.state is a mutable object shared for the whole run. Set values in one agent and read them in another (or in entry).
After runWorkflow, the final state is also on the return value:
Related
Last updated