OpenClaw Agent 评估与基准测试

量化Agent能力 — 从"感觉不错"到"数据说话"

3分47秒,我的Agent完成了10个测试用例。7个通过,2个部分通过,1个完全失败。这就是Agent评估——不是玄学,是工程。如果你的Agent"感觉还行",那说明你还没开始认真测。

为什么需要Agent评估?

LLM模型有MMLU、HumanEval等标准基准,但Agent不是模型——它是模型+工具+策略的组合。Agent评估需要回答:

📐 评估金字塔:
Level 1: 冒烟测试(能跑吗?)
Level 2: 功能测试(做对了吗?)
Level 3: 性能测试(够快够便宜吗?)
Level 4: 对抗测试(会出错吗?)
Level 5: 对比测试(比其他方案好吗?)

设计Agent基准测试

1. 定义评估维度

# eval_config.yaml - Agent评估配置

dimensions:
  - name: task_completion
    description: "任务完成准确率"
    weight: 0.4
    metrics:
      - exact_match       # 完全匹配
      - partial_match     # 部分匹配
      - contains_key_info # 包含关键信息

  - name: efficiency
    description: "效率指标"
    weight: 0.25
    metrics:
      - steps_to_complete  # 完成步数
      - token_efficiency   # token效率
      - time_to_complete   # 完成时间

  - name: robustness
    description: "鲁棒性"
    weight: 0.2
    metrics:
      - error_recovery_rate    # 错误恢复率
      - edge_case_handling     # 边界情况处理
      - hallucination_rate     # 幻觉率

  - name: cost_efficiency
    description: "成本效率"
    weight: 0.15
    metrics:
      - cost_per_task          # 每任务成本
      - cache_hit_rate         # 缓存命中率
      - model_utilization      # 模型利用率

2. 编写测试用例

# test_cases.json - 标准化测试用例格式

{
  "test_suite": "openclaw_agent_basic",
  "version": "1.0",
  "cases": [
    {
      "id": "TC001",
      "category": "web_search",
      "input": "搜索OpenClaw最新版本更新",
      "expected_tools": ["web_search", "web_fetch"],
      "expected_output": {
        "contains": ["OpenClaw", "版本", "更新"],
        "not_contains": ["我不确定", "作为AI"],
        "max_steps": 5
      },
      "scoring": {
        "tool_correctness": 20,
        "output_quality": 40,
        "efficiency": 20,
        "no_hallucination": 20
      }
    },
    {
      "id": "TC002",
      "category": "file_operations",
      "input": "在/var/www/miaoquai/tools/下创建test.html",
      "expected_tools": ["write"],
      "expected_output": {
        "file_exists": true,
        "file_path": "/var/www/miaoquai/tools/test.html",
        "max_steps": 3
      }
    },
    {
      "id": "TC003",
      "category": "error_handling",
      "input": "读取不存在的文件 /tmp/nonexistent.txt",
      "expected_behavior": "优雅地报告文件不存在,而非崩溃",
      "expected_tools": ["read"],
      "expected_output": {
        "contains": ["不存在", "not found", "无法读取"],
        "not_contains": ["Error: process exit"],
        "max_steps": 3
      }
    }
  ]
}

运行评估

命令行评估

# 运行完整评估套件
openclaw eval run --suite openclaw_agent_basic

# 运行特定类别
openclaw eval run --category web_search

# 对比不同模型
openclaw eval run --model-a gpt-4o --model-b deepseek-chat

# 运行回归测试
openclaw eval run --regression --baseline last_week

结果输出

# 评估结果报告
=====================================================
OpenClaw Agent Evaluation Report
Date: 2026-04-25 | Agent: miaoquai | Model: gpt-4o
=====================================================

Overall Score: 82.3/100 (B+)

Dimension Breakdown:
  Task Completion:  88.5/100 ✅
  Efficiency:       75.2/100 ⚠️
  Robustness:       79.0/100 ⚠️
  Cost Efficiency:  82.1/100 ✅

