技巧

别用提示词约束 Agent,用确定性测试代替文字规则

精选理由

写代码 Agent 工作流的都该看看:与其在提示词里念叨规则,不如给每个检查点配上对应测试,省 token 还更稳。

Santiago 提出观点:能用测试自动检查的内容,就不该写进给 Agent 的文字指令里。他给出对应关系:行为用单元测试和集成测试、格式用 Formatter 和 Linter、类型用静态类型检查、API 契约用 Schema 校验、回归用回归测试、安全用静态分析和安全扫描。核心逻辑是确定性测试比写在提示词里的规则更可靠,还能省下 token。

原文 · Ate-a-Pi

Deterministic tests > Written instructions

Anything that should be checked automatically should have a test case:

• Checking behavior → Unit and integration tests • Checking formatting → Formatters and linters • Checking types → Static type checkers • Checking API contracts → Schema validation • Checking edge cases → Targeted tests • Checking regressions → Regression tests • Checking security → Static analysis and security scans

Don't waste tokens telling an agent to follow rules you could enforce with deterministic tests.

别用提示词约束 Agent,用确定性测试代替文字规则|AI热报