Agent skill

LLM Query Loop Implementation

by simbajigege in simbajigege/book2skills

Implements a production-style agent loop in your own AI product, with tool calling, tool results fed back, exit conditions and budget guards.

Apache-2.0Auto-check passedAI & LLM Engineering

Install LLM Query Loop Implementation

skills CLI
$ npx skills add simbajigege/book2skills --skill query-loop-implementation -a claude-code

Project install by default; add -g for ~/.claude/skills/.

GitHub CLI
$ gh skill install simbajigege/book2skills query-loop-implementation --agent claude-code

Project scope by default; add --scope user for a personal install. Needs GitHub CLI 2.90.0 or later (public preview).

Manual copy
$ git clone --depth 1 https://github.com/simbajigege/book2skills.git skills-src && mkdir -p .claude/skills && cp -r skills-src/skills/query-loop-implementation .claude/skills/query-loop-implementation && rm -rf skills-src

Use ~/.claude/skills/ instead of .claude/skills for a personal install. The folder must contain SKILL.md.

Claude Code skills documentation · loads skills from .claude/skills/

Facts

Skill name
query-loop-implementation
GitHub stars
184
Token cost
~1.4k tokens
SKILL.md length
473 words
Files
6 (incl. references)
Skills in repo
37
Repo updated
First seen
Licence
Apache-2.0

At a glance

Implements a production-style agent loop in your own AI product, with tool calling, tool results fed back, exit conditions and budget guards.

  • Works in 6 steps: Inspect the user's stack and current LLM… → Introduce a minimal query loop. → Normalize message shapes. → …
  • Adding tool calling to a product that currently makes single LLM calls
  • SKILL.md covers Core Idea, Implementation Workflow, Minimal Loop and Required Exit Conditions, plus 3 more sections
  • Instructions only: no scripts, shell commands, URLs or credentials in SKILL.md

What it does

Treating the agent loop as product infrastructure rather than prompt glue, the skill splits it into three parts: a `ConversationManager` that owns durable state, a `QueryLoop` that runs one task turn by calling the model, detecting tool calls, executing them and appending results, and a `ToolRuntime` that owns tool schemas, permission checks, execution and error formatting. ReAct is the mental model, but traffic should use the provider's structured tool-call format.

The workflow inspects your stack and current LLM call site, introduces a minimal loop with one model call and one tool registry, normalizes message shapes, then adds tool-input validation, permission checks and logging. Exit and budget guards come before extra features: normal completion, `maxTurns`, abort or timeout, permission denied, and token or cost checks. Context trimming and summarization are left to a separate layer. A `references/query-loop-patterns.md` file supplements the main text.

When your agent uses it

  • Adding tool calling to a product that currently makes single LLM calls
  • Building a ReAct-style reasoning, action and observation cycle
  • Putting max-turn, timeout and budget limits on an existing agent loop
  • Designing an agent runtime similar to Claude Code's loop for your own codebase

Example prompts

  • “Add a tool-calling loop to our support chatbot with a turn limit and a cost budget.”
  • “Refactor our function-calling code into a QueryLoop and a ToolRuntime.”
  • “Make our agent stop cleanly when the user cancels or the request times out.”

Workflow steps

6 steps, taken from the first numbered list in SKILL.md.

  1. Inspect the user's stack and current LLM call site.
  2. Introduce a minimal query loop.
  3. Normalize message shapes.
  4. Add tool execution safety.
  5. Add exit and budget guards before expanding features.
  6. Keep context-window strategy outside this skill.

What it can do on your machine

Read from SKILL.md and the folder at commit e5ba66c. It shows what the files ask for, not the result of running them.

  • Tool permissions

    Pre-approves nothing: there is no allowed-tools line, so your agent's usual permission prompts apply.

    From allowed-tools in the SKILL.md frontmatter.

  • Runs code

    No scripts in the folder and no shell commands in SKILL.md (its code samples are typescript).

    From the folder's file list and the shell code blocks in SKILL.md.

  • Network

    No URLs in SKILL.md.

    From URLs in SKILL.md, links to its own repository left out.

  • Credentials

    Names no API keys, tokens, secrets or passwords.

    From names ending in _API_KEY, _TOKEN, _SECRET, _KEY or _PASSWORD in SKILL.md.

Context cost

