Hooks: Steer Your Agent Before the LLM Call
说明与用途
在 Dapr Agents 中使用 before_llm_call 钩子,可可靠注入实时上下文、验证令牌并去除 PII,同时在失败时保持状态。
问题
Integrating dynamic context and safety checks into an LLM agent loop without losing state on failures.
结果
Using before_llm_call hooks in Dapr Agents enables reliable injection of live web context, token‑budget enforcement, and PII redaction while maintaining durable state across failures.
工作流
- Define hook function (e.g., add_live_context) that appends fresh data to context.
- Instantiate DurableAgent with name and hooks mapping before_llm_call to the hook.
- Hook executes before each LLM call, ensuring fresh context or validation.
- If crash occurs after hook but before LLM call, durable workflow replays hook, preserving context.
- Ensure hook is replay‑safe and declare determinism to control replay behavior.
证据
Dapr Agents lets you register hooks around the agent loop where the before_llm_call being the one I use the most.
Everything the hook does, lives inside the durable workflow, so a crash after the hook but before the call replays cleanly with no lost context.
For live context, this is great and probably what we want. For validating user tokens, this is equally great - otherwise we'd just roll an approval on a potentially expired token!
- 记录日期: 2026-10-09T08:55:56.960Z