Test Case Results:
  TC001 (web_search):     PASS  92/100 ✅
  TC002 (file_ops):       PASS  95/100 ✅
  TC003 (error_handling): PASS  85/100 ✅
  TC004 (multi_tool):     PARTIAL 65/100 ⚠️
    - Used 8 steps (expected ≤5)
    - Redundant tool calls detected

  TC005 (edge_case):      FAIL  35/100 ❌
    - Hallucinated file path
    - Did not verify result

Recommendations:
  1. Add result verification step after file writes
  2. Set max_steps limit for multi-tool tasks
  3. Add guardrails for path hallucination

A/B 测试对比

模型对比

# 对比两个模型在同一测试集上的表现
openclaw eval compare \
  --model-a gpt-4o \
  --model-b deepseek-chat \
  --suite openclaw_agent_basic \
  --runs 3  # 每个测试用例跑3次取平均

# 对比结果
┌──────────────┬──────────┬──────────────┬─────────┐
│ Metric       │ gpt-4o   │ deepseek-chat│ Delta   │
├──────────────┼──────────┼──────────────┼─────────┤
│ Task Score   │ 88.5     │ 82.1         │ -6.4    │
│ Avg Steps    │ 4.2      │ 5.8          │ +1.6    │
│ Avg Tokens   │ 2,340    │ 3,120        │ +780    │
│ Avg Cost     │ $0.12    │ $0.02        │ -$0.10  │
│ Time (avg)   │ 8.3s     │ 12.1s        │ +3.8s   │
│ Cost/Score   │ $0.0014  │ $0.0002      │ -85.7%  │
└──────────────┴──────────┴──────────────┴─────────┘

# 结论:deepseek-chat性价比更高,但gpt-4o质量更好

Prompt版本对比

# 对比不同System Prompt版本
openclaw eval compare \
  --prompt-a "v1_system_prompt" \
  --prompt-b "v2_system_prompt" \
  --suite openclaw_agent_basic

# 关键:只改一个变量,其他保持不变

自动化回归测试

CI/CD 集成

# .github/workflows/agent-eval.yml
name: Agent Evaluation
on:
  push:
    paths:
      - 'agents/**'
      - 'skills/**'
  schedule:
    - cron: '0 6 * * 1'  # 每周一早上6点

jobs:
  evaluate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Run Agent Eval
        run: |
          openclaw eval run --suite regression \
            --output results.json \
            --fail-threshold 70
      - name: Check Results
        run: |
          # 如果总分低于70,CI失败
          SCORE=$(jq '.overall_score' results.json)
          if [ "$SCORE" -lt 70 ]; then
            echo "Agent score $SCORE below threshold 70"
            exit 1
          fi

持续监控

# 定期运行评估,追踪Agent质量趋势
openclaw eval trend --last 30days

# 输出趋势图
# Score  88 → 85 → 82 → 84 → 86 → 83
#                ↓      ↑         ↓
#              4/10   4/17      4/24
# Agent质量在缓慢下降,需要调查原因

关键评估指标速查

Task Completion Rate 任务完成率 Tool Selection Accuracy 工具选择准确率 Step Efficiency 步骤效率 Hallucination Rate 幻觉率 Error Recovery 错误恢复率 Cost Per Task 单任务成本 Latency P95 延迟P95 Token Efficiency Token效率
🎯 黄金指标:不同场景关注不同指标。内容生产Agent关注Task Completion Rate,客服Agent关注Hallucination Rate,自动化Agent关注Error Recovery Rate。别一把抓,先定义你的北极星指标。

常见问题

Q: LLM输出不确定,怎么保证评估一致性?

同一个输入跑3-5次取平均值。关键测试用例跑10次以上。设置随机种子可以提升可复现性,但LLM本身有温度参数,完全一致不可能。关注趋势而非绝对值。

Q: 评估本身会不会消耗很多token?

会。一个完整的回归测试套件可能消耗50-100K tokens。建议:1) 用便宜模型运行评估 2) 只在关键变更时运行完整套件 3) 日常只跑冒烟测试(5-10个核心用例)。

Q: 怎么评估Agent的"创造力"?

创造力不适合自动化评估。建议用"LLM-as-Judge":用一个强模型(如GPT-4o)评估另一个模型的创意输出。设置评分标准(新颖性、实用性、一致性),人工抽检10%校准。

相关教程