iterative-retrieval
Official逐步优化上下文检索以解决子代理上下文问题的模式
What this skill does
When applied, it prepends a system prompt before your request is sent — no extra calls and no change to how you are billed beyond the added tokens.
---
name: iterative-retrieval
description: 逐步优化上下文检索以解决子代理上下文问题的模式
origin: ECC
---
# 迭代检索模式
解决多智能体工作流中的“上下文问题”,即子智能体在开始工作前不知道需要哪些上下文。
## 何时激活
* 当需要生成需要代码库上下文但无法预先预测的子代理时
* 构建需要逐步完善上下文的多代理工作流时
* 在代理任务中遇到"上下文过大"或"缺少上下文"的失败时
* 为代码探索设计类似 RAG 的检索管道时
* 在代理编排中优化令牌使用时
## 问题
子智能体被生成时上下文有限。它们不知道:
* 哪些文件包含相关代码
* 代码库中存在哪些模式
* 项目使用什么术语
标准方法会失败:
* **发送所有内容**:超出上下文限制
* **不发送任何内容**:智能体缺乏关键信息
* **猜测所需内容**:经常出错
## 解决方案:迭代检索
一个逐步优化上下文的 4 阶段循环:
```
┌─────────────────────────────────────────────┐
│ │
│ ┌──────────┐ ┌──────────┐ │
│ │ 调度 │─────│ 评估 │ │
│ └──────────┘ └──────────┘ │
│ ▲ │ │
│ │ ▼ │
│ ┌──────────┐ ┌──────────┐ │
│ │ 循环 │─────│ 优化 │ │
│ └──────────┘ └──────────┘ │
│ │
│ 最多3次循环,然后继续 │
└─────────────────────────────────────────────┘
```
### 阶段 1:调度
初始的广泛查询以收集候选文件:
```javascript
// Start with high-level intent
const initialQuery = {
patterns: ['src/**/*.ts', 'lib/**/*.ts'],
keywords: ['authentication', 'user', 'session'],
excludes: ['*.test.ts', '*.spec.ts']
};
// Dispatch to retrieval agent
const candidates = await retrieveFiles(initialQuery);
```
### 阶段 2:评估
评估检索到的内容的相关性:
```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:优化
根据评估结果更新搜索条件:
```javascript
function refineQuery(evaluation, previousQuery) {
return {
// Add new patterns discovered in high-relevance files
patterns: [...previousQuery.patterns, ..Use this skill
Add a "skill" field with the skill’s ID to your chat completion request. It is applied server-side before your prompt is sent — no extra calls.
{
"model": "gpt-4o-mini",
"skill": "imp-7b9ad757-689e-4820-b4a0-e20fd24c6871",
"messages": [{ "role": "user", "content": "…" }]
}Install the skill, enable it in your dashboard and (optionally) limit it to specific models. It then applies automatically to every matching request — with no "skill" field to send each time.
Set it up in your dashboardMore skills
Set up and use 1Password CLI for sign-in, desktop integration, and reading or injecting secrets.
Create, view, edit, delete, search, move, or export Apple Notes via the memo CLI on macOS.
List, add, edit, complete, or delete Apple Reminders and reminder lists via remindctl.
Create, search, and manage Bear notes via grizzly CLI.
Monitor blogs and RSS/Atom feeds for updates using the blogwatcher CLI.
BluOS CLI (blu) for discovery, playback, grouping, and volume.
Capture frames or clips from RTSP/ONVIF cameras.
Search, install, update, sync, or publish agent skills with the ClawHub CLI and registry.