LLM Query Loop Implementation loads about 1.4k tokens when it runs, and up to ~2k if it reads all its reference files. Until then it costs about 95 tokens; SKILL.md has 473 words of instructions outside code blocks.

Always · name and description, kept in context so the agent knows when to use it
~95
When it runs · the whole SKILL.md, loaded when a task matches
~1.4k
With references · SKILL.md plus every file in references/, read only if the agent opens them
~2k

Estimates: characters ÷ 4, the usual rule of thumb; real counts depend on the model's tokenizer. Scripts and assets cost tokens only if the agent reads them.

Safety

Auto-check passed

The automated check found no risky patterns in SKILL.md.

Automated static check — not a guarantee. Review scripts before installing. It scans the text of SKILL.md for risky patterns (piping downloads into a shell, reading credential files, hidden Unicode, destructive commands); files beside SKILL.md are not scanned.

SKILL.md

The full file from simbajigege/book2skills at commit e5ba66c, republished under its Apache-2.0 licence (© simbajigege). 473 words, ~1,418 tokens.

Download SKILL.mdSave it as .claude/skills/query-loop-implementation/SKILL.md (or your agent's skills folder). This skill also uses 5 other files; get the full folder from GitHub.
name
query-loop-implementation
description
Implement a production-ready LLM query loop / agent loop for AI applications. Use this skill whenever the user wants to add tool calling, ReAct-style reasoning-action-observation cycles, function calling loops, query engines, agent runtimes, tool_result feedback, max-turn exits, or Claude Code-like Agent Loop behavior to their own product or codebase.

Query Loop Implementation

Core Idea

Build the loop as product infrastructure, not prompt glue:

text
ConversationManager -> QueryLoop -> ToolRuntime
  • ConversationManager owns durable state: session id, messages, user settings, budget, persistence.
  • QueryLoop owns one task turn: call model, detect tool calls, execute tools, append tool results, repeat.
  • ToolRuntime owns registered tools: schemas, permission checks, execution, error formatting.

Use ReAct as the mental model:

text
Thought -> Action -> Observation -> Thought -> Answer

Implement it as structured API traffic:

text
model thinking/text -> tool_call -> tool_result -> next model call -> final text

Implementation Workflow

  1. 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.

  2. Introduce a minimal query loop. Keep the first version narrow: one model call function, one tool registry, explicit exit conditions.

  3. 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.

  4. 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.

  5. Add exit and budget guards before expanding features. Always include maxTurns, timeout/cancel support, token/cost budget checks, and a fatal-error path.

  6. 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:

ts
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 }
}

The "model continues judging" step is not a separate function. It happens when the loop calls the model again after appending tool_result messages.

Show full SKILL.md (227 more words)Show less

Required Exit Conditions

Implement these before shipping:

  • Normal completion: the model response has no tool calls.
  • Max turns: stop after a fixed number of model-tool cycles.
  • Abort/timeout: stop when the user cancels or the request exceeds its deadline.
  • Permission denied: stop or return a tool error depending on the product's safety policy.
  • Budget exceeded: stop when token, cost, or runtime budget is exhausted.
  • Fatal tool error: stop when a tool failure cannot be corrected by the model.

Prefer returning structured terminal reasons:

ts
type TerminalReason =
  | "completed"
  | "max_turns"
  | "aborted"
  | "timeout"
  | "permission_denied"
  | "budget_exceeded"
  | "fatal_tool_error"

Tool Runtime Contract

Each tool should define:

ts
type Tool = {
  name: string
  description: string
  inputSchema: unknown
  risk: "read" | "write" | "execute" | "external"
  validate(input: unknown): ValidatedInput
  canUse(input: ValidatedInput, ctx: ToolContext): Promise<PermissionDecision>
  call(input: ValidatedInput, ctx: ToolContext): Promise<ToolResult>
}

Execution order:

text
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.

© simbajigege, Apache-2.0. Rendered from Markdown: HTML in the file is shown as text, images as links, and headings moved down two levels. Raw file

Files

SKILL.md and 5 other files (references) in skills/query-loop-implementation of simbajigege/book2skills.

  • SKILL.md
  • LICENSE
  • README.md
  • agents/openai.yaml
  • query-loop-implementation.zip
  • references/query-loop-patterns.md

Open the folder on GitHubat commit e5ba66c

Compare with similar skills

LLM Query Loop Implementation next to the 5 skills that share the most tags, products or categories with it. Stars are the repository's; “used in” counts other GitHub owners with a copy.

