Part 2 of 6

Control flow: how work moves through a prompt system

Six control-flow patterns from chaining to agents, and a map of twenty programming principles translated into prompt systems.

Sections 05–06
05 · Control flow

How work moves through a prompt system

Six patterns cover most systems. The first five are workflows: paths controlled by code. The sixth hands the next-step decision to the model.

1 · Prompt chaining WORKFLOW
Prompt A Output A Prompt B Output B

Use when a task has clear sequential stages, each step depends on the previous result, and intermediate results can be validated.

Request → Requirements → Spec → Plan → Code
2 · Routing WORKFLOW
Input Classifier
Workflow A Workflow B Workflow C

Use when different request types need different workflows, or a cheaper, safer path can be selected. Not every message requires an agent.

Question · Small edit · Redesign · New build · Configuration
3 · Parallelization WORKFLOW
Split
Worker A Worker B Worker C
Merge

Use when subtasks are independent and results can be merged or compared.

Accessibility · SEO · Visual · Performance reviews
4 · Orchestrator-workers WORKFLOW
Orchestrator
Specialist 1 Specialist 2 Specialist 3
Synthesis

Use when subtasks must be created dynamically and a central component coordinates ownership.

⚠ Define task ownership and merge rules to avoid overlapping edits.
5 · Evaluator-optimizer WORKFLOW
Generate Evaluate Pass?
yes
✓ Finish
Repair no

Use when quality can be measured, results can be repaired iteratively, and external tests or graders exist.

6 · Agents MODEL-DRIVEN LOOP
Goal Observe Decide Act with tool

The model dynamically chooses what to do next, which tool to call, whether to decompose or repair, and when it believes the task is complete.

Agents must operate inside a declared state machine, capability model and stopping policy.
Workflow ≠ agent

A predefined path controlled primarily by code. The model fills in steps; code decides what happens next.

Agent

A model-driven loop in which the model dynamically decides the next action within declared boundaries. Never use the two words as synonyms.

What this means for the business
An agent is the most expensive option, not the default one

Six patterns cover almost every system, and five of them are ordinary workflows controlled by your own code. Agents hand the next-step decision to the model, which makes them the most flexible and the least predictable choice, with the widest spread in cost and latency. The same feature can differ by an order of magnitude in running cost depending on which pattern it uses, and the cheaper patterns are usually easier to test and explain to auditors.

Decision For every use case, require the team to name the chosen pattern and justify why a simpler one is not sufficient.
06 · The principle map

Modern programming principles, transferred

Twenty established software-engineering concepts, and what each becomes in a prompt-based system.

Concept
Traditional programming
Prompt & agent equivalent
Functions
A reusable operation with inputs and outputs.
A prompt module with one defined responsibility, structured input and structured output.
Types
Integers, strings, enums, classes, validated structures.
JSON Schema, Pydantic models, enums, validators, structured outputs; invalid states become hard to represent.
Interfaces
Contracts between modules.
Tool descriptions, API schemas, agent capabilities, input/output contracts.
Modules
Reusable components with focused responsibilities.
Specialized prompts, skills, tool adapters, evaluators, planners, agents.
Composition
Combining small functions and modules.
Connecting prompt modules into workflows and pipelines.
Control flow
Sequence, branch, loop, parallel execution.
Prompt chaining, routing, evaluator loops, parallel workers, agent tool loops.
State machines
Explicit states and legal transitions.
Declared workflow stages: RECEIVED → SPECIFIED → PLANNED → APPROVED → VERIFIED → PUBLISHED
Recursion
A function invokes itself or repeats a structure.
An agent decomposes tasks into sub-tasks that decompose again; requires max depth, budget caps, progress checks.
Concurrency
Several processes run at the same time.
Parallel agents, fan-out/fan-in, voting, competing proposals. Risks: races, conflicting edits, stale context.
Exception handling
Errors are caught and handled.
Retry · repair · fallback · escalate · ask the user · abort · rollback.
Design by contract
Preconditions, postconditions, invariants.
Every prompt module declares what must be true before, after, and throughout the workflow.
Transactions
Changes succeed together or roll back.
Plan → Preview → Approve → Apply → Verify → Commit; on failure: Rollback
Idempotency
Repeating an operation creates no unintended duplicates.
A retried task must not duplicate records, send an email twice, or create duplicate components.
Event sourcing
Every meaningful state-changing event is recorded.
Log user intent, model decisions, tool calls, state transitions, approvals, file changes, evaluations, rollbacks.
Testing
Unit, integration, end-to-end tests.
Prompt-unit evals, golden cases, tool integration tests, multi-turn scenarios, environment-state verification, regression suites.
Compilation
Source code becomes an executable representation.
Natural language becomes Intent → Spec → Plan → Tool calls → Verified artifact; prompts optimized against eval metrics.
Observability
Logs, metrics, traces, debuggers.
Track prompts, model versions, tool calls, decisions, state transitions, costs, tokens, latency, retries, failures, eval scores.
Security
Least privilege, isolation, authentication, sandboxing.
Tool capability restrictions, tenant isolation, network restrictions, approval gates, prompt-injection defenses, sandboxed execution, secret isolation.
Version control
Source code and configuration are versioned.
Version prompts, system instructions, models, tool schemas, workflow definitions, eval datasets, policies, design systems.
Deployment engineering
Staging, canaries, feature flags, rollback.
Shadow runs, offline evals, limited rollout, model comparison, prompt canaries, gradual tool access, automatic rollback after regression.
What this means for the business
This is not a new discipline, and it needs no new job title

Each row in this table maps an established engineering concept onto its prompt-system equivalent. Read together, they say something useful for staffing: reliable AI features are built by experienced engineers applying familiar ideas, such as types, contracts, transactions, idempotency and observability, to a probabilistic component. The failure pattern is the opposite, a team that treats prompting as a separate craft and rediscovers thirty years of software engineering the expensive way.

Decision Staff these projects with senior engineers who already know these principles, and use this table as the review checklist for their designs.
Series

Continue the engineering guide

This is one part of a six-part guide on turning prompts into specification-driven probabilistic software.

Back to the series overview →

App & Cloud Architecture · Book a Product Discovery Call

More articles