Spend SDK quickstart
The SDK has no hard dependency on any provider. You bring your own OpenAI, Anthropic or Bedrock client. The next time your agent tries to spend over the cap, AgentGuard throws before the provider is called, so the blocked request is never dispatched or billed.
- Install.
Terminal window npm install @agentguard-run/spendTerminal window pip install agentguard-spend - Wrap your provider client. One call. Pass in your existing client, declare a cap, use the guarded client the same way.
Every call through the wrapped client now runs a local preflight. Cost is projected from estimated input tokens andimport Anthropic from '@anthropic-ai/sdk';import { withSpendGuardAnthropic } from '@agentguard-run/spend';const policy = {id: 'daily-cap-v1',name: 'Daily cap',scope: { tenantId: 'acme' },caps: [{ amountCents: 2000, window: 'per_day', action: 'block' }],mode: 'enforce',version: 1,effectiveFrom: new Date().toISOString(),};const guarded = withSpendGuardAnthropic(new Anthropic(), {policy,scope: { tenantId: 'acme', agentId: 'my-agent' },});await guarded.messages.create({model: 'claude-opus-4-7',max_tokens: 1024,messages: [{ role: 'user', content: 'hello' }],});from agentguard_spend import easy_installfrom anthropic import Anthropicclient = easy_install(Anthropic(), daily_cap_dollars=20)client.messages.create(model="claude-opus-4-7",max_tokens=1024,messages=[{"role": "user", "content": "hello"}],)max_tokens. If the scope’s daily spend would cross the cap,AgentGuardBlockedErroris thrown and the provider method is not invoked. - Watch it block. Drop the cap to a few cents and trigger a real call.
try {await guarded.messages.create({ /* ... */ });} catch (err) {if (err.name === 'AgentGuardBlockedError') {console.log(err.toString()); // human-readable traceconsole.log(err.decision); // policy decision; not a signed entry by itself}}from agentguard_spend import AgentGuardBlockedErrortry:client.messages.create(...)except AgentGuardBlockedError as err:print(err) # color-coded traceprint(err.decision.action) # "block"print(err.decision.cap_hit) # which cap fired
- Verify a receipt. Direct wrappers create signed entries only when
config.signingKeysis supplied. The CLI demo creates local throwaway keys and a real signed chain.Terminal window agentguard demoagentguard verify# ✓ full chain valid# ✓ entry hashes match canonical JSON# ✓ signatures match supplied public key# ✓ no sequence gaps (ledger appears complete)import { verifyChain } from '@agentguard-run/spend';const result = await verifyChain(entries, publicKey);console.log(result.ok ? 'valid' : result.reason);
CapabilitiesCap windows, actions, scope dimensions, capability tiers, locales, CLI.
npm: @agentguard-run/spendPackage license permits production use up to 10,000 enforcement calls per calendar month.