Spring AI Recipe: Asking Questions to the User
We’re used to asking LLMs questions and getting answers. But what happens when the LLM needs to ask us something? That’s where things start to feel a lot more like an agent.
Instead of requiring all input up front, an agent can:
- ask clarifying questions
- gather missing details
- adapt based on user responses
Spring AI enables this pattern with AskUserQuestionTool.
NOTE: This recipe has been updated since it’s original publication to reflect changes in Spring AI 2.0.0.
Adding AskUserQuestionTool
The AskUserQuestionTool is provided by the Spring AI Agent Utils project.
Add the dependency:
implementation 'org.springaicommunity:spring-ai-agent-utils:0.7.0'Then configure AskUserQuestionTool as a default tool on ChatClientusing a ChatClientBuilderCustomizer (as covered in a previous recipe):
@Bean
ChatClientBuilderCustomizer askUserQuestionTool() {
return builder -> builder
.defaultTools(
AskUserQuestionTool.builder()
.questionHandler(new CommandLineQuestionHandler())
.build()
);
}In this example, CommandLineQuestionHandler is used to:
- display questions via standard output
- accept responses via standard input
Other implementations of QuestionHandler could support other forms of interaction, including WebSocket.
Trying it out
Assuming you are building on the chat loop from a previous recipe, try asking something that lacks sufficient detail:
> Plan a trip to Disney WorldRather than immediately responding, the LLM begins asking questions:
- When are you traveling?
- Who is going?
- How long will the trip be?
- What are your priorities?
For example:
Dates: When are you planning to go to Disney World?
1. No dates yet - I don't have dates and can be flexible. (Recommended)
2. Specific dates - I have exact arrival and departure dates.
3. Season/month - I know the month or season but not exact days.
Travelers: Who will be traveling?
1. Adults only
2. Family with kids
3. Multi-generationAs you answer these questions, the LLM gathers the context it needs to produce a more accurate and useful plan.
How it works
What makes AskUserQuestionTool particularly interesting is that it does not define specific questions.
Instead, it provides a mechanism for the LLM to:
- ask questions during execution
- gather preferences or requirements
- clarify ambiguous instructions
- offer choices to the user
The behavior is guided by the tool’s description:
Use this tool when you need to ask the user questions during execution.
This allows you to:
1. Gather user preferences or requirements
2. Clarify ambiguous instructions
3. Get decisions on implementation choices as you work
4. Offer choices to the user about what direction to take.Usage notes:
— Users will always be able to select “Other” to provide custom text input
— Use multiSelect: true to allow multiple answers to be selected for a question
— If you recommend a specific option, make that the first option in the list and add “(Recommended)” at the end of the labelThis allows the LLM to dynamically determine:
This allows the LLM to dynamically determine:
- what information is needed
- when to ask for it
- how to incorporate the answers
Why this matters
This represents a shift from traditional application design:
Traditional applications
- Require structured input up front
- Fail or guess when information is missing
Agentic applications
- Discover missing information
- Interact with the user to fill gaps
- Adapt as new information becomes available
A key building block for agents
This pattern aligns closely with a common definition of agents:
Agents are LLMs with tools in a loop to achieve a goal.
In this case:
- the goal is to plan a trip
- the tool enables interaction with the user
- the loop continues until enough information is gathered
Takeaway
AskUserQuestionTool is a simple but powerful way to make your applications more interactive and adaptive.
It allows the user to become part of the agent’s reasoning loop, leading to more accurate and personalized outcomes.
What’s next
This is just one of several components that enable agentic behavior in Spring AI.
In upcoming recipes, we’ll explore additional tools and patterns for building more capable agentic systems.