Skip to main content

AgentKavach

PyPI Python License: MIT

Hard budget limits for LLM agents. AgentKavach wraps your OpenAI, Anthropic, Google, or Mistral client, tracks spend in real time, and stops an agent once it reaches its budget, so a runaway loop or retry storm cannot quietly run up your API bill.

pip install agentkavach

Contents

Overview

An AI agent can spend money on every API call. When one gets stuck in a loop or retries aggressively, the cost climbs faster than a human can react, and most tooling only reports the damage after it happens.

AgentKavach is a circuit breaker for that problem. It sits in front of your LLM client, checks the running spend before each call, raises alerts as usage approaches a limit, and refuses further calls once the budget is reached. The check runs locally and in memory, so enforcement does not depend on a network round trip.

You pass both keys explicitly: your AgentKavach key (api_key) and your provider key (llm_key). The SDK never reads them from the environment. Your provider key is used only inside your process to call the provider directly. It is never read from the environment, never written to disk, and never sent to AgentKavach.

Installation

pip install agentkavach

AgentKavach supports Python 3.9 and later. OpenAI works out of the box. To use Anthropic, Google, or Mistral, install that provider's own SDK alongside it.

Quickstart

from agentkavach import AgentKavach, Budget

guard = AgentKavach(
    provider="openai",
    api_key="ak_...",          # your AgentKavach key
    llm_key="sk-...",          # your OpenAI key, used locally and never sent to AgentKavach
    agent_name="research-bot",
    budget=Budget.daily(20),   # a hard 20 USD per day cap
)

response = guard.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Summarize today's headlines."}],
)

Both api_key and llm_key are required and must be passed explicitly to the constructor. The SDK never reads them from the environment, so supply them yourself, for example api_key=os.environ["AGENTKAVACH_API_KEY"]. If either is missing the constructor raises ValueError immediately. Your provider key (llm_key) stays in your process: it is used only to call the provider directly, is never read from the environment, is never written to disk, and is never sent to AgentKavach.

guard.create(...) mirrors the underlying provider client, so you can drop it into existing code with minimal changes. Before each call the engine checks the running spend. As usage crosses the configured thresholds it sends alerts, and once the budget is exhausted the next call raises BudgetExceededError instead of reaching the provider.

Budgets

A budget defines how much an agent may spend over a period.

from agentkavach import Budget

Budget.daily(20)      # 20 USD per calendar day
Budget.monthly(500)   # 500 USD per calendar month
Budget.total(1000)    # 1000 USD lifetime cap

# Share one budget across every agent in an organization:
Budget.org_budget(limit=2000, period="monthly")

Per agent budgets cap a single agent. An organization budget aggregates spend across every agent that reports to the same backend, which is useful for a fleet of workers that should share one limit.

Guardrails

Budgets cap cost. Guardrails cap the shape of a single run, and are passed to the constructor:

guard = AgentKavach(
    provider="openai",
    api_key="ak_...",
    llm_key="sk-...",
    agent_name="research-bot",
    budget=Budget.daily(20),
    max_tokens_per_run=50_000,    # stop the run after 50k tokens
    max_calls_per_run=100,        # stop after 100 API calls
    max_runtime_seconds=300,      # stop after 5 minutes
    detect_loops=True,            # halt on repeated identical calls
)

Each guardrail raises a specific, catchable exception (TokenLimitError, CallLimitError, RuntimeLimitError, LoopDetectedError) when its limit is reached.

Alerts and the kill switch

Attach channels that fire as usage crosses thresholds you choose. Email, Slack, PagerDuty, and webhooks are supported.

from agentkavach import AgentKavach, Budget, ChannelType

