Skip to main content

What hooks are

Hooks are async callbacks you register at startup. KavachOS calls them at specific points in the authorization and agent lifecycle. They let you add custom logic without patching the SDK: log denials to Slack, block agents from running in unsandboxed environments, fire webhooks, or update your own database.
Hooks are async. beforeAuthorize and beforeAgentCreate can block requests by returning { allow: false }. All other hooks are fire-and-forget from KavachOS’s perspective, but any unhandled exception in a hook will propagate to the caller.

Available hooks

Fires before every authorize() call. Return { allow: false, reason } to block. Return void or { allow: true } to proceed.
Fires after authorize() completes with the final result. Useful for logging and alerting.
Fires before an agent is created. Return { allow: false } to reject the creation.
Fires after an agent is successfully created.
Fires when an agent is revoked.
Fires when a permission denial, rate limit, or policy violation is detected.

Violation types

The onViolation hook receives a typed type field so you can route each category to a different handler.

Registering hooks

Pass a hooks object to createKavach:

Logging every denial

The result.auditId links this log line to the immutable audit entry. You can use it to correlate your own logs with the KavachOS audit trail.

Enforcing a sandbox check

Returning { allow: false } from beforeAgentCreate causes the kavach.agent.create() call to throw a KavachError with the reason you provided.

Reacting to violations

The onViolation hook fires for any denial that fits a known violation category. Use it to send alerts or update your observability platform.

Cleaning up after revocation

Next steps

Budget policies

Block agents when they exceed token or call limits.

Event streaming

Stream authorization events to Kafka, NATS, or Webhooks.

Audit log

Query the immutable record of every authorization decision.
Last modified on April 20, 2026