Skip to main content
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:
  1. 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 askUserQuestion tool function
  2. User provides answers - Your custom handler receives these questions, presents them through your UI, collects answers, and returns them to AI.
  3. Ask additional questions - Repeat 1 and 2 if necessary to collect additional user feedback
  4. AI continues with context - The agent uses the answers to provide tailored solutions
Each question supports:
  • 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
💡 Portability and Model Agnostic - No Vendor Lock-In - Unlike implementations tied to specific LLM platforms, this Spring AI implementation works across many LLM providers, letting you switch models without rewriting code or question handlers. 💡 Relation to MCP Elicitation - AskUserQuestionTool serves as an agent-local approach to interactive user input, conceptually similar to the MCP Elicitation capability. While MCP Elicitation enables MCP servers to request structured user input via JSON schemas, AskUserQuestionTool provides the same interactive pattern directly within your agent without requiring an MCP server. Spring AI also provides full MCP Elicitation support via the @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:
The agent gathered comprehensive requirements through interactive questions before providing personalized recommendations—no assumptions, no iteration needed.

Getting Started

1. Add the dependency:
2. Configure your agent:
3. Implement your 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:
The handler displays options, accepts numeric selections (like “1,2”) or free text (like “A moderate budget”), and returns answers to the agent.

Web-Based QuestionHandler

For web applications, use CompletableFuture 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
Start experimenting with the ask-user-question-demo.

Resources