verify-patterns

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Agent Pattern Verification

Agent模式验证

Purpose

用途

Verify AI agent code for common anti-patterns that can cause infinite loops, runaway retries, tool mismatches, and context overflow. All analysis happens locally.
验证AI Agent代码中可能导致无限循环、失控重试、工具不匹配和上下文溢出的常见反模式。所有分析均在本地进行。

When to Use

使用场景

Trigger this skill when the user asks to:
  • "verify agent patterns"
  • "check agent loops"
  • "verify tools"
  • "check retry limits"
  • "verify agent safety"
Note: For full verification including security, quality, and language-specific checks, tell the user to say "verify agent".
当用户提出以下请求时触发此技能:
  • "验证Agent模式"
  • "检查Agent循环"
  • "验证工具"
  • "检查重试限制"
  • "验证Agent安全性"
注意: 如需包含安全、质量和特定语言检查的完整验证,请告知用户说出 "verify agent"

Process

流程

Step 1: Detect Agent Framework

步骤1:检测Agent框架

Identify the agent framework by checking imports in Python/TypeScript files:
Import PatternFramework
from langgraph
or
import langgraph
LangGraph
from crewai
or
import crewai
CrewAI
from autogen
or
import autogen
AutoGen
from langchain
or
import langchain
LangChain
Direct
openai
/
anthropic
SDK only
Custom
Also check for framework config files:
langgraph.json
,
crew.yaml
.
通过检查Python/TypeScript文件中的导入语句识别Agent框架:
导入模式框架
from langgraph
import langgraph
LangGraph
from crewai
import crewai
CrewAI
from autogen
import autogen
AutoGen
from langchain
import langchain
LangChain
仅直接使用
openai
/
anthropic
SDK
自定义框架
同时检查框架配置文件:
langgraph.json
crew.yaml

Step 2: Locate Agent Files

步骤2:定位Agent文件

