Skip to main content

Hooks & Guardrails

Agentium agents support hooks for lifecycle events and guardrails for validating input and output. Use hooks for logging, analytics, or side effects; use guardrails for content moderation, safety, or compliance.

AgentHooks

Hooks are async functions called at specific points in the agent lifecycle:

Example: Logging Hook


Input Guardrails

Input guardrails validate user input before it is sent to the LLM. If any guardrail fails, the run is aborted with an error.

Output Guardrails

Output guardrails validate the agent’s response after the run. If any guardrail fails, the run throws.

GuardrailResult

Each guardrail returns a GuardrailResult:
  • pass: true — Validation succeeded; execution continues.
  • pass: false — Validation failed; execution stops with an error containing reason.

Example: Content Moderation Guardrail


Example: Output Length Guardrail


Multiple Guardrails

You can use multiple input and output guardrails. All must pass for the run to proceed:
Guardrails run in order. The first failure stops execution and throws.

Full Example