Iterative Retrieval logo

Iterative Retrieval

Community
virgo777
iterative-retrieval

逐步优化上下文检索以解决子智能体(subagent)上下文问题的模式。

Overview

Publishervirgo777
Repositorybuddyme
Skill nameiterative-retrieval
Stars
62
Forks
8
Bundled files
Instructions only
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • Self-contained

    Everything the model needs lives in the instructions — no extra files to sync.

  • Open source

    Published by virgo777 on GitHub. Read the source before you install it.

Installation

Install the Iterative Retrieval AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/virgo777/buddyme/tree/master/buddyMe/skill_library/skills/iterative-retrieval
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/virgo777/buddyme.git /tmp/buddyme
mkdir -p .claude/skills
cp -r /tmp/buddyme/buddyMe/skill_library/skills/iterative-retrieval .claude/skills/iterative-retrieval
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Iterative Retrieval in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Iterative Retrieval on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Iterative Retrieval is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

迭代检索模式(Iterative Retrieval Pattern)

解决了多智能体工作流(multi-agent workflows)中的“上下文问题”——子智能体(subagent)在开始工作前往往不知道自己需要哪些上下文。

何时激活

  • 生成需要事先无法完全预测的代码库上下文(codebase context)的子智能体时
  • 构建上下文需要逐步优化的多智能体工作流时
  • 在智能体任务中遇到“上下文过大”或“缺失上下文”的失败时
  • 为代码探索设计类 RAG 的检索流水线时
  • 优化智能体编排(agent orchestration)中的 Token 使用时

问题背景(The Problem)

生成的子智能体通常只带有有限的上下文。它们并不清楚:

  • 哪些文件包含相关的代码
  • 代码库中存在哪些模式(patterns)
  • 项目使用了哪些术语

标准方法往往会失败:

  • 全部发送:超出上下文限制。
  • 什么都不发:智能体缺少关键信息。
  • 猜测需求:经常猜错。

解决方案:迭代检索(The Solution: Iterative Retrieval)

一个由 4 个阶段组成的循环,用于逐步优化上下文:

┌─────────────────────────────────────────────┐
│                                             │
│   ┌──────────┐      ┌──────────┐            │
│   │   派发   │─────▶│   评估   │            │
│   │ DISPATCH │      │ EVALUATE │            │
│   └──────────┘      └──────────┘            │
│        ▲                  │                 │
│        │                  ▼                 │
│   ┌──────────┐      ┌──────────┐            │
│   │   循环   │◀─────│   优化   │            │
│   │   LOOP   │      │  REFINE  │            │
│   └──────────┘      └──────────┘            │
│                                             │
│        最多 3 个周期,然后继续              │
└─────────────────────────────────────────────┘

阶段 1:派发(DISPATCH)

初始的广泛查询,用于收集候选文件:

javascript
// 从高层意图开始
const initialQuery = {
  patterns: ['src/**/*.ts', 'lib/**/*.ts'],
  keywords: ['authentication', 'user', 'session'],
  excludes: ['*.test.ts', '*.spec.ts']
};

// 派发给检索智能体
const candidates = await retrieveFiles(initialQuery);

阶段 2:评估(EVALUATE)

评估检索到的内容的相关性:

javascript
function evaluateRelevance(files, task) {
  return files.map(file => ({
    path: file.path,
    relevance: scoreRelevance(file.content, task),
    reason: explainRelevance(file.content, task),
    missingContext: identifyGaps(file.content, task)
  }));
}

评分标准:

  • 高 (0.8-1.0):直接实现了目标功能
  • 中 (0.5-0.7):包含相关的模式或类型
  • 低 (0.2-0.4):间接相关
  • 无 (0-0.2):不相关,排除

阶段 3:优化(REFINE)

根据评估结果更新搜索条件:

javascript
function refineQuery(evaluation, previousQuery) {
  return {
    // 添加在高度相关文件中发现的新模式
    patterns: [...previousQuery.patterns, ...extractPatterns(evaluation)],

    // 添加在代码库中发现的术语
    keywords: [...previousQuery.keywords, ...extractKeywords(evaluation)],

    // 排除已确认为不相关的路径
    excludes: [...previousQuery.excludes, ...evaluation
      .filter(e => e.relevance < 0.2)
      .map(e => e.path)
    ],

    // 针对特定的缺口
    focusAreas: evaluation
      .flatMap(e => e.missingContext)
      .filter(unique)
  };
}

