📐 思维模型手册/PID 控制器 · PID Controller
控制论11 分钟阅读13 天前

04PID 控制器 · PID Controller

经典控制论 · 用于 Agent 输出质量反馈
#agent#feedback𝕏 分享
04

PID 控制器 · PID Controller

经典控制论 · 用于 Agent 输出质量反馈
控制
核心公式
u(t) = Kp·e(t) + Ki·∫e(t)dt + Kd·de/dt e(t) = 误差 = 目标分数 − 实际分数 Kp·e = P 项:当前这次差多少 → 立即修正 Ki·∫e = I 项:历史误差积累 → 系统性修正 Kd·de = D 项:误差变化速度 → 预判式修正
三项的工程映射
PID 项信号Claude Code 中的动作
P(当前误差) 这次输出得 4/10 立刻修改这次的 prompt 措辞/结构
I(历史积累) 最近 10 次平均 5.5/10 重写 system prompt,修改 CLAUDE.md 的 agent 规则
D(变化趋势) 得分从 7→6→5,趋势在恶化 预判性重构,不等质量崩溃再改
代码模式 — Agent 质量反馈系统
quality-feedback.ts Content Agent
class AgentQualityPID {
  private history: number[] = [];
  private kp = 0.5, ki = 0.1, kd = 0.3;
  private target = 8.5;

  record(score: number): CorrectiveAction {
    this.history.push(score);
    
    const e = this.target - score;                           // 当前误差
    const recentHistory = this.history.slice(-10);
    const I = recentHistory.reduce((s, v) => s + (this.target - v), 0);
    const D = this.history.length > 1
      ? score - this.history[this.history.length - 2]  // 正数=改善,负数=恶化
      : 0;
    
    const signal = this.kp * e + this.ki * I - this.kd * D;

    // 决策逻辑
    if (Math.abs(e) > 3) return {
      type: 'immediate',
      action: 'rewrite_user_prompt',    // P 大:立即修这次
      reason: `Score ${score} vs target ${this.target}`
    };

    if (I > 8) return {
      type: 'systemic',
      action: 'update_system_prompt',    // I 大:系统性问题
      reason: `Accumulated error: ${I.toFixed(1)}`
    };

    if (D < -1) return {
      type: 'preemptive',
      action: 'restructure_agent_pipeline', // D 负:趋势恶化
      reason: `Quality trending down: ${D.toFixed(2)}/call`
    };

    return { type: 'none', action: 'continue' };
  }
}

// 用法:在 agent 每次执行后记录
const pid = new AgentQualityPID();
const result = await agent.run(task);
const action = pid.record(await evaluate(result));
if (action.type !== 'none') handleCorrection(action);
⚠ 反模式(只看 P) 每次输出差了就改措辞,从不看历史趋势。结果是过度修正——像没有 D 项的控制器,会在目标附近震荡,无法稳定收敛。

调参直觉

  • Kp 太高:修正过猛,质量在目标附近上下震荡
  • Ki 太高:对历史误差太敏感,频繁触发"系统性重写"
  • Kd 太高:对趋势过于敏感,还没到问题就开始重构
  • 推荐起点:Kp=0.5, Ki=0.1, Kd=0.3,根据实际震荡情况调整
拿走就能用 — 粘贴进你的 CLAUDE.md
CLAUDE.md Agent 质量反馈规则
## Agent 输出质量反馈(PID 模式)

### P 项(当前误差)— 立即修正
- 单次输出明显偏差时:在下一条消息里直接指出,让 Claude 重做这一步
- 不要积累问题,当场纠正

### I 项(历史积累)— 系统性修正
- 如果同类问题出现 3 次以上:更新本文件的相关规则
- 触发信号:反复提醒同一件事 → 说明 system prompt 有结构性漏洞

### D 项(变化趋势)— 预判式修正
- 输出质量在变差但还没崩溃时:主动重新描述任务背景和约束
- 不要等到质量已经很差再修正,提前干预成本最低
← 上一篇香农熵 · Shannon Entropy下一篇 →相变 · Phase Transition