Building reliable LLM features often fails when developers try to cram extraction, normalization, sorting, and formatting into a single massive prompt. A recent deep dive into the spring-ai-examples repository argues for prompt chaining instead, breaking complex tasks into a fixed sequence of smaller, testable steps. While the pattern is endorsed by Anthropic’s research on effective agents, the actual code in the ChainWorkflow sample reveals a critical disconnect: the documentation promises validation gates that the implementation simply does not have.

The Four-Step Chain Architecture

The sample implements a chain where each step receives the output of the previous one, performing a single transformation. The workflow is defined in a DEFAULT_SYSTEM_PROMPTS array within the ChainWorkflow class. Step 1 extracts numerical values and metrics. Step 2 normalizes these values to percentages or decimals. Step 3 sorts the lines in descending order. Step 4 formats the result as a Markdown table. This structure keeps each prompt short and focused, with concrete examples embedded in the instructions to pin down the expected output format for the next stage.

Missing Validation Gates in Production

The most significant finding is that the sample’s chain method is a naive loop. It passes the response from one step directly into the next without any intermediate checks. The README claims each step acts as a gate that validates output, but the code does not enforce this contract. If Step 1 returns an empty string or a paragraph of apologies instead of the expected value: metric lines, Step 2 will still attempt to process it, leading to silent failures or garbled data. The author proposes adding deterministic checks, such as regex validation for line formats, or model-based checks to ensure integrity before proceeding.

Operational Hardening Requirements

For anything beyond a demo, the current implementation lacks essential operational safeguards. The chain executes four separate model calls, multiplying latency and token costs by four. There is no retry policy for transient errors like rate limits, nor a global timeout to cap total execution time. Furthermore, the sample uses System.out.println for logging, which is too noisy for production and risks leaking user data. Developers must replace this with structured logging that captures step indices, durations, and output sizes without exposing sensitive content.

Deterministic Logic vs. Model Calls

A key architectural insight is knowing when to stop using the LLM. The sample uses the model for all four steps, including sorting and formatting. However, sorting numbers and generating Markdown tables are deterministic tasks that Java code can handle more reliably and cheaply. The author suggests that in a real service, Steps 3 and 4 could be replaced with plain Java functions, leaving the model to handle only the language understanding tasks in Steps 1 and 2. This hybrid approach reduces variance and cost while maintaining the chain's modularity.

Key Takeaways

  • Prompt chaining is superior to monolithic prompts for tasks with predictable stages, offering better debuggability.
  • The spring-ai-examples ChainWorkflow lacks the validation gates described in its documentation.
  • Always define strict output contracts between steps to enable independent testing and failure isolation.
  • Replace model calls with deterministic code for simple transformations like sorting and formatting to save costs.

The Bottom Line

Prompt chaining is a powerful pattern, but the spring-ai-examples sample is a teaching tool, not a production-ready framework. Do not copy the loop without adding validation gates, retry logic, and cost controls. The intelligence lives in the prompts, but the reliability lives in the code between them.