with one click
iterative-retrieval
Pattern for progressively refining context retrieval to solve the subagent context problem
Menu
Pattern for progressively refining context retrieval to solve the subagent context problem
ไธบ OpenClaw AI Agent ้ป้ ๅฎๆด็้พ่พ็ต้ญๆนๆกใๆ นๆฎ็จๆทๅๅฅฝๆ้ๆบๆฝๅก๏ผ ่พๅบ่บซไปฝๅฎไฝใ็ต้ญๆ่ฟฐ(SOUL.md)ใ่ง่ฒๅๅบ็บฟ่งๅใๅๅญๅๅคดๅ็ๅพๆ็คบ่ฏใ ๅฆๅฝๅ็ฏๅขๆไพๅทฒๅฎกๆ ธ็็ๅพ skill๏ผๅฏ่ชๅจ็ๆ็ปไธ้ฃๆ ผๅคดๅๅพ็ใ ๅฝ็จๆท้่ฆๅๅปบใ่ฎพ่ฎกๆๅฎๅถ OpenClaw ้พ่พ็ต้ญๆถไฝฟ็จใ ไธ้็จไบ๏ผๅพฎ่ฐๅทฒๆ SOUL.mdใ้ OpenClaw ๅนณๅฐ็่ง่ฒ่ฎพ่ฎกใ็บฏๅทฅๅ ทๅๆ ๆงๆ ผ Agentใ ่งฆๅ่ฏ๏ผ้พ่พ็ต้ญใ่พ้ญใOpenClaw ็ต้ญใๅ ป่พ็ต้ญใ้พ่พ่ง่ฒใ้พ่พๅฎไฝใ ้พ่พๅงๆฌๆ่ง่ฒใ้พ่พๆธธๆ่ง่ฒใ้พ่พ NPCใ้พ่พๆงๆ ผใ้พ่พ่ๆฏๆ ไบใ lobster soulใlobster characterใๆฝๅกใ้ๆบ้พ่พใ้พ่พ SOULใgachaใ
Use when auditing Gemini skills and commands for quality. Supports Quick Scan (changed skills only) and Full Stocktake modes with sequential subagent batch evaluation.
Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol.
Research-before-coding workflow. Search for existing tools, libraries, and patterns before writing custom code. Invokes the researcher agent.
Backend architecture patterns, API design, database optimization, and server-side best practices for Node.js, Express, and Next.js API routes.
Multi-source deep research using firecrawl and exa MCPs. Searches the web, synthesizes findings, and delivers cited reports with source attribution. Use when the user wants thorough research on any topic with evidence and citations.
| name | iterative-retrieval |
| description | Pattern for progressively refining context retrieval to solve the subagent context problem |
Solves the "context problem" in multi-agent workflows where subagents don't know what context they need until they start working.
Subagents are spawned with limited context. They don't know:
Standard approaches fail:
A 4-phase loop that progressively refines context:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โ DISPATCH โโโโโโโถโ EVALUATE โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โฒ โ โ
โ โ โผ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โ LOOP โโโโโโโโ REFINE โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โ
โ Max 3 cycles, then proceed โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Initial broad query to gather candidate files:
// 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);
Assess retrieved content for relevance:
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)
}));
}
Scoring criteria:
Update search criteria based on evaluation:
function refineQuery(evaluation, previousQuery) {
return {
// Add new patterns discovered in high-relevance files
patterns: [...previousQuery.patterns, ...extractPatterns(evaluation)],
// Add terminology found in codebase
keywords: [...previousQuery.keywords, ...extractKeywords(evaluation)],
// Exclude confirmed irrelevant paths
excludes: [...previousQuery.excludes, ...evaluation
.filter(e => e.relevance < 0.2)
.map(e => e.path)
],
// Target specific gaps
focusAreas: evaluation
.flatMap(e => e.missingContext)
.filter(unique)
};
}
Repeat with refined criteria (max 3 cycles):
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);
// Check if we have sufficient context
const highRelevance = evaluation.filter(e => e.relevance >= 0.7);
if (highRelevance.length >= 3 && !hasCriticalGaps(evaluation)) {
return highRelevance;
}
// Refine and continue
query = refineQuery(evaluation, query);
bestContext = mergeContext(bestContext, highRelevance);
}
return bestContext;
}
Task: "Fix the authentication token expiry bug"
Cycle 1:
DISPATCH: Search for "token", "auth", "expiry" in src/**
EVALUATE: Found auth.ts (0.9), tokens.ts (0.8), user.ts (0.3)
REFINE: Add "refresh", "jwt" keywords; exclude user.ts
Cycle 2:
DISPATCH: Search refined terms
EVALUATE: Found session-manager.ts (0.95), jwt-utils.ts (0.85)
REFINE: Sufficient context (2 high-relevance files)
Result: auth.ts, tokens.ts, session-manager.ts, jwt-utils.ts
Task: "Add rate limiting to API endpoints"
Cycle 1:
DISPATCH: Search "rate", "limit", "api" in routes/**
EVALUATE: No matches - codebase uses "throttle" terminology
REFINE: Add "throttle", "middleware" keywords
Cycle 2:
DISPATCH: Search refined terms
EVALUATE: Found throttle.ts (0.9), middleware/index.ts (0.7)
REFINE: Need router patterns
Cycle 3:
DISPATCH: Search "router", "express" patterns
EVALUATE: Found router-setup.ts (0.8)
REFINE: Sufficient context
Result: throttle.ts, middleware/index.ts, router-setup.ts
Use in agent prompts:
When retrieving context for this task:
1. Start with broad keyword search
2. Evaluate each file's relevance (0-1 scale)
3. Identify what context is still missing
4. Refine search criteria and repeat (max 3 cycles)
5. Return files with relevance >= 0.7
continuous-learning skill - For patterns that improve over time~/.gemini/agents/