DEV Community

Cover image for Hooks: Steer Your Agent Before the LLM Call
Casper Nielsen
Casper Nielsen

Posted on

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},
)
Enter fullscreen mode Exit fullscreen mode

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. That's the difference between bolting logic onto the loop and having the runtime own it. It's important to note here that the hook itself has to be replay safe. Furthermore, the hook author needs to define whether this hook is deterministic or not. This has impact on replaying. A web search, as the above example is, will not be deterministic and thus will rerun. 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!

Uses we've seen in the wild: injecting fresh web context (my webinar demo), token-budget enforcement before the spend happens, redacting PII on the way in - and on the way out too. The latter is powerful to use if your agent handles sensitive data you'd want to mask before sending to an LLM provider.

The webinar takes you through a full example with Tavily and Chainlit to enrich a models answer with fresh web results: https://www.diagrid.io/webinars/make-your-llm-agent-production-smart

Give it a spin and lmk what you think!

Top comments (0)