{
  "id": 13062835,
  "title": "Hooks: Steer Your Agent Before the LLM Call",
  "url": "https://urgent.news/2026/10/09/hooks-steer-your-agent-before-the-llm-call",
  "topic": "ai",
  "section": "AI",
  "published": "2026-10-09T07:51:25.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/casper_nielsen_47eb2e6023/hooks-steer-your-agent-before-the-llm-call-17hn"
  },
  "original_language": "en",
  "account": "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.\n\nHere's an example of how to use it:\n\n```python\nfrom dapr_agents import DurableAgent\n\ndef add_live_context(ctx):\nctx.messages.append(web_search(ctx.task)) # Fresh context, every call\n\nagent = DurableAgent(\nname=\"researcher\",\nhooks={\n\"before_llm_call\": add_live_context\n},\n)\n```\n\nAll 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.\n\nIt'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.\n\nThere are various use cases for hooks, as demonstrated by real-world examples. Some common applications include:\n\n1. Injecting fresh web context (as showcased in a webinar demonstration)\n2. Enforcing token budgets before the budget is spent\n3. Redacting Personally Identifiable Information (PII) from both incoming and outgoing data\n\nRedacting 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.\n\nIf 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!",
  "summary": "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 }, )…",
  "key_points": [
    "Dapr Agents allow registration of hooks around agent loop.",
    "\"beforellmcall\" hook adds live context before LLM call.",
    "Hooks must be replay-safe and deterministic or not."
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}