Spring AI for Beginners

Module 7 of 17

Module 7: Advanced Prompt Patterns

4 min read698 words
What you'll learn
Control how much reasoning a model does (eagerness)Guide a model through multi-step tasksAsk a model to check its own workGet clean, structured output you can parse in Java

"Once you know the basics, you can tune how a model thinks — quick or deep, visible or silent, free-form or strictly structured."

Level: Beginner–Intermediate · Time: ~3–4 days · Prerequisites: Module 6

Learning Objectives

By the end of this module, you will be able to:

  • Control how much reasoning a model does (eagerness)
  • Guide a model through multi-step tasks
  • Ask a model to check its own work
  • Get clean, structured output you can parse in Java

1. Reasoning Control: Low vs High Eagerness

Newer reasoning models let you dial how hard the model thinks. Not every question deserves deep analysis — a lookup should be fast; a system design should be thorough.

Low eagerness — quick, focused answers. You explicitly cap the thinking:

java
[object Object], ,[object Object], ,[object Object], ,[object Object],;

High eagerness — deep, comprehensive analysis:

java
[object Object], ,[object Object], ,[object Object], ,[object Object],;

Concept: More reasoning isn't always better — it's slower and costs more tokens. Match the effort to the task: low eagerness for simple facts, high eagerness for hard design problems.

Try this: Send the same design question twice — once capped at "maximum 2 reasoning steps," once asking for a thorough analysis. Compare the depth and the latency. You'll quickly develop an instinct for which questions are worth the extra thinking time.

2. Task Execution: Step-by-Step Progress

For multi-step jobs (a migration, a refactor), ask the model to plan first, narrate each step, then summarize. You get a workflow you can follow, not a wall of text.

java
[object Object], ,[object Object], ,[object Object], ,[object Object],;

Real-world use case: A developer asks the model to modernize old code. With a task-execution prompt, the model outlines the plan up front — so the developer can sanity-check the approach before the model writes a single line.

3. Self-Reflection: Let the Model Check Itself

Ask the model to review and improve its own answer. A first draft plus a critique pass often beats a single shot, because the model evaluates its output with fresh attention instead of committing to the first thing it produced. It's cheap insurance for anything correctness-critical.

java
[object Object], ,[object Object], ,[object Object], ,[object Object],;

Concept: This mirrors how people work — draft, review, revise. The model catches its own gaps (empty input, weird characters) in the second pass.

4. Structured / Constrained Output

Free-form text is hard for a program to use. Constrain the output to a strict shape — like JSON — so your Java code can parse it reliably. Spring AI can even map the response straight into a Java object.

Imagine building a product-review dashboard: you need a numeric score and a label for every review, not a chatty paragraph. Structured output hands your code a Sentiment object with fields you can store in a database or chart directly — no brittle text-splitting in between.

java
[object Object], ,[object Object],[object Object], {}

,[object Object], ,[object Object], ,[object Object], chatClient.prompt()
        .user(,[object Object],)
        .call()
        .entity(Sentiment.class);   ,[object Object],

Concept: .entity(Type.class) tells Spring AI to request structured output and convert it into your Java type — no fragile string parsing. This is the bridge from "chatbot" to "AI feature inside real software."

Common mistake: Asking for JSON in the prompt but then reading the reply as plain text. Use .entity(...) (or an output converter) so you get a typed object, and handle the rare case where the model returns something unexpected.

✅ Checkpoint

  1. When would you choose low eagerness over high eagerness?
  2. What are the three phases of a task-execution prompt?
  3. Why constrain output to a structure like JSON?

Answers: 1) For simple, fast tasks like lookups or basic math, to save time and tokens. 2) Plan, narrate each step, then summarize. 3) So your program can reliably parse the result into typed data instead of guessing at free text.

Key Takeaway: Advanced prompting is about control. Tune eagerness to match a task's difficulty, use task-execution prompts to get a plan-then-do workflow, ask for self-reflection to catch mistakes, and demand structured output (mapped via .entity(...)) when your Java code needs reliable, typed data. These patterns turn a chat model into a dependable component of real applications.

Further Learning

Part of "Spring AI for Beginners." Adapted from Microsoft's open Spring AI curriculum (MIT License).