On a supported TypeScript integration, add configured spend caps, declared capability checks, and an opt-in circuit breaker before dispatch. Signed metadata receipts require configured Ed25519 keys.
AgentGuardBlockedError before that hook dispatches to the provider. Ed25519 signing requires configured keys and a decision store.
npm install @agentguard-run/spend # Node / TypeScript
pip install agentguard-spend # Python
The exported handler preflights LangChain.js chat-model and LLM callback starts, then settles usage on completion.
import { ChatOpenAI } from '@langchain/openai';
import { createLangChainHandler } from '@agentguard-run/spend/frameworks/langchain';
const guard = createLangChainHandler({
policy,
scope: { tenantId: 'acme', agentId: 'research-agent' },
});
const llm = new ChatOpenAI({ callbacks: [guard] });
await llm.invoke('hello'); // throws AgentGuardBlockedError if over cap
One middleware on your language model. Works with generateText and streaming.
import { wrapLanguageModel } from 'ai';
import { openai } from '@ai-sdk/openai';
import { agentguard } from '@agentguard-run/spend/frameworks/vercel-ai';
const model = wrapLanguageModel({
model: openai('gpt-4o-mini'),
middleware: agentguard({
policy: { caps: [{ amountCents: 2000, window: 'per_day', action: 'block' }] },
scope: { tenantId: 'acme', agentId: 'assistant' },
}),
});
Anthropic and OpenAI-compatible clients have distinct exported wrappers because their request and response shapes differ.
import Anthropic from '@anthropic-ai/sdk';
import { withSpendGuardAnthropic } from '@agentguard-run/spend';
const guarded = withSpendGuardAnthropic(new Anthropic(), {
policy,
scope: { tenantId: 'acme', agentId: 'my-agent' },
});