Find files to analyze:
Priority files:
  • graph.py
    ,
    graph.ts
    - Agent workflow definitions
  • tools.py
    ,
    tools.ts
    ,
    tools/*.py
    ,
    tools/*.ts
    - Tool implementations
  • state.py
    ,
    state.ts
    - State schemas
  • prompts.py
    ,
    prompts/*.md
    ,
    system.md
    - Prompt templates
  • agent.py
    ,
    agent.ts
    - Main agent logic
Directories to check:
  • src/agent/
    ,
    agent/
    ,
    src/
    , project root
  • lib/
    ,
    app/
    ,
    packages/
Exclude from analysis:
  • skills/
    directory — these are skill definitions, not agent system prompts
找到需要分析的文件:
优先文件:
  • graph.py
    graph.ts
    - Agent工作流定义
  • tools.py
    tools.ts
    tools/*.py
    tools/*.ts
    - 工具实现
  • state.py
    state.ts
    - 状态模式
  • prompts.py
    prompts/*.md
    system.md
    - 提示词模板
  • agent.py
    agent.ts
    - 主Agent逻辑
需检查的目录:
  • src/agent/
    agent/
    src/
    、项目根目录
  • lib/
    app/
    packages/
排除分析的内容:
  • skills/
    目录 —— 这些是技能定义,而非Agent系统提示词

Step 3: Run Pattern Checks

步骤3:运行模式检查

Check Tiers

检查层级

  • [PATTERN]
    — Mechanical check. Apply exactly as written.
  • [HEURISTIC]
    — Judgment required. Mark findings clearly.
Tag every finding with
[P]
for pattern or
[H]
for heuristic.

  • [PATTERN]
    —— 机械检查。严格按描述执行。
  • [HEURISTIC]
    —— 需要主观判断。清晰标记发现的问题。
为每个发现的问题标记
[P]
(模式匹配)或
[H]
(启发式判断)。

3.1
[PATTERN]
Loop Safety

3.1
[PATTERN]
循环安全性

Apply mechanically. Do not pass a loop because it "looks like it might terminate."
Pattern to findPass conditionSeverity
while True:
in Python
A
break
statement exists within the same block scope
⚠️ Warning if absent
for { }
in Go
A
break
or
return
exists within the block
⚠️ Warning if absent
while (true)
in TS/JS
A
break
or
return
exists within the block
⚠️ Warning if absent
Function calls itself recursivelyA non-recursive return path exists (base case), OR a depth/counter parameter is present⚠️ Warning if absent
[HEURISTIC]
Fallback: Unrecognized Loop Patterns
After applying the pattern table, also scan for:
  • Loops where termination depends entirely on external/runtime state with no timeout
  • Generator functions that
    yield
    indefinitely without documented exit
  • Event/polling loops without timeout parameters
  • Recursive call chains across multiple functions without depth tracking
Flag as ⚠️ Warning: "Potential unbounded loop not matching known patterns — verify termination condition manually"

机械执行检查。不要因为循环“看起来可能终止”就忽略问题。
需查找的模式通过条件严重程度
Python中的
while True:
同一代码块内存在
break
语句
⚠️ 缺失则警告
Go中的
for { }
代码块内存在
break
return
⚠️ 缺失则警告
TS/JS中的
while (true)
代码块内存在
break
return
⚠️ 缺失则警告
函数递归调用自身存在非递归返回路径(基准情况),或包含深度/计数器参数⚠️ 缺失则警告
[HEURISTIC]
备选方案:未识别的循环模式
应用上述模式表后,还需扫描以下情况:
  • 终止完全依赖外部/运行时状态且无超时的循环
  • 无限
    yield
    且未记录退出条件的生成器函数
  • 无超时参数的事件/轮询循环
  • 跨多个函数且无深度跟踪的递归调用链
标记为 ⚠️ 警告:"存在未匹配已知模式的潜在无限循环 —— 请手动验证终止条件"

3.2
[PATTERN]
Retry Limit Enforcement

3.2
[PATTERN]
重试限制强制

Apply mechanically. If required parameter is absent, flag as ❌ Issue.
Python — Decorator-based:
Library/PatternRequired parameterFail condition
@retry
(tenacity)
stop=stop_after_attempt(n)
or
stop=stop_after_delay(n)
stop=
absent
@backoff.on_exception
max_tries=n
max_tries=
absent
Python — HTTP client retry:
Library/PatternRequired parameterFail condition
urllib3.Retry(...)
total=n
where n > 0
total=
absent or
total=0
HTTPAdapter(max_retries=Retry(...))
Retry object must have
total=n
total=
absent
httpx.HTTPTransport(retries=n)
retries=n
where n > 0
retries=
absent or
retries=0
Python — AWS SDK (boto3):
Library/PatternRequired parameterFail condition
Config(retries={...})
max_attempts
> 1
max_attempts
absent or ≤ 1
Note: boto3 without explicit retry config uses SDK defaults (3 attempts) — do not flag absence.
JavaScript/TypeScript:
Library/PatternRequired parameterFail condition
retry(...)
(async-retry)
retries: n
in options
retries:
absent
pRetry(...)
(p-retry)
retries: n
in options
retries:
absent
Custom retry loops (all languages):
Pattern to findPass conditionFail condition
Loop +
try/except
+
continue
Integer counter with max checkNo counter → ❌ Issue
[HEURISTIC]
Fallback: Unrecognized Retry Patterns
After applying pattern tables, scan for:
  • Functions/decorators with "retry" in name not in tables above
  • Imported modules with "retry" in package name (e.g.
    stamina
    ,
    aiohttp_retry
    )
  • Loops with sleep + exception handling + re-invocation without visible counter
  • Config keys like
    max_retries
    ,
    retry_count
    ,
    attempts
Flag as ⚠️ Warning: "Potential retry pattern not matching known libraries — verify retry bounds manually"

机械执行检查。若缺失必填参数,标记为 ❌ 问题。
Python —— 基于装饰器:
库/模式必填参数失败条件
@retry
(tenacity)
stop=stop_after_attempt(n)
stop=stop_after_delay(n)
缺失
stop=
@backoff.on_exception
max_tries=n
缺失
max_tries=
Python —— HTTP客户端重试:
库/模式必填参数失败条件
urllib3.Retry(...)
total=n
且n > 0
缺失
total=
total=0
HTTPAdapter(max_retries=Retry(...))
Retry对象必须包含
total=n
缺失
total=
httpx.HTTPTransport(retries=n)
retries=n
且n > 0
缺失
retries=
retries=0
Python —— AWS SDK(boto3):
库/模式必填参数失败条件
Config(retries={...})
max_attempts
> 1
缺失
max_attempts
或其值≤1
注意:未显式配置重试的boto3会使用SDK默认值(3次尝试)—— 无需标记缺失。
JavaScript/TypeScript:
库/模式必填参数失败条件
retry(...)
(async-retry)
选项中包含
retries: n
缺失
retries:
pRetry(...)
(p-retry)
选项中包含
retries: n
缺失
retries:
自定义重试循环(所有语言):
需查找的模式通过条件失败条件
循环 +
try/except
+
continue
包含带最大值检查的整数计数器无计数器 → ❌ 问题
[HEURISTIC]
备选方案:未识别的重试模式
应用上述模式表后,扫描以下情况:
  • 名称包含“retry”且未在表中的函数/装饰器
  • 包名包含“retry”的导入模块(如
    stamina
    aiohttp_retry
  • 包含sleep+异常处理+重新调用但无可见计数器的循环
  • 类似
    max_retries
    retry_count
    attempts
    的配置键
标记为 ⚠️ 警告:"存在未匹配已知库的潜在重试模式 —— 请手动验证重试边界"

3.3
[PATTERN]
Tool Registry Consistency

3.3
[PATTERN]
工具注册表一致性

Step 1: Collect defined tools
Scan tool definition files. A name found by any pattern counts as registered.
Python — decorator patterns:
PatternHow to extract name
@tool
(LangChain) on
def
Function name below decorator
@function_tool
(OpenAI Agents SDK) on
def
Function name below decorator
@tool(name="...")
Use
name=
argument value
Python — dict/list patterns:
PatternHow to extract name
{"type": "function", "function": {"name": "..."}}
(OpenAI)
Value of
"name"
inside
"function"
{"name": "...", "input_schema": {...}}
(Anthropic)
Top-level
"name"
{"name": "...", "description": "...", "parameters": {...}}
Top-level
"name"
ToolNode([func1, func2, ...])
(LangGraph)
Each function name in list
tools = [func1, func2]
/
TOOLS = [...]
Each identifier in list
TypeScript/JavaScript:
PatternHow to extract name
{ type: "function", function: { name: "..." } }
(OpenAI)
name:
inside
function:
tool({ description: "...", parameters: z.object({...}) })
The
const
variable name
new DynamicTool({ name: "...", ... })
(LangChain.js)
Value of
name:
zodFunction({ name: "...", ... })
Value of
name:
Step 2: Collect tool references from prompts
Scan
.md
,
.txt
,
prompts.py
for backtick-quoted identifiers naming capabilities.
Step 3: Cross-reference
FindingSeverity
Reference not in definition list❌ Issue (hallucinated tool)
Defined tool not in any prompt⚠️ Warning (undocumented tool)
[HEURISTIC]
Tools never bound to LLM
Find where tools are defined and where LLM is invoked. If tools exist but are never connected to the LLM call, flag as ❌ Issue: "Tools defined but never connected to LLM invocation"
[HEURISTIC]
Fallback: Unrecognized Tool Definitions
Scan for tool-like structures:
  • Dicts with both
    "description"
    and
    "parameters"
    keys
  • Functions with structured docstrings (name, params, return)
  • Variables named
    tools
    ,
    tool_list
    ,
    available_tools
    ,
    functions
  • Classes with
    run()
    ,
    execute()
    , or
    __call__()
    methods
Include in count and note: "Tool detected via heuristic — verify this is an intended agent tool."

步骤1:收集已定义的工具
扫描工具定义文件。通过任意模式找到的名称均视为已注册。
Python —— 装饰器模式:
模式名称提取方式
def
上的
@tool
(LangChain)
装饰器下方的函数名
def
上的
@function_tool
(OpenAI Agents SDK)
装饰器下方的函数名
@tool(name="...")
使用
name=
参数的值
Python —— 字典/列表模式:
模式名称提取方式
{"type": "function", "function": {"name": "..."}}
(OpenAI)
"function"
内的
"name"
{"name": "...", "input_schema": {...}}
(Anthropic)
顶层的
"name"
{"name": "...", "description": "...", "parameters": {...}}
顶层的
"name"
ToolNode([func1, func2, ...])
(LangGraph)
列表中的每个函数名
tools = [func1, func2]
/
TOOLS = [...]
列表中的每个标识符
TypeScript/JavaScript:
模式名称提取方式
{ type: "function", function: { name: "..." } }
(OpenAI)
function:
内的
name:
tool({ description: "...", parameters: z.object({...}) })
const
变量名
new DynamicTool({ name: "...", ... })
(LangChain.js)
name:
的值
zodFunction({ name: "...", ... })
name:
的值
步骤2:收集提示词中的工具引用
扫描
.md
.txt
prompts.py
文件,查找用反引号包裹的标识能力的名称。
步骤3:交叉引用
发现结果严重程度
引用未出现在定义列表中❌ 问题(幻觉工具)
已定义工具未出现在任何提示词中⚠️ 警告(未文档化工具)
[HEURISTIC]
工具未绑定到LLM
查找工具定义位置和LLM调用位置。若存在工具但从未关联到LLM调用,标记为 ❌ 问题:"工具已定义但从未关联到LLM调用"
[HEURISTIC]
备选方案:未识别的工具定义
扫描类工具结构:
  • 同时包含
    "description"
    "parameters"
    键的字典
  • 带有结构化文档字符串(名称、参数、返回值)的函数
  • 名为
    tools
    tool_list
    available_tools
    functions
    的变量
  • 包含
    run()
    execute()
    __call__()
    方法的类
将其计入总数并备注:"通过启发式检测到工具 —— 请验证这是否为预期的Agent工具。"

3.4
[PATTERN]
Context Size Awareness

3.4
[PATTERN]
上下文大小感知

Formula:
token_estimate = len(file_content_chars) / 4
Content⚠️ Warning threshold❌ Issue threshold
System prompt file> 4,000 tokens> 8,000 tokens
Single tool description> 500 tokens> 1,000 tokens
All tool descriptions combined> 2,000 tokens> 4,000 tokens
Exclude:
skills/
directories (loaded on demand, not embedded)
[HEURISTIC]
Fallback: Borderline and Non-Standard
  • Estimates within 20% of threshold → flag with tokenizer recommendation
  • Dynamic prompts (f-strings,
    .format()
    ) → flag if template alone is large
  • Multiple concatenated prompts → estimate combined size
  • Prompts with includes → note effective size may be larger

计算公式:
token_estimate = len(file_content_chars) / 4
内容⚠️ 警告阈值❌ 问题阈值
系统提示词文件> 4000 tokens> 8000 tokens
单个工具描述> 500 tokens> 1000 tokens
所有工具描述总和> 2000 tokens> 4000 tokens
排除:
skills/
目录(按需加载,不嵌入)
[HEURISTIC]
备选方案:临界值和非标准情况
  • 估算值在阈值的20%范围内 → 标记并推荐使用分词器
  • 动态提示词(f-strings、
    .format()
    )→ 若模板本身较大则标记
  • 多个拼接的提示词 → 估算总大小
  • 包含引用的提示词 → 备注实际大小可能更大

3.5
[HEURISTIC]
Explicit Tool Listing

3.5
[HEURISTIC]
工具显式列出

Check system prompts for:
  • Headers like "Available Tools", "You have access to"
  • Tool capability descriptions
Flag if tools are defined but not documented in system prompt.

检查系统提示词是否包含:
  • 类似“可用工具”“你可以访问”的标题
  • 工具能力描述
若工具已定义但未在系统提示词中记录则标记。

3.6
[PATTERN]
LangGraph Graph Cycle Analysis

3.6
[PATTERN]
LangGraph图循环分析

(Only when LangGraph is detected)
Detection steps:
a. Find graph file (
graph.py
,
graph.ts
, or file with
StateGraph
/
MessageGraph
)
b. Build edge map:
  • workflow.add_edge(source, dest)
    — unconditional edge
  • workflow.add_conditional_edges(source, fn, mapping)
    — extract destinations from mapping
c. Identify cycles: nodes reachable from themselves
d. For each cycle, check if
END
(or
"__end__"
) is reachable via conditional edge
ConditionSeverity
Cycle exists,
END
reachable via conditional
✅ Pass
Cycle exists, no path to
END
❌ Issue
Graph has no
END
node
❌ Issue
Node has no outgoing edges and is not
END
⚠️ Warning (dead-end)
Example — infinite cycle (❌ Issue):
python
workflow.add_edge("agent", "tools")
workflow.add_edge("tools", "agent")  # no path to END
Example — cycle with exit (✅ Pass):
python
workflow.add_conditional_edges("agent", should_continue, {
    "continue": "tools",
    "end": END
})
workflow.add_edge("tools", "agent")
[HEURISTIC]
Fallback: Non-LangGraph Graphs
Scan for graph-like control flow:
  • State machines with transition tables
  • Custom routing with implicit cycles
  • LangGraph.js (camelCase methods)
  • CrewAI/AutoGen agent handoffs
  • Adjacency lists without termination path
Flag as ⚠️ Warning: "Potential cyclic control flow — verify termination condition exists"

(仅检测到LangGraph时执行)
检测步骤:
a. 找到图文件(
graph.py
graph.ts
或包含
StateGraph
/
MessageGraph
的文件)
b. 构建边映射:
  • workflow.add_edge(source, dest)
    —— 无条件边
  • workflow.add_conditional_edges(source, fn, mapping)
    —— 从映射中提取目标节点
c. 识别循环:可从自身到达的节点
d. 对每个循环,检查是否可通过条件边到达
END
(或
"__end__"
条件严重程度
存在循环,可通过条件边到达
END
✅ 通过
存在循环,无到达
END
的路径
❌ 问题
图中无
END
节点
❌ 问题
节点无出边且不是
END
⚠️ 警告(死胡同)
示例 —— 无限循环(❌ 问题):
python
workflow.add_edge("agent", "tools")
workflow.add_edge("tools", "agent")  # 无到达END的路径
示例 —— 带出口的循环(✅ 通过):
python
workflow.add_conditional_edges("agent", should_continue, {
    "continue": "tools",
    "end": END
})
workflow.add_edge("tools", "agent")
[HEURISTIC]
备选方案:非LangGraph图
扫描类图控制流:
  • 带有转换表的状态机
  • 包含隐式循环的自定义路由
  • LangGraph.js(驼峰命名方法)
  • CrewAI/AutoGen Agent交接
  • 无终止路径的邻接表
标记为 ⚠️ 警告:"存在潜在循环控制流 —— 请验证是否存在终止条件"

Step 4: Generate Report

步骤4:生成报告

markdown
undefined
markdown
undefined

Agent Pattern Verification Report

Agent模式验证报告

Project: [name or path] Date: [current date] Framework detected: [LangGraph | CrewAI | AutoGen | LangChain | Custom | None] Files analyzed: [count]
项目: [名称或路径] 日期: [当前日期] 检测到的框架: [LangGraph | CrewAI | AutoGen | LangChain | 自定义 | 无] 分析文件数: [数量]

Summary

摘要

✅ X checks passed | ⚠️ Y warnings | ❌ Z issues
✅ X项检查通过 | ⚠️ Y项警告 | ❌ Z项问题

Loop Safety

循环安全性

  • All loops have termination conditions
  • ⚠️ Potential unbounded loop at
    [file:line]
  • 所有循环均有终止条件
  • ⚠️
    [file:line]
    处存在潜在无限循环

Retry Limits

重试限制

  • All retry mechanisms have explicit limits
  • ❌ Missing retry limit at
    [file:line]
  • 所有重试机制均有显式限制
  • [file:line]
    处缺失重试限制

Tool Consistency

工具一致性

  • Tool registry found: X tools defined
  • ❌ Y hallucinated tool references
  • ⚠️ Z undocumented tools
  • 找到工具注册表:已定义X个工具
  • ❌ Y个幻觉工具引用
  • ⚠️ Z个未文档化工具

Context Size

上下文大小

  • System prompt within limits (~X tokens)
  • ⚠️ System prompt exceeds recommended size
  • 系统提示词在限制范围内(约X tokens)
  • ⚠️ 系统提示词超出推荐大小

Findings

发现结果

[P]
= pattern-matched ·
[H]
= heuristic
[P]
= 模式匹配 ·
[H]
= 启发式判断

✅ Passing

✅ 通过项

  • [P]
    [Check]: [confirmation]
  • [P]
    [检查项]:[确认内容]

⚠️ Warnings

⚠️ 警告项

  • [P|H]
    [Check]: [description]
    • Location: [file:line]
    • Suggestion: [how to fix]
  • [P|H]
    [检查项]:[描述]
    • 位置: [file:line]
    • 建议: [修复方法]

❌ Issues

❌ 问题项

  • [P|H]
    [Check]: [description]
    • Location: [file:line]
    • Rule: [which rule violated]
    • Fix: [specific remediation]
  • [P|H]
    [检查项]:[描述]
    • 位置: [file:line]
    • 规则: [违反的规则]
    • 修复方案: [具体整改措施]

Recommendations

建议

  1. [Priority recommendation]
  2. [Additional improvements]

---

*For full verification including security, quality, and language-specific checks, say "verify agent".*
  1. [优先级建议]
  2. [其他改进建议]

---

*如需包含安全、质量和特定语言检查的完整验证,请说出 "verify agent"。*