LLM Query Loop Implementation compared with similar skills
SkillStarsUsed inTokensAuto-checkLicenceRepo updated
LLM Query Loop Implementation this skillsimbajigege/book2skills184—~1.4kAutomated safety check: PassApache-2.0
AI SDKvercel-labs/ai-facts16821 repos~1.2kAutomated safety check: PassNone
Building Pydantic AI Agentsdocling-project/docling68k—~2.8kAutomated safety check: PassMIT
Building Pydantic AI Agentspydantic/pydantic-ai20k—~8kAutomated safety check: PassMIT
Google Agents CLI Adk Codepifferologo/cloud-agents-cli1291 repos~768Automated safety check: PassApache-2.0
LangGraph Decision Modelslangchain-ai/langchain-skills1.3k—~2.3kAutomated safety check: PassMIT

Similar skills

  • AI SDK

    vercel-labs/ai-facts

    Official

    Answer questions about the AI SDK and help build AI-powered features.

    168 GitHub starsUsed in 21 repos~1.2k tokens
    AI & LLM EngineeringAuto-check passed
  • Building Pydantic AI Agents

    docling-project/docling

    Patterns and tested examples for building agents with Pydantic AI: tools, capabilities, structured output, dependency injection, hooks, YAML specs, streaming and testing.

    68k GitHub stars~2.8k tokensUpdated today
    AI & LLM EngineeringAuto-check passed
  • Building Pydantic AI Agents

    pydantic/pydantic-ai

    Official

    Build AI agents with Pydantic AI — tools, capabilities (including on-demand loading), workspaces, structured output, streaming, testing, and multi-agent patterns.

    20k GitHub stars~8k tokensUpdated today
    AI & LLM EngineeringAuto-check passed
  • Google Agents CLI Adk Code

    pifferologo/cloud-agents-cli

    This skill should be used when the user wants to "write agent code", "build an agent with ADK", "add a tool", "create a callback", "define an agent", "use state management", or needs ADK (Agent…

    129 GitHub starsUsed in 1 repo~768 tokens
    AI & LLM EngineeringAuto-check passed
  • LangGraph Decision Models

    langchain-ai/langchain-skills

    Official

    Routes LangGraph agents with typed decision models that return probabilities, and finds LLM calls that only exist to produce a routing decision.

    1.3k GitHub stars~2.3k tokensUpdated yesterday
    AI & LLM EngineeringAuto-check passed
  • Claude Code Clawdbot

    win4r/claude-code-clawdbot-skill

    Run Claude Code (Anthropic) from this host via the claude CLI (Agent SDK) in headless mode (-p) for codebase analysis, refactors, test fixing, and structured output.

    123 GitHub stars~2.5k tokensUpdated 8 mo ago
    AI & LLM EngineeringAuto-check: warnings

More from simbajigege/book2skills

All 37 skills in this repo
  • MEMORY.md Restructuring

    simbajigege/book2skills

    Reorganizes an overgrown MEMORY.md into a short pointer index plus separate topic files, and fixes or deletes outdated memories instead of archiving them.

    184 GitHub stars~2.4k tokensUpdated 1 mo ago
    Auto-check passed
  • Compact Memory Implementation

    simbajigege/book2skills

    A developer guide to adding compact memory to an agent: when to trigger compaction, how to fork a compactor sub-agent, what the summary holds, and how to restore it.

    184 GitHub stars~2.5k tokensUpdated 1 mo ago
    Auto-check passed
  • Semantic Line-Art SVG Diagrams

    simbajigege/book2skills

    Turns text, screenshots, or existing diagrams into minimal, accessible line-art SVGs for teaching material, with an optional Mermaid relationship spec.

    184 GitHub stars~2.5k tokensUpdated 1 mo ago
    Auto-check passed
  • Fail-Closed Agent Tool Builder

    simbajigege/book2skills

    Helps define agent tools with a fail-closed pattern: one class holding name, schema, security flags and a validate, permission and call execution chain.

    184 GitHub stars~2k tokensUpdated 1 mo ago
    Auto-check passed
  • Analyzing Financial Reports

    simbajigege/book2skills

    Analyzes Chinese public company financial statements (balance sheet, income statement, cash flow) to assess asset quality, profit authenticity, cash flow health, solvency, and overall investment…

    184 GitHub starsUsed in 1 repo~930 tokens
    Auto-check passed
  • Tool Permission System Design

    simbajigege/book2skills

    Guides designing a layered permission pipeline for agent tools that decides which calls are allowed, need confirmation or are denied, with scopes and hooks.

    184 GitHub stars~2.1k tokensUpdated 1 mo ago
    Auto-check passed

Questions about LLM Query Loop Implementation

What does LLM Query Loop Implementation do?

Implements a production-style agent loop in your own AI product, with tool calling, tool results fed back, exit conditions and budget guards. Treating the agent loop as product infrastructure rather than prompt glue, the skill splits it into three parts: a `ConversationManager` that owns durable state, a `QueryLoop` that runs one task turn by calling the model, detecting tool calls, executing them and appending results, and a `ToolRuntime` that owns tool schemas, permission checks, execution and error formatting. ReAct is the mental model, but traffic should use the provider's structured tool-call format.

When should I use LLM Query Loop Implementation?

LLM Query Loop Implementation fits situations like: adding tool calling to a product that currently makes single LLM calls; building a ReAct-style reasoning, action and observation cycle; putting max-turn, timeout and budget limits on an existing agent loop; designing an agent runtime similar to Claude Code's loop for your own codebase.

How do I install LLM Query Loop Implementation in Claude Code?

Run `npx skills add simbajigege/book2skills --skill query-loop-implementation -a claude-code`. Or copy the skill folder (skills/query-loop-implementation in simbajigege/book2skills) into .claude/skills/query-loop-implementation in your project. Claude Code loads it when a task matches its description.

How do I install LLM Query Loop Implementation in Codex?

Run `npx skills add simbajigege/book2skills --skill query-loop-implementation -a codex`. Or copy the skill folder (skills/query-loop-implementation in simbajigege/book2skills) into .agents/skills/query-loop-implementation in your project. Codex loads it when a task matches its description.

Can I use LLM Query Loop Implementation in Cursor, Gemini CLI or GitHub Copilot?

Cursor, Gemini CLI, GitHub Copilot and OpenCode also load SKILL.md folders. With the skills CLI, run `npx skills add simbajigege/book2skills --skill query-loop-implementation -a cursor` (or -a gemini-cli, github-copilot or opencode for the others). To copy it by hand, put the folder in .cursor/skills/query-loop-implementation, .gemini/skills/query-loop-implementation, .github/skills/query-loop-implementation and .opencode/skills/query-loop-implementation in your project.

What does LLM Query Loop Implementation need to run?

SKILL.md names no scripts, command-line tools or credentials: LLM Query Loop Implementation is instructions for the agent only.

Does LLM Query Loop Implementation access the network?

SKILL.md contains no URLs. Any network use would come from the scripts or tools the agent runs. This is read from the text; nothing was executed.

Is LLM Query Loop Implementation safe to install?

Our automated static check of SKILL.md found no risky patterns, such as piping downloads into a shell, reading credential files or hidden Unicode. It is not a guarantee. Review the folder before installing.

What licence does LLM Query Loop Implementation use?

LLM Query Loop Implementation is published under the Apache-2.0 licence (from the LICENSE file in the skill folder). It allows redistribution, so the full SKILL.md is shown on this page.

How many tokens does LLM Query Loop Implementation use?

About 1.4k tokens (SKILL.md is roughly 5.7k characters). Agents keep only the skill's name and description in context until a task matches; then they load SKILL.md in full. Its references folder adds about 623 tokens, read only when the agent opens those files.

What are the alternatives to LLM Query Loop Implementation?

Skills that share tags, products or a category with LLM Query Loop Implementation: AI SDK (vercel-labs/ai-facts, 168 stars), Building Pydantic AI Agents (docling-project/docling, 68k stars), Building Pydantic AI Agents (pydantic/pydantic-ai, 20k stars) and Google Agents CLI Adk Code (pifferologo/cloud-agents-cli, 129 stars). The comparison table on this page puts their stars, adoption, token cost, safety result and licence side by side.

Who maintains LLM Query Loop Implementation?

simbajigege (a GitHub user) maintains it in simbajigege/book2skills, which has 184 GitHub stars. The repository holds 37 skills in this directory. The repository was last updated on August 26, 2026.

Source: simbajigege/book2skills on GitHub. Facts on this page come from the repository at the commit we read; the author's words are quoted as theirs.