Basic chain

public class AgentChain {
  private final List stages;

  public AgentResponse invoke(String input) {
    String current = input;
    for (LlmAgent stage : stages) {
      current = stage.invoke(current).text();
    }
    return AgentResponse.of(current);
  }
}

AgentChain chain = new AgentChain(List.of(
    parserAgent, validatorAgent, transformerAgent, summarizerAgent
));
Advertisement

Passing more than text

Real pipelines carry structured state. Use a PipelineContext instead of raw strings.

public record PipelineContext(String userInput, Map extracted, List warnings) {}
Advertisement

Skipping stages conditionally

Not every stage runs every time. Give each stage a shouldRun(ctx) guard so the chain becomes a decision tree.

Early exit

Sometimes the parser stage decides no further processing is needed. Support early exit by having stages return a terminating response.

Tracing the chain

Each stage is a span. Emit start/end and duration. When something goes wrong three stages deep, the trace tells you which one failed.