展示HN:SafeAgent – AI代理副作用的精确一次执行保护器
我构建了一个名为 SafeAgent 的小型 Python 库,旨在保护 AI 代理在重试工具调用时避免产生现实世界的副作用。
在实验代理工作流时,我们遇到的一个问题是,重试可能会多次触发不可逆的操作:
代理调用工具
↓
网络超时
↓
代理重试
↓
副作用发生两次
示例:
• 重复支付
• 重复发送邮件
• 重复生成票据
• 重复交易
大多数系统通过在不同服务中散布幂等性密钥来临时解决这个问题。
SafeAgent 将这一过程集中到一个小型执行保护机制中。
这个想法很简单:
1. 每次工具执行都会获得一个 request_id
2. SafeAgent 记录执行凭证
3. 重试时返回原始凭证,而不是再次执行副作用
示例:
第一次调用
真实副作用:发送邮件
第二次调用(使用相同的 request_id)
SafeAgent 返回原始执行凭证
(没有第二次副作用)
该项目仍处于早期阶段,但包括以下示例:
• OpenAI 工具调用
• LangChain 风格的工具
• CrewAI 操作
PyPI:
[https://pypi.org/project/safeagent-exec-guard/](https://pypi.org/project/safeagent-exec-guard/)
GitHub:
[https://github.com/azender1/SafeAgent](https://github.com/azender1/SafeAgent)
我很好奇其他人是如何处理代理副作用的重试安全问题的。
查看原文
I built a small Python library called SafeAgent that protects real-world side effects when AI agents retry tool calls.<p>One issue we ran into while experimenting with agent workflows is that retries can trigger irreversible actions multiple times:<p>agent calls tool
↓
network timeout
↓
agent retries
↓
side effect happens twice<p>Examples:<p>• duplicate payment
• duplicate email
• duplicate ticket
• duplicate trade<p>Most systems solve this ad-hoc using idempotency keys scattered around different services.<p>SafeAgent centralizes this into a small execution guard.<p>The idea is simple:<p>1. every tool execution gets a request_id
2. SafeAgent records the execution receipt
3. retries return the original receipt instead of running the side effect again<p>Example:<p>FIRST CALL
REAL SIDE EFFECT: sending email<p>SECOND CALL WITH SAME request_id
SafeAgent returns the original execution receipt
(no second side effect)<p>The project is early but includes examples for:<p>• OpenAI tool calls
• LangChain style tools
• CrewAI actions<p>PyPI:
<a href="https://pypi.org/project/safeagent-exec-guard/" rel="nofollow">https://pypi.org/project/safeagent-exec-guard/</a><p>GitHub:
<a href="https://github.com/azender1/SafeAgent" rel="nofollow">https://github.com/azender1/SafeAgent</a><p>Curious how other people are handling retry safety for agent side effects.