Orchestrating Tools: Sequential, Parallel and Conditional Composition

Quoting a ride sounds like one action, yet the tool behind it has to estimate a route, check for surge pricing and count nearby drivers before it can answer. Whether those lookups run one after another, side by side, or only when needed decides how fast the answer arrives and what happens when one of them fails. LVD names four patterns for this, and each has a clear use.

What is being composed

A tool's handler rarely does one thing. It combines several calls to Resources with a few pure calculations from Utils to produce a coherent result. Notice what is on that list and what is not. The pieces are resource calls and utility functions, never other tools. Chaining whole tools together is a different question, and the Tools layer answers it with a rule: see Axiom A5 for why that sequencing moves to a higher layer.

Within a single handler, the book describes four patterns.

Sequential, parallel and conditional

Sequential composition is the simplest: each step feeds its result into the next. Fetch a booking, choose a pickup point from it, then record the assignment. The total latency is the sum of every step, so it is the right shape only when each step truly needs the one before.

Parallel composition applies when steps are independent of one another. They start together and the handler waits for all of them, so total latency is the slowest step rather than the sum. The book calls this an important optimisation for tools under tight latency budgets.

const quoteHandler = async ({ pickup, dropoff }: QuoteParams) => {
  const [route, surge, nearby] = await Promise.all([
    routeClient.estimate(pickup, dropoff),
    surgeClient.multiplierAt(pickup),
    driverRepository.countAvailable(pickup),
  ]);
  const fare = priceTrip(route, surge); // pure function from Utils
  return { text: `Fare ${fare.display}`, data: { fare, nearby } };
};

Conditional composition branches on an intermediate result. A cancellation tool might waive its fee when no driver has been assigned yet, and charge one when a driver is already on the way. The book recommends moving each branch into a helper function, which keeps the top-level handler readable and holds its cyclomatic complexity at six or below.

Deciding what to do when a step fails

The fourth pattern is about the unhappy path. When a composed workflow fails partway through, the tool has to choose how the error travels. The book recommends two strategies.

  • Fail fast. The tool throws immediately and leaves it to the protocol layer above to turn that error into something the client can read. This is the default.
  • Partial result. The tool catches the error and returns a result that signals partial success, with diagnostic details attached.

The choice comes down to a practical question: is half an answer still useful to the caller? A quote tool that could not fetch surge pricing might be better off failing, since a wrong fare is worse than none. A tool that checks the availability of several items could reasonably report which ones are short and let the caller decide what to do about it. The point is to decide deliberately rather than let whichever exception arrives first make the choice.

Reading a workflow as a graph

A quick way to pick between the first two patterns is to draw the data flow. If one step consumes another step's output, the arrow forces an order. If two steps share no arrow, nothing stops them running together. Many handlers written as a ladder of await lines turn out to contain independent calls that were serialised by habit.

It also pays to look at where the failure handling sits. A handler that mixes parallel fetches, a branch and a partial-result path can still stay tidy if each piece is small and named. The book's combined checkout example uses all four patterns in a single handler, which is a reminder that they are meant to be mixed.

The takeaway

Orchestration inside a tool comes down to four choices: order, concurrency, branching and failure. Make each one on purpose. Sequential costs the sum, parallel costs the maximum, branches belong in helpers, and errors either fail fast or carry a partial result. When the workflow starts to need more than one tool, step up a layer rather than sideways. The next hazard is state that must change together, which is the subject of transactions and sagas. The tool structure these patterns live in is covered in the anatomy of a tool.

What the book covers

  • Annotated examples of each of the four composition patterns
  • How helper functions keep a handler's cyclomatic complexity low
  • A combined workflow that uses all four patterns in one handler
  • Fail-fast and partial-result error propagation in composed workflows