Hooks: Steer Your Agent Before the LLM Call
Small feature, outsized leverage: Dapr Agents lets you register hooks around the agent loop where the before_llm_call being the one I use the most. from dapr_agents import DurableAgent def add_live_context ( ctx ): ctx . messages . append ( web_search ( ctx . task )) # fresh context, every call agent = DurableAgent ( name = " researcher " , hooks = { " before_llm_call " : add_live_context }, )…
Dapr Agents enables the registration of hooks, also known as handlers, around the agent loop. The specific hook I find most useful is called before_llm_call. To incorporate it, you first import the DurableAgent class from the dapr_agents module.
Here's an example of how to use it:
```python
from dapr_agents import DurableAgent
def add_live_context(ctx):
ctx.messages.append(web_search(ctx.task)) # Fresh context, every call
agent = DurableAgent(
name="researcher",
hooks={
"before_llm_call": add_live_context
},
)
```
All the logic within the hook resides inside the durable workflow. This means that if the agent crashes after the hook but before the call, it can replay the workflow cleanly without losing any context. This approach is preferable to adding logic to the loop, as it allows the runtime to handle the hook, ensuring a more robust and reliable system.
It's crucial to note that the hook itself must be replay-safe, meaning it should not rely on any state that would be lost during a replay. Additionally, the hook author must specify whether the hook is deterministic or not. This information impacts how the replay process behaves. For instance, a web search, like the example provided, will not be deterministic and will rerun each time it's encountered.
This is beneficial for live context enrichment, as it ensures you always have the most up-to-date information. However, for tasks such as validating user tokens, a non-deterministic behavior could lead to issues, as it may rerun an approval process for a potentially expired token.
There are various use cases for hooks, as demonstrated by real-world examples. Some common applications include:
1. Injecting fresh web context (as showcased in a webinar demonstration)
2. Enforcing token budgets before the budget is spent
3. Redacting Personally Identifiable Information (PII) from both incoming and outgoing data
Redacting PII is particularly valuable when your agent handles sensitive data. By masking this information before sending it to an LLM provider, you enhance the security and privacy of your system.
If you're interested in learning more about implementing hooks in Dapr Agents, you can watch a webinar that walks through a comprehensive example using Tavily and Chainlit to enrich a model's answer with fresh web results. The webinar can be found at https://www.diagrid.io/webinars/make-your-llm-agent-production-smart. I encourage you to give it a try and share your thoughts!
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.