AI SDK
vercel-labs/ai-facts
Answer questions about the AI SDK and help build AI-powered features.
Implements a production-style agent loop in your own AI product, with tool calling, tool results fed back, exit conditions and budget guards.
$ npx skills add simbajigege/book2skills --skill query-loop-implementation -a claude-codeProject install by default; add -g for ~/.claude/skills/.
$ gh skill install simbajigege/book2skills query-loop-implementation --agent claude-codeProject scope by default; add --scope user for a personal install. Needs GitHub CLI 2.90.0 or later (public preview).
$ 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-srcUse ~/.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/
Install the "query-loop-implementation" agent skill from https://github.com/simbajigege/book2skills/tree/main/skills/query-loop-implementation into .claude/skills/query-loop-implementation/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "query-loop-implementation", then confirm the skill loads.Claude Code copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$skill-installer install https://github.com/simbajigege/book2skills/tree/main/skills/query-loop-implementationType this inside Codex. $skill-installer <name> installs a curated skill from openai/skills. The installer writes to $CODEX_HOME/skills (default ~/.codex/skills). Restart Codex if the skill does not show up.
$ npx skills add simbajigege/book2skills --skill query-loop-implementation -a codexProject install goes to .agents/skills/; add -g for ~/.codex/skills/.
$ gh skill install simbajigege/book2skills query-loop-implementation --agent codexProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/simbajigege/book2skills.git skills-src && mkdir -p .agents/skills && cp -r skills-src/skills/query-loop-implementation .agents/skills/query-loop-implementation && rm -rf skills-srcUse ~/.agents/skills/ instead of .agents/skills for a personal install.
Codex skills documentation · loads skills from .agents/skills/
Install the "query-loop-implementation" agent skill from https://github.com/simbajigege/book2skills/tree/main/skills/query-loop-implementation into .agents/skills/query-loop-implementation/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "query-loop-implementation", then confirm the skill loads.Codex copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ npx skills add simbajigege/book2skills --skill query-loop-implementation -a cursorProject install goes to .agents/skills/; add -g for ~/.cursor/skills/.
$ gh skill install simbajigege/book2skills query-loop-implementation --agent cursorProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/simbajigege/book2skills.git skills-src && mkdir -p .cursor/skills && cp -r skills-src/skills/query-loop-implementation .cursor/skills/query-loop-implementation && rm -rf skills-srcUse ~/.cursor/skills/ instead of .cursor/skills for a personal install.
Cursor skills documentation · loads skills from .cursor/skills/, .agents/skills/, .claude/skills/, .codex/skills/
Install the "query-loop-implementation" agent skill from https://github.com/simbajigege/book2skills/tree/main/skills/query-loop-implementation into .cursor/skills/query-loop-implementation/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "query-loop-implementation", then confirm the skill loads.Cursor copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ gemini skills install https://github.com/simbajigege/book2skills.git --path skills/query-loop-implementation--scope user (default) or --scope workspace; --path is the subfolder of the repo that holds the skill; --consent skips the security confirmation prompt.
$ npx skills add simbajigege/book2skills --skill query-loop-implementation -a gemini-cliProject install goes to .agents/skills/; add -g for ~/.gemini/skills/.
$ gh skill install simbajigege/book2skills query-loop-implementation --agent gemini-cliProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/simbajigege/book2skills.git skills-src && mkdir -p .gemini/skills && cp -r skills-src/skills/query-loop-implementation .gemini/skills/query-loop-implementation && rm -rf skills-srcUse ~/.gemini/skills/ instead of .gemini/skills for a personal install, then run /skills reload.
Gemini CLI skills documentation · loads skills from .gemini/skills/, .agents/skills/
Install the "query-loop-implementation" agent skill from https://github.com/simbajigege/book2skills/tree/main/skills/query-loop-implementation into .gemini/skills/query-loop-implementation/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "query-loop-implementation", then confirm the skill loads.Gemini CLI copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ gh skill install simbajigege/book2skills query-loop-implementationInstalls for Copilot at project scope by default; add --scope user for a personal install. Preview a skill first with gh skill preview. Needs GitHub CLI 2.90.0 or later (public preview).
$ npx skills add simbajigege/book2skills --skill query-loop-implementation -a github-copilotProject install goes to .agents/skills/; add -g for ~/.copilot/skills/.
$ git clone --depth 1 https://github.com/simbajigege/book2skills.git skills-src && mkdir -p .github/skills && cp -r skills-src/skills/query-loop-implementation .github/skills/query-loop-implementation && rm -rf skills-srcUse ~/.copilot/skills/ instead of .github/skills for a personal install. Commit .github/skills so cloud agent and code review can use it.
GitHub Copilot skills documentation · loads skills from .github/skills/, .claude/skills/, .agents/skills/
Install the "query-loop-implementation" agent skill from https://github.com/simbajigege/book2skills/tree/main/skills/query-loop-implementation into .github/skills/query-loop-implementation/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "query-loop-implementation", then confirm the skill loads.GitHub Copilot copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ npx skills add simbajigege/book2skills --skill query-loop-implementation -a opencodeOpenCode documents no install command of its own. Project install goes to .agents/skills/; add -g for ~/.config/opencode/skills/.
$ gh skill install simbajigege/book2skills query-loop-implementation --agent opencodeProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/simbajigege/book2skills.git skills-src && mkdir -p .opencode/skills && cp -r skills-src/skills/query-loop-implementation .opencode/skills/query-loop-implementation && rm -rf skills-srcUse ~/.config/opencode/skills/ instead of .opencode/skills for a personal install.
OpenCode skills documentation · loads skills from .opencode/skills/, .claude/skills/, .agents/skills/
Install the "query-loop-implementation" agent skill from https://github.com/simbajigege/book2skills/tree/main/skills/query-loop-implementation into .opencode/skills/query-loop-implementation/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "query-loop-implementation", then confirm the skill loads.OpenCode copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
query-loop-implementationImplements 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.
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.
6 steps, taken from the first numbered list in SKILL.md.
Read from SKILL.md and the folder at commit e5ba66c. It shows what the files ask for, not the result of running them.
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.
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.
No URLs in SKILL.md.
From URLs in SKILL.md, links to its own repository left out.
Names no API keys, tokens, secrets or passwords.
From names ending in _API_KEY, _TOKEN, _SECRET, _KEY or _PASSWORD in SKILL.md.
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.
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.
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.
The full file from simbajigege/book2skills at commit e5ba66c, republished under its Apache-2.0 licence (© simbajigege). 473 words, ~1,418 tokens.
.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.Build the loop as product infrastructure, not prompt glue:
ConversationManager -> QueryLoop -> ToolRuntimeConversationManager 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:
Thought -> Action -> Observation -> Thought -> AnswerImplement it as structured API traffic:
model thinking/text -> tool_call -> tool_result -> next model call -> final textInspect 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.
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 }
}The "model continues judging" step is not a separate function. It happens when the loop calls the model again after appending tool_result messages.
Implement these before shipping:
Prefer returning structured terminal reasons:
type TerminalReason =
| "completed"
| "max_turns"
| "aborted"
| "timeout"
| "permission_denied"
| "budget_exceeded"
| "fatal_tool_error"Each tool should define:
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:
find tool by name
-> schema validate model input
-> run tool-specific validation
-> check permission
-> call tool
-> format success or error as tool_resultReturn 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.
Keep "intelligence" in the model and "reliability" in code:
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.
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
SKILL.md and 5 other files (references) in skills/query-loop-implementation of simbajigege/book2skills.
Open the folder on GitHubat commit e5ba66c
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.
| Skill | Stars | Used in | Tokens | Auto-check | Licence | Repo updated |
|---|---|---|---|---|---|---|
| LLM Query Loop Implementation this skillsimbajigege/book2skills | 184 | — | ~1.4k | Automated safety check: Pass | Apache-2.0 | |
| AI SDKvercel-labs/ai-facts | 168 | 21 repos | ~1.2k | Automated safety check: Pass | None | |
| Building Pydantic AI Agentsdocling-project/docling | 68k | — | ~2.8k | Automated safety check: Pass | MIT | |
| Building Pydantic AI Agentspydantic/pydantic-ai | 20k | — | ~8k | Automated safety check: Pass | MIT | |
| Google Agents CLI Adk Codepifferologo/cloud-agents-cli | 129 | 1 repos | ~768 | Automated safety check: Pass | Apache-2.0 | |
| LangGraph Decision Modelslangchain-ai/langchain-skills | 1.3k | — | ~2.3k | Automated safety check: Pass | MIT |
vercel-labs/ai-facts
Answer questions about the AI SDK and help build AI-powered features.
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.
pydantic/pydantic-ai
Build AI agents with Pydantic AI — tools, capabilities (including on-demand loading), workspaces, structured output, streaming, testing, and multi-agent patterns.
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…
langchain-ai/langchain-skills
Routes LangGraph agents with typed decision models that return probabilities, and finds LLM calls that only exist to produce a routing decision.
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.
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.
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.
simbajigege/book2skills
Turns text, screenshots, or existing diagrams into minimal, accessible line-art SVGs for teaching material, with an optional Mermaid relationship spec.
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.
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…
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.
Categories
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.
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.
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.
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.
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.
SKILL.md names no scripts, command-line tools or credentials: LLM Query Loop Implementation is instructions for the agent only.
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.
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.
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.
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.
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.
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.