Control Flow

Control Flow

Route between steps with branch and execute steps in parallel with fork.

Noetic provides two control flow primitives -- branch for conditional routing and fork for parallel execution.

branch -- Conditional Routing

branch evaluates a route function at runtime and dispatches to one step, or returns null to skip.

import { branch, step } from '@noetic/core';

const router = branch({
  id: 'language-router',
  route: (input: string, ctx) => {
    if (input.startsWith('translate:')) {
      return step.llm({
        id: 'translator',
        model: 'gpt-4o',
        system: 'Translate the text to English.',
      });
    }
    return step.llm({
      id: 'responder',
      model: 'gpt-4o-mini',
      system: 'Respond to the user.',
    });
  },
});

See branch deep dive.

fork -- Parallel Execution

fork runs multiple steps concurrently in one of three modes:

  • race -- first to finish wins, others are discarded.
  • all -- wait for every step, then merge results.
  • settle -- wait for every step, collect successes and failures, then merge.
import { fork, step } from '@noetic/core';

const parallel = fork({
  id: 'multi-search',
  mode: 'all',
  paths: (query: string) => [
    step.run({
      id: 'web-search',
      execute: async (q) => searchWeb(q),
    }),
    step.run({
      id: 'db-search',
      execute: async (q) => searchDb(q),
    }),
  ],
  merge: (results) => results.flat(),
});

See fork deep dive.

Semantic Conditions

Noetic includes condition helpers for AI-powered routing -- use embeddings, LLM classifiers, and boolean combinators to build intelligent route functions for branch.

import { branch, semanticSwitch, step } from '@noetic/core';

const router = branch({
  id: 'semantic-router',
  route: semanticSwitch({
    embed,
    cases: {
      'greeting': step.llm({ id: 'greet', model: 'gpt-4o-mini' }),
      'technical question': step.llm({ id: 'tech', model: 'gpt-4o' }),
    },
    threshold: 0.7,
  }),
});

See Semantic Conditions for the full API: semanticSwitch, semanticRoute, embeddingMatch, aiCondition, combinators, and caching.

Comparison

branchfork
ExecutionOne pathMultiple paths
ConcurrencySequentialParallel
Can skipYes (return null)No
Merge stepNoYes (all and settle modes)

Next Steps

  • branch -- full API and examples.
  • Semantic Conditions -- AI-powered routing with embeddings and LLM classifiers.
  • fork -- full API, modes, and merge functions.
  • Spawn -- isolated child execution.
  • Loop & Until -- repeat steps with termination conditions.

On this page