Getting it into your agent
One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.
npx agentmods add skills/simbajigege/book2skills/query-loop-implementationnpx skills add simbajigege/book2skills --skill query-loop-implementationgit clone --depth 1 https://github.com/simbajigege/book2skillsWrote this? Show the measurements
A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.
[](https://agentmods.dev/skills/simbajigege/book2skills/query-loop-implementation)<a href="https://agentmods.dev/skills/simbajigege/book2skills/query-loop-implementation"><img src="https://agentmods.dev/badge/skills/simbajigege/book2skills/query-loop-implementation.svg" alt="Measured on agentmods" height="20"></a>What it costs to keep this loaded
Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.
| Model | Per session | Once invoked |
|---|---|---|
| Fable 5.1 | $0.00075 | $0.01241 |
| Opus 5 | $0.00037 | $0.00620 |
| Sonnet 5 | $0.00015 | $0.00248 |
| Haiku 4.5 | $0.00007 | $0.00124 |
Grade A, and why
query-loop-implementation scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 6d ago.
A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.
Nothing flagged
None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.
How it starts
The opening of the file, as written. The whole thing — 171 lines — stays where its author put it; the contents beside it link to each section on GitHub.
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
messagesas loop input and return updatedmessages, 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 <= maxTurns; turn++) {
if (signal?.aborted) return { status: "aborted", messages }
const response = await model.generate({
messages,
tools: tools.definitions(),
signal,
})
messages.push(response.message)
const toolCalls = extractToolCalls(response.message)
if (toolCalls.length === 0) {
return {
status: "completed",
finalMessage: response.message,
messages,
}
}
for (const call of toolCalls) {
const result = await tools.execute(call, { signal, messages })
messages.push(makeToolResultMessage(call.id, result))
}
}
return { status: "max_turns", messages }
}
What ships with it
5 files beside SKILL.md in the same directory: the scripts, references and assets a skill reads on demand. Not counted in the per-session cost; read them before you install if any of them is executable.
What this file has done since we first saw it
Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.
- 6d ago First seen · 171 lines · 75 tokens per session scan A cf6555ee5332
query-loop-implementation is a skill published in the GitHub repository simbajigege/book2skills (156 stars, last pushed 10d ago), licensed MIT. It adds 75 tokens to every session and 1,241 once invoked, about $0.0004 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.
Other skills, from other repositories
skill-check
Validate Claude Code skills against the agentskills specification. Catches structural, semantic, and naming issues before users do.
llm-app-patterns
Production-ready patterns for building LLM applications. Covers RAG pipelines, agent architectures, prompt IDEs, and LLMOps monitoring. Use when designing AI applications, implementing RAG, building agents, or setting up LLM observability.
langsmith-observability
LLM observability platform for tracing, evaluation, and monitoring. Use when debugging LLM applications, evaluating model outputs against datasets, monitoring production systems, or building systematic testing pipelines for AI applications.
crewai
Expert in CrewAI - the leading role-based multi-agent framework used by 60% of Fortune 500 companies. Covers agent design with roles and goals, task definition, crew orchestration, process types (sequential, hierarchical, parallel), memory systems, and flows for complex workflows. Essential for building collaborative…
research-engineer
An uncompromising Academic Research Engineer. Operates with absolute scientific rigor, objective criticism, and zero flair. Focuses on theoretical correctness, formal verification, and optimal implementation across any required technology.
context7-auto-research
Automatically fetch latest library/framework documentation for Claude Code via Context7 API.