picoflow.io

Lesson 11: Fan-Out Patterns


Two Fan-Out Patterns

The demo uses two different orchestration patterns.

1. Step-level fan-out

Inside InContextStep, the flow launches child steps from within the current step using runStep or runSteps:

const [concurStep1, concurStep2] = await this.runSteps([
  { step: ConcurStep1, userMessage: 'Run the first concurrent follow-up task.' },
  { step: ConcurStep2, userMessage: 'Run the second concurrent follow-up task.' },
]);
  • This stays inside the current step’s lifecycle.
  • The child steps are orchestrated as part of the parent step’s work.
  • Results are saved back into step state for the parent to consume.

2. Flow-level fan-out

At the top level, BasicFlow uses concurrentSteps(...) to spawn many child flow runs from the shell:

await this.concurrentSteps<string>({
  items: nths,
  batchSize: 3,
  onConfig: (item) => ({ nth: item, isPresident: true }),
  onBotResponse(item, response) {
    step.saveState({ [item]: response['message'] });
  },
});
  • This is document-flow fan-out rather than step fan-out.
  • The parent flow creates multiple mini-runs and aggregates their results.
  • It is useful when the work is naturally parallel and should be isolated as separate flow runs.

Optional Mini-Flow

When config.isPresident is true, BasicFlow can spawn concurrent trivia runs using PresidentStep.

public onCrossing(...) {
  const nth = this.getContext<string>('config.nth');
  this.sessionCompleted();
  return new HumanMessageEx(this, `Who is the ${nth} President of United State`);
}
  • Uses its own memory namespace (president).
  • Marks the session complete immediately inside each spawned run.

Triggering It Over HTTP

To force the concurrent path from outside the UI, hit the flow runner directly:

curl -X POST http://localhost:8000/ai/run \
  -H "Content-Type: application/json" \
  -H "CHAT_SESSION_ID: <uuid>" \
  -d '{ 
        "flowName": "BasicFlow",
        "config": { "_concurrent": true }
      }'
  • _concurrent: true tells BasicFlow to activate the PresidentStep fan‑out.
  • Add a CHAT_SESSION_ID header to keep runs isolated if you are issuing multiple requests.

When to Use

  • Batch evaluations or trivia fan-out.
  • Large document scoring or summarization.
  • Step-local orchestration when one step needs to delegate follow-up work.

With this, every step in BasicFlow now has a dedicated lesson following the real transition order.