Skill in 《Query Loop Implementation》

Skill Description

Implement a production-ready LLM query loop for AI applications: tool calling, structured tool_result feedback, ReAct-style cycles, max-turn exits, permission checks, timeouts, budgets, and fatal-error paths.

Skill.md

Query Loop Implementation

Most AI products start with a single model call. The moment you add tools, that is no longer enough. The model must be able to ask for a tool, receive the result, reason over the observation, and either call another tool or produce a final answer.

This skill turns that pattern into product infrastructure.

Core Architecture

Use three boundaries:

ConversationManager Owns durable state: session id, persisted messages, user settings, auth context, usage, and budget.

QueryLoop Owns one task turn: call the model, detect tool calls, execute tools, append tool results, and decide whether to continue or stop.

ToolRuntime Owns registered tools: schemas, validation, permission checks, execution, error formatting, and result size limits.

Minimal Loop

The first production version should be deliberately narrow:

for (let turn = 1; turn <= maxTurns; turn++) {
  const response = await model.generate({ messages, tools })
  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 after tool use because the loop calls the model again with the tool_result messages appended.

Safety Requirements

  • Require maxTurns.
  • Support abort signals and request timeouts.
  • Validate every model-produced tool input against a schema.
  • Check permission before any side effect.
  • Track token, cost, and runtime budgets.
  • Size-limit tool output before appending it to history.
  • Return recoverable tool errors as tool results.
  • Stop on permission denial, budget exhaustion, repeated failure, or fatal tool errors.

When to Use

Use this skill when implementing or reviewing:

  • Tool calling in a chat or workflow product
  • Function-calling loops
  • ReAct-style reasoning-action-observation cycles
  • Query engines or agent runtimes
  • Claude Code-like agent loop behavior
  • Tool result feedback and continuation logic
  • Guardrails for max turns, timeouts, budgets, and permissions

What It Deliberately Leaves Out

This skill does not design context-window management. Keep trimming, retrieval, summarization, and compaction in a separate layer. The query loop should accept messages as input, return updated messages, and remain focused on deterministic control flow.

Install & Use

Install command

npx skills add simbajigege/book2skills/skills/query-loop-implementation
OR

Direct download

Related Skills

Tool Permission System AI Skill

Design a configurable, layered permission/safety system for agent tools: one permission pipeline decides allow / ask / deny across enterprise/user/project scopes.

LangChain Tool Builder AI Skill

Build LangChain (Python) tools using Claude Code's fail-closed design pattern — unified name, schema, security, and e...

Agent Memory Implementation Guide AI Skill

Restructures a bloated or chaotic MEMORY.md into the 2-layer architecture Claude Code's autoDream service uses intern...

Compact Memory Implementation Guide AI Skill

A developer implementation guide for adding compact memory to an Agent built with the Anthropic API or Claude Agent S...

Harness Step 3 — Session State Management AI Skill

Creates init.sh (environment health check), tasks.json (structured task queue), and progress.md (append-only session ...

Compact with Memory AI Skill

Executes /compact correctly — generates a high-quality conversation summary that preserves reasoning, decisions, and ...

Harness Step 1 — Create AGENTS.md & Knowledge Base AI Skill

Scans your codebase and generates AGENTS.md (the agent's orientation file) plus a complete docs/ knowledge base skele...

Harness Step 2 — Fill docs/ with Real Content AI Skill

Deep-reads your codebase and fills every docs/ file with specific, accurate content grounded in what the code actuall...

Session Dream AI Skill

On-demand session memory distillation — extracts key decisions, eliminated approaches, new discoveries, and current b...

Poor Charlie's Almanack AI Skill

Transform Charlie Munger's latticework of mental models and worldly wisdom into an AI analysis tool for investments, ...

Creating Superfans AI Skill

Apply Brittany Hodak's SUPER Model from Creating Superfans to turn ordinary customers into advocates through story, c...