Script Agent Queries
Scripts can query LLM agents to make decisions using structured questions.
Overview
Section titled “Overview”The ask.agent() function lets scripts:
- Ask the LLM structured multiple-choice questions
- Get parsed responses with selected indices
- Handle “other” free-text answers
- Make decisions based on LLM reasoning
This enables script-to-agent communication where another LLM answers questions instead of a human.
Lua API
Section titled “Lua API”-- Create a question batchlocal batch = ask.batch() :question(ask.question("Auth", "Which authentication method?") :choice("OAuth (Recommended)") :choice("JWT") :choice("API Key")) :question(ask.question("Database", "Which database?") :choice("PostgreSQL") :choice("SQLite") :choice("MySQL"))
-- Ask the LLM agentlocal response = ask.agent(batch)
-- Process the responseif not response:is_cancelled() then local auth_answer = response:get_answer(0) local selected = auth_answer:selected_indices()
if selected[1] == 0 then -- OAuth was selected configure_oauth() elseif auth_answer:has_other() then -- Custom answer provided local custom = auth_answer:other_text() handle_custom_auth(custom) endendQuestion Building
Section titled “Question Building”Creating Questions
Section titled “Creating Questions”-- Basic question with choiceslocal q = ask.question("Header", "Question text?") :choice("Option A") :choice("Option B") :choice("Option C")
-- Multi-select questionlocal q = ask.question("Features", "Which features to enable?") :choice("Logging") :choice("Metrics") :choice("Tracing") :multi_select()Creating Batches
Section titled “Creating Batches”-- Multiple questions in one batchlocal batch = ask.batch() :question(q1) :question(q2) :question(q3)
-- Check batch infoprint(batch:question_count()) -- 3print(batch:id()) -- UUID stringResponse Handling
Section titled “Response Handling”Answer Structure
Section titled “Answer Structure”Each answer contains:
selected_indices()- Array of selected choice indices (0-based)other_text()- Custom text if “other” was chosenhas_other()- Boolean indicating if custom text exists
local answer = response:get_answer(0)
-- Check what was selectedlocal indices = answer:selected_indices()for i, idx in ipairs(indices) do print("Selected choice " .. idx)end
-- Check for custom answerif answer:has_other() then print("Custom: " .. answer:other_text())endBatch Response
Section titled “Batch Response”local response = ask.agent(batch)
-- Check if cancelledif response:is_cancelled() then print("Request was cancelled") returnend
-- Iterate answersfor i = 0, response:answer_count() - 1 do local answer = response:get_answer(i) -- Process each answerendUse Cases
Section titled “Use Cases”Dynamic Configuration
Section titled “Dynamic Configuration”-- Let LLM choose configuration based on contextlocal batch = ask.batch() :question(ask.question("Performance", "Optimize for?") :choice("Memory efficiency") :choice("Speed") :choice("Balanced"))
local response = ask.agent(batch)local choice = response:get_answer(0):selected_indices()[1]
if choice == 0 then config.memory_limit = "256MB"elseif choice == 1 then config.workers = 8endDecision Trees
Section titled “Decision Trees”-- Multi-step decision makinglocal function decide_action(context) local batch = ask.batch() :question(ask.question("Action", "Given context: " .. context .. "\nWhat should we do?") :choice("Proceed with caution") :choice("Request more info") :choice("Abort operation"))
local response = ask.agent(batch) return response:get_answer(0):selected_indices()[1]endSee Also
Section titled “See Also”- Custom Handlers - Using ask_agent in handlers
- Creating Plugins - Plugin development
- Ask Module - Full ask module reference