Skip to content

Getting started

Goal Install
Product integrations (recommended) pip install "kazenai-finops[openai]==1.2.6"
Core engine only pip install "kazenai[openai]==1.2.6"
Event contract only pip install kazen-event-schema

Most teams should install kazenai-finops. It re-exports monitor() from core. Prefer provider extras so the first-run install stays inside the supported SDK ranges.

Terminal window
python -m pip install "kazenai-finops[openai]==1.2.6"
# Optional Anthropic path:
# python -m pip install "kazenai-finops[anthropic]==1.2.6"
# Both providers:
# python -m pip install "kazenai-finops[providers]==1.2.6"

kazenai-finops 1.2.6 installs kazenai 1.2.6 or later. To upgrade an existing install, run the same pip install command.

Requires Python 3.10–3.14 (the CrewAI adapter: 3.10–3.13).

Provider extras bound openai>=1.40,<3 and anthropic>=0.39,<1. New provider-SDK major versions are not implied compatible until they are added to the supported-surface matrix. Async clients are supported too — see the OpenAI and Anthropic guides.

from kazenai_finops import monitor, BudgetExceeded, KazenCircuitBreaker
import openai
client = openai.OpenAI(max_retries=0)
monitored = monitor(
client,
agent_id="customer-support",
max_budget_usd=5.00,
)
try:
monitored.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Summarize the open tickets"}],
)
except BudgetExceeded as exc:
print("Hard cap before provider:", exc)
except KazenCircuitBreaker as exc:
print("Soft pause after a completed call:", exc)

Sending the same prompt again is fine. By default monitor() only records a request that repeats like a loop: it writes a finops.loop.anomaly event with "reason": "repeated_request" and sends the call. To refuse such a request before it reaches the provider, pass loop_guard="block" and catch LoopDetected:

from kazenai_finops import BudgetExceeded, LoopDetected, monitor
monitored = monitor(client, agent_id="customer-support", max_budget_usd=5.00, loop_guard="block")
try:
monitored.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Summarize the open tickets"}],
)
except LoopDetected as exc:
print("Refused as a repeat, before the provider:", exc)
except BudgetExceeded as exc:
print("Hard cap before provider:", exc)

The history is kept per customer (the call’s business_subject_ref), so two customers asking the same question are never a loop. Calls that name no customer share one history per client. Within one customer, "block" also refuses ordinary traffic that repeats: a long shared system prompt with short questions, or an agent that sends the same tools list on each call (from its third call). loop_guard="off" skips the check. Up to 1.2.2, blocking was the default and the history was shared by every customer on the client.

To enforce a limit per customer across every process, connect Agent FinOps:

Terminal window
export KAZENAI_FINOPS_URL=https://finops.example.com # your Agent FinOps URL
export KAZENAI_FINOPS_API_KEY=kz_...
export KAZENAI_TIER_MISMATCH_MULTIPLIER=2 # required under Control
monitored = monitor(
client,
agent_id="customer-support",
org_id="<organization ID>", # both IDs are on the Setup page in Agent FinOps
workspace_id="<workspace ID>",
max_budget_usd=5.00,
require_customer_id=True, # refuse a call that names no customer
)

An Agent FinOps API key belongs to one organization and one workspace. Pass the same two IDs to monitor(); the Setup page in Agent FinOps shows them and fills them into its code snippet. monitor() does not read them from the key or from KAZENAI_ORG_ID. Without them it uses local and default, Agent FinOps refuses each reservation with HTTP 403, and the call raises BudgetUnavailable before it reaches the provider.

Name the customer on each call. A per-customer limit in Agent FinOps applies only to calls that carry that customer’s ID as business_subject_ref:

from kazenai_finops import use_attribution
with use_attribution(business_subject_ref="cust_123"):
monitored.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Summarize this ticket"}],
)

Use the same ID the limit was set for. A call without a customer ID is checked against the organization and workspace limits only; with require_customer_id=True it raises CustomerIdRequired before it reaches the provider instead. Worker threads and other ways to pass the ID: Attribution.

Setting the FinOps URL selects the Control lifecycle: the monitor() call reserves in Agent FinOps before dispatch and settles after it, and refuses the call when Agent FinOps cannot be reached. Under Control, monitor() refuses to start without the multiplier or with a provider client built with retries, so keep max_retries=0. Details: Configuration.