Install
$ agentstack add skill-simbajigege-book2skills-query-loop-implementation ✓ scanned · ✓ verified, works with Claude Code, Cursor, and more.
Security review
✓ PassedNo issues found. Passed automated security review. · v0.1.0 How review works →
- ✓ Prompt-injection patterns
- ✓ Secret / credential exfiltration
- ✓ Dangerous shell & filesystem operations
- ✓ Untrusted network calls
- ✓ Known-malicious package signatures
What it can access
- ✓ Network access No
- ✓ Filesystem access No
- ✓ Shell / process execution No
- ✓ Environment & secrets No
- ✓ Dynamic code execution No
From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.
Verified badge
Passed review? Show it. Paste this badge into your README, it links to the public security report.
Reliability & compatibility
Declared compatibility
Compatibility is declared by the source manifest. End-to-end runtime verification is coming, see below.
We're building live execution health for every listing: tool-call success rate, median latency, uptime, and last-checked timestamps, measured, not self-reported. It isn't live yet, so we don't show numbers we can't stand behind.
How agent discovery & health will work →About
Query Loop Implementation
Core Idea
Build the loop as product infrastructure, not prompt glue:
ConversationManager -> QueryLoop -> ToolRuntime
ConversationManagerowns durable state: session id, messages, user settings, budget, persistence.QueryLoopowns one task turn: call model, detect tool calls, execute tools, append tool results, repeat.ToolRuntimeowns registered tools: schemas, permission checks, execution, error formatting.
Use ReAct as the mental model:
Thought -> Action -> Observation -> Thought -> Answer
Implement it as structured API traffic:
model thinking/text -> tool_call -> tool_result -> next model call -> final text
Implementation Workflow
- Inspect the user's stack and current LLM call site.
Find where messages are built, where the model is called, and whether tool/function calling is already configured.
- Introduce a minimal query loop.
Keep the first version narrow: one model call function, one tool registry, explicit exit conditions.
- Normalize message shapes.
Use the provider's structured tool-call format when available. Avoid parsing free-form Action: text unless the provider has no function/tool-calling API.
- Add tool execution safety.
Validate tool input against a schema, apply permission checks for risky tools, wrap failures as tool results, and log every call.
- Add exit and budget guards before expanding features.
Always include maxTurns, timeout/cancel support, token/cost budget checks, and a fatal-error path.
- Keep context-window strategy outside this skill.
Accept messages as loop input and return updated messages, but leave trimming, retrieval, summarization, and compaction to a separate context-management layer.
Minimal Loop
Adapt this shape to the user's language and SDK:
async function runQueryLoop({
initialMessages,
model,
tools,
maxTurns = 10,
signal,
}: {
initialMessages: Message[]
model: ModelClient
tools: ToolRegistry
maxTurns?: number
signal?: AbortSignal
}) {
let messages = [...initialMessages]
for (let turn = 1; turn
call(input: ValidatedInput, ctx: ToolContext): Promise
}
Execution order:
find tool by name
-> schema validate model input
-> run tool-specific validation
-> check permission
-> call tool
-> format success or error as tool_result
Return tool errors to the model when it can plausibly recover, for example invalid arguments, file not found, empty search result, or transient API errors. Stop the loop for security violations, repeated failures, missing credentials, or budget exhaustion.
Product Design Guidance
Keep "intelligence" in the model and "reliability" in code:
- Let the model decide whether it needs another tool call.
- Let code enforce schemas, permissions, budgets, and loop exits.
- Keep prompts focused on tool semantics and task policy.
- Keep implementation focused on deterministic control flow.
For simple AI apps, avoid subagents, worktrees, and streaming tool execution at first. Add them only when the product actually needs parallel work, isolation, or long-running tasks.
When More Detail Is Needed
Read references/query-loop-patterns.md when designing a new query engine, reviewing an existing implementation, or explaining ReAct-to-query-loop architecture to another engineer.
Source & license
This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.
- Author: simbajigege
- Source: simbajigege/book2skills
- License: MIT
- Homepage: https://book2skills.com/
Install and usage instructions live in the source repository linked above.
Reviews
No reviews yet, be the first.
Write a review
Versions
- v0.1.0 Imported from the upstream source.