Skip to main content

The problem policies solve

When agents make LLM calls, costs accumulate in the background. Without limits, a single runaway agent or a misconfigured loop can burn through a month’s budget in hours. Budget policies let you set hard caps and choose what happens when those caps are hit. Policies are evaluated at authorization time, before any LLM call is made. If an agent is over budget, the authorization check fails before your code even runs.
Policies stack. An agent can have a per-agent policy, a per-user policy, and a per-tenant policy all active at once. KavachOS evaluates all of them and returns the first one that is exceeded.

Data model

string
Stable identifier with a pol_ prefix.
string
default:"undefined (all agents)"
Agent this policy applies to. Omit to create a global policy that applies to all agents.
string
default:"undefined (all users)"
User this policy applies to. Omit to apply regardless of owner.
string
default:"undefined (all tenants)"
Tenant this policy applies to.
BudgetLimits
The numeric thresholds for this policy.
'warn' | 'throttle' | 'block' | 'revoke'
What happens when a limit is exceeded.
'active' | 'triggered' | 'disabled'
Current policy state. ‘triggered’ means a limit has been hit.
BudgetUsage
Running counters for this policy.

BudgetLimits

number
default:"undefined (no daily token limit)"
Maximum token cost units allowed per day. Resets at midnight UTC.
number
default:"undefined (no monthly token limit)"
Maximum token cost units allowed per calendar month.
number
default:"undefined (no daily call limit)"
Maximum authorize() calls allowed per day.
number
default:"undefined (no monthly call limit)"
Maximum authorize() calls allowed per calendar month.

Actions

warn is useful for sending alerts before you start blocking. Set a warn policy at 80% of your limit and a block policy at 100%.

Creating a policy

Checking a budget before a call

Call checkBudget with a speculative tokensCost to see whether this call would exceed any policy. The cost is included in the check but not recorded yet.
checkBudget evaluates all active policies for the agent (both exact-match and global). If any policy is exceeded, it returns the first violation and stops.

Recording usage after a call

Call recordUsage after the LLM call completes to update the counters.
recordUsage increments callsToday, callsThisMonth, tokensCostToday, and tokensCostThisMonth on every active policy that applies to this agent. It also transitions any policy from active to triggered if the new totals cross a threshold.

Resetting counters

Reset daily counters on a UTC midnight cron job:
Reset monthly counters on the first of each month:
After a reset, policies that were triggered are automatically moved back to active if the new totals are within limits.

Listing and updating policies

Combining warn and block

A common pattern: warn at a soft limit, block at the hard limit.
When the agent hits 800, the warn policy triggers and you can send an alert (via a lifecycle hook). At 1000, the block policy triggers and requests stop.

Next steps

Lifecycle hooks

Fire custom logic when a policy is triggered.

Cost attribution

Aggregate token costs per agent for billing reports.

Multi-tenant isolation

Attach policies to entire tenants.
Last modified on April 20, 2026