guard = AgentKavach(
    provider="openai",
    api_key="ak_...",
    llm_key="sk-...",
    agent_name="research-bot",
    budget=Budget.daily(20),
    channels=[
        AgentKavach.channel(ChannelType.EMAIL, threshold=0.5, to="oncall@example.com"),
        AgentKavach.channel(ChannelType.SLACK, threshold=0.8, webhook_url="https://hooks.slack.com/services/..."),
        AgentKavach.channel(ChannelType.PAGERDUTY, threshold=1.0, routing_key="R0..."),
    ],
    on_kill=lambda: print("agent halted"),
)

A threshold of 0.8 fires when usage reaches 80 percent of the budget. The optional on_kill callback runs once when an agent is halted, which is a convenient place to release resources or page a human.

Providers

One API across four providers. Set provider and pass the matching key as llm_key:

Provider provider value
OpenAI "openai"
Anthropic "anthropic"
Google "google"
Mistral "mistral"

Security and privacy

This SDK handles your provider key, so here is exactly what it does with your data. Every line is open and MIT licensed, so you can verify these claims yourself.

  • Your provider key (llm_key) is passed explicitly to the constructor. The SDK never reads it from the environment, never writes it to disk, and never sends it to AgentKavach. It is held only in memory, used solely to call the provider directly from your process.
  • Your AgentKavach key (api_key) is likewise passed explicitly and used only to authenticate spend-tracking requests to the AgentKavach backend. If an api_key is expired or revoked, your LLM calls keep running — the SDK simply stops sending spend data once the backend rejects the key, so a lapsed key never takes your application down.
  • For spend tracking and alerts, the SDK reports the following to the AgentKavach backend on each call: agent name, provider, model, input and output token counts, computed cost, duration, timestamp, and a run identifier.
  • Prompt and response text is sent only when you opt in with save_prompts=True. It is off by default.
  • The budget check runs in your process and in memory, so enforcement does not wait on a network call.

How it works

  1. Before a call, the engine reads the running spend for the active budget and compares it to the limit.
  2. If the limit is already reached, it raises BudgetExceededError and the call never goes out.
  3. Otherwise it forwards the call to the provider, then records the actual cost and usage and checks the configured thresholds.
  4. If the budget is exhausted, the local kill switch runs your on_kill teardown. Alert delivery (email, Slack, PagerDuty, webhook) is performed by the AgentKavach cloud, which aggregates spend across all of your agents and fires each alert once at the true combined total — so a budget shared by many agents alerts correctly instead of each process seeing only its own slice.

AgentKavach is designed to fail open. If anything inside the SDK raises an unexpected error, your LLM call still proceeds. Only the budget and guardrail errors are allowed to propagate.

Open core

The SDK in this repository is open source under the MIT license. The hosted backend and dashboard, which add spend analytics, organization budgets aggregated across processes, and alert delivery, are a separate commercial service at agentkavach.com. They are optional and are not required to read, run, or audit this code.

Documentation

License

Released under the MIT License.

Release files for agentkavach 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agentkavach 2.1.0
File Size Uploaded
agentkavach-2.1.0.tar.gz 60.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for agentkavach 2.1.0
File Interpreter ABI Platform
agentkavach-2.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 128.7 kB

Release files / agentkavach-2.1.0.tar.gz

Download URL agentkavach-2.1.0.tar.gz
Size 60.1 kB
Tags Source
SHA-256 checksum
How to use checksums
66b688ff7c6e96214e0907a54177283434f8c961386b4edded211e10a4b3881e
BLAKE2b-256 checksum
How to use checksums
714151b6b0edb16b7fb63cc474d5096ad0262991ce2381a6e6fa7273e8ef9821
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.

Transparency log

Release files / agentkavach-2.1.0-py3-none-any.whl

Download URL agentkavach-2.1.0-py3-none-any.whl
Size 68.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b95953b0e2b4583886e76c166cac306fe445fdf21a69d666db2eb799268b91e7
BLAKE2b-256 checksum
How to use checksums
70aaa594c12e0372682af9144cddc75fac0ec8c57eb6d521031cc15629a6622e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

2.1.0 This release

2 release files

2.0.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page