Spending limits for Anthropic calls in Python
Supported in 1.2.0
Section titled “Supported in 1.2.0”Anthropic Messages, sync and async, non-streaming and streaming. Verified locally with synthetic provider transports and real local KazenAI services, and also verified against the live OpenAI and Anthropic APIs on 2026-10-12 (gpt-6-luna, claude-sonnet-5-5).
from kazenai_finops import monitor, BudgetExceededimport anthropic
client = monitor(anthropic.Anthropic(max_retries=0), agent_id="support", max_budget_usd=1.0)
# Non-streamingclient.messages.create( model="claude-haiku-4-5", max_tokens=256, messages=[{"role": "user", "content": "Hello"}],)
# Streaming via create(stream=True)for event in client.messages.create( model="claude-haiku-4-5", max_tokens=256, stream=True, messages=[{"role": "user", "content": "Hello"}],): pass
# Streaming via messages.stream context managerwith client.messages.stream( model="claude-haiku-4-5", max_tokens=256, messages=[{"role": "user", "content": "Hello"}],) as stream: for event in stream: pass final = stream.get_final_message()Early context exit or close() leaves financial reconciliation pending when final
usage is unknown.
KazenAI merges input usage from message_start with later output usage from
message_delta or the final Message. Text, tool JSON, thinking, and signature
deltas contribute to the optional local cutoff estimate; provider usage remains
the authority for settlement.
Wrap anthropic.AsyncAnthropic the same way and await the calls:
import asyncio
import anthropicfrom kazenai_finops import monitor, BudgetExceeded
async def main() -> None: client = monitor(anthropic.AsyncAnthropic(max_retries=0), agent_id="support", max_budget_usd=1.0) try: msg = await client.messages.create( model="claude-haiku-4-5", max_tokens=256, messages=[{"role": "user", "content": "Hello"}], )
stream = await client.messages.create( model="claude-haiku-4-5", max_tokens=256, stream=True, messages=[{"role": "user", "content": "Hello"}], ) async for event in stream: pass except BudgetExceeded as exc: print(f"Denied before dispatch: {exc}") finally: await client.close()
asyncio.run(main())Both calls send the same prompt. That’s fine: by default the loop guard records a
repeat and sends it. With monitor(..., loop_guard="block") the second call raises
LoopDetected (from kazenai import LoopDetected) before dispatch, so catch it next to
BudgetExceeded. See Repeated requests.
Not supported
Section titled “Not supported”| Surface | Status |
|---|---|
| Bedrock / Vertex Anthropic wrappers | UNSUPPORTED |
See the full matrix: Supported surfaces.
