Traditional AI interactions follow a common pattern: you provide a prompt, the AI makes assumptions, and produces a response. When those assumptions don’t match your needs, you’re left iterating through corrections. Each assumption creates rework—wasting time and context.
What if your AI agent could ask you clarifying questions before providing answers?
The AskUserQuestionTool addresses this. It allows the AI agent to ask clarifying questions before answering, gathers requirements interactively, and creates a specification aligned with your actual needs from the start.
Spring AI’s implementation brings this interactive pattern to the Java ecosystem, ensuring LLM portability—define your question handlers once and use them with OpenAI, Anthropic, Google Gemini, or any other supported model.
This is Part 2 of our Spring AI Agentic Patterns series. In Part 1, we explored Agent Skills—modular capabilities that extend AI functionality. Now we examine AskUserQuestionTool, which transforms AI agents into collaborative partners that gather requirements interactively.
🚀 Want to jump right in? Skip to the Getting Started section.
How AskUserQuestionTool Works
AskUserQuestionTool, part of the spring-ai-agent-utils toolkit, is a portable, Spring AI implementation of Claude Code’s AskUserQuestion tool, enabling AI agents to ask users multiple-choice questions during execution.
The tool follows a question-answer workflow:
- AI generates questions - The agent determines it needs input and constructs questions (each containing question text, header, 2-4 options, and multiSelect flag) and calls the
askUserQuestiontool function - User provides answers - Your custom handler receives these questions, presents them through your UI, collects answers, and returns them to AI.
- Ask additional questions - Repeat 1 and 2 if necessary to collect additional user feedback
- AI continues with context - The agent uses the answers to provide tailored solutions
- Single-select or multi-select - Choose one option or combine multiple
- Free-text input - Users can always provide custom text beyond predefined options
- Rich context - Every option includes a description explaining implications and trade-offs
@McpElicitation annotation for server-driven scenarios.
Example: Travel Recommendation Assistant
Here’s how the tool works in practice with a travel recommendation use case from the ask-user-question-demo:Getting Started
1. Add the dependency:QuestionHandler using the console or web examples below.
The agent automatically invokes the tool when it needs clarification and uses the answers to provide tailored solutions.
💡 Demo: ask-user-question-demo
QuestionHandler Examples
Console-Based QuestionHandler
A console-based implementation:Web-Based QuestionHandler
For web applications, useCompletableFuture to bridge async UI interactions with the synchronous QuestionHandler API. Send questions to your frontend via WebSocket/SSE and block on future.get(). Complete the future when the user submits answers via a REST endpoint.
Conclusion
AskUserQuestionTool transforms AI agents from assumption-based responders into collaborative partners that gather requirements before acting, delivering answers aligned with your needs on the first attempt. Next in this series:- TodoWriteTool - Track multi-step workflows transparently
- Subagent Orchestration - Hierarchical multi-agent architectures
- Subagent Extension Framework (coming soon) - Protocol-agnostic agent orchestration
Resources
- GitHub Repository: spring-ai-agent-utils
- AskUserQuestionTool Documentation: AskUserQuestionTool
- Spring AI Documentation: docs.spring.io/spring-ai
- Demo: ask-user-question-demo - Console-based interactive questioning (this post)
- Claude Agent SDK: User Input Documentation
Related Spring AI Blogs
- Agent Skills - Modular Capabilities - Part 1 of this series
- Dynamic Tool Discovery - Achieve 34-64% token savings
- Tool Argument Augmentation - Capture LLM reasoning during tool execution