阶段 4:循环(LOOP)

使用优化后的条件重复执行(最多 3 个周期):

javascript
async function iterativeRetrieve(task, maxCycles = 3) {
  let query = createInitialQuery(task);
  let bestContext = [];

  for (let cycle = 0; cycle < maxCycles; cycle++) {
    const candidates = await retrieveFiles(query);
    const evaluation = evaluateRelevance(candidates, task);

    // 检查是否已有足够的上下文
    const highRelevance = evaluation.filter(e => e.relevance >= 0.7);
    if (highRelevance.length >= 3 && !hasCriticalGaps(evaluation)) {
      return highRelevance;
    }

    // 优化并继续
    query = refineQuery(evaluation, query);
    bestContext = mergeContext(bestContext, highRelevance);
  }

  return bestContext;
}

实践案例

案例 1:Bug 修复上下文

任务:"修复身份验证令牌过期 bug"

周期 1:
  派发 (DISPATCH):在 src/** 中搜索 "token", "auth", "expiry"
  评估 (EVALUATE):发现 auth.ts (0.9), tokens.ts (0.8), user.ts (0.3)
  优化 (REFINE):添加 "refresh", "jwt" 关键词;排除 user.ts

周期 2:
  派发 (DISPATCH):搜索优化后的术语
  评估 (EVALUATE):发现 session-manager.ts (0.95), jwt-utils.ts (0.85)
  优化 (REFINE):上下文已足够(2 个高度相关文件)

结果:auth.ts, tokens.ts, session-manager.ts, jwt-utils.ts

案例 2:功能实现

任务:"为 API 端点添加速率限制 (rate limiting)"

周期 1:
  派发 (DISPATCH):在 routes/** 中搜索 "rate", "limit", "api"
  评估 (EVALUATE):无匹配项 —— 代码库使用的是 "throttle" 术语
  优化 (REFINE):添加 "throttle", "middleware" 关键词

周期 2:
  派发 (DISPATCH):搜索优化后的术语
  评估 (EVALUATE):发现 throttle.ts (0.9), middleware/index.ts (0.7)
  优化 (REFINE):需要路由器模式

周期 3:
  派发 (DISPATCH):搜索 "router", "express" 模式
  评估 (EVALUATE):发现 router-setup.ts (0.8)
  优化 (REFINE):上下文已足够

结果:throttle.ts, middleware/index.ts, router-setup.ts

与智能体(Agents)集成

在智能体提示词中使用:

markdown
在为此任务检索上下文时:
1. 从广泛的关键词搜索开始
2. 评估每个文件的相关性(0-1 等级)
3. 识别仍然缺失的上下文
4. 优化搜索条件并重复执行(最多 3 个周期)
5. 返回相关性 >= 0.7 的文件

最佳实践

  1. 由广入深,逐步缩小范围 —— 初始查询不要过于具体。
  2. 学习代码库术语 —— 第一个周期通常能揭示命名规范。
  3. 追踪缺失内容 —— 明确的缺口识别是驱动优化的关键。
  4. 见好就收 —— 3 个高度相关的文件优于 10 个平庸的文件。
  5. 果断排除 —— 低相关性的文件通常不会突然变得相关。

相关资源

  • 长篇指南 (The Longform Guide) —— 子智能体编排部分
  • continuous-learning 技能 —— 用于随时间改进的模式
  • ~/.claude/agents/ 中的智能体定义

Frequently asked questions

What does the Iterative Retrieval AI skill do?

逐步优化上下文检索以解决子智能体(subagent)上下文问题的模式。

Why use Iterative Retrieval on TypingMind?

Because you install it once and use it with any model. Iterative Retrieval is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Iterative Retrieval in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/virgo777/buddyme/tree/master/buddyMe/skill_library/skills/iterative-retrieval. TypingMind reads its SKILL.md and installs it as a skill you can enable per chat.

Which AI models can use Iterative Retrieval?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Iterative Retrieval?

As many as you like. As long as a model supports skills, you can use Iterative Retrieval with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Iterative Retrieval AI skill free?

It is published on GitHub by virgo777. Check the repository for licensing terms. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