| name | cost-budget-enforcement |
| description | Set per-request, per-session, daily, and monthly spend limits, configure rate limiting and circuit breakers, and isolate costs per user or tenant. |
| compatibility | Reactive Agents TypeScript projects using @reactive-agents/* |
| metadata | {"author":"reactive-agents","version":"2.0","tier":"capability"} |
Cost and Budget Enforcement
Agent objective
Produce a builder with cost tracking, budget limits, and rate limiting configured so the agent never exceeds defined spending thresholds.
When to load this skill
- Deploying agents in production with real API costs
- Building multi-tenant SaaS where per-user cost isolation matters
- Protecting against runaway agent loops consuming excessive tokens
- Adding circuit breakers for provider reliability
Implementation baseline
import { ReactiveAgents } from "@reactive-agents/runtime";
const agent = await ReactiveAgents.create()
.withName("assistant")
.withProvider("anthropic")
.withReasoning({ defaultStrategy: "adaptive", maxIterations: 15 })
.withTools({ allowedTools: ["web-search", "http-get", "checkpoint"] })
.withCostTracking({
perRequest: 0.25,
perSession: 2.0,
daily: 10.0,
monthly: 100.0,
})
.withRateLimiting({
requestsPerMinute: 30,
tokensPerMinute: 50_000,
maxConcurrent: 3,
})
.withCircuitBreaker()
.build();
Key patterns
withCostTracking() — budget limits
.withCostTracking()
.withCostTracking({
perRequest: 0.50,
perSession: 5.0,
daily: 25.0,
monthly: 200.0,
})
When a budget is exceeded, the agent throws a BudgetExceededError and stops. Daily/monthly budgets reset based on the timezone configured in .withGateway() (if used) or UTC by default.
withRateLimiting() — throughput caps
.withRateLimiting()
.withRateLimiting({
requestsPerMinute: 60,
tokensPerMinute: 100_000,
maxConcurrent: 10,
})
Requests that exceed limits are queued (not dropped) — the agent waits for capacity before proceeding.
withCircuitBreaker() — provider reliability
.withCircuitBreaker()
.withCircuitBreaker({
failureThreshold: 5,
windowMs: 60_000,
retryAfterMs: 30_000,
})
Circuit breaker states: closed (normal) → open (failing fast) → half-open (probing recovery).
Per-user cost isolation (multi-tenant)
const userAgent = await ReactiveAgents.create()
.withProvider("anthropic")
.withCostTracking({ perSession: 1.0, daily: 5.0 })
.withName(`user-${userId}`)
.withSystemPrompt(`You are assisting user ${userId}.`)
.build();
const result = await agent.run(task, {
context: { userId, tenantId },
});
Dynamic pricing (LiteLLM / custom providers)
import { createLiteLLMPricingProvider } from "@reactive-agents/llm-provider";
.withDynamicPricing(createLiteLLMPricingProvider())
CostTrackingOptions reference
| Field | Type | Default | Notes |
|---|
perRequest | number | 1.00 | Max USD per single LLM request |
perSession | number | 5.00 | Max USD per agent.run() call |
daily | number | 20.00 | Max USD per calendar day |
monthly | number | 200.00 | Max USD per calendar month |
RateLimiterConfig reference
| Field | Type | Default | Notes |
|---|
requestsPerMinute | number | 60 | Max LLM requests/minute |
tokensPerMinute | number | 100_000 | Max tokens/minute (input + output) |
maxConcurrent | number | 10 | Max simultaneous in-flight requests |
Pitfalls
- Budget limits are enforced per-process — multiple processes running the same agent each get their own daily/monthly counters; use an external store for true cross-process budget tracking
withCostTracking() with no args is still useful — it enables cost telemetry without enforcing limits (all defaults are generous)
withCircuitBreaker() opens on LLM provider errors, not on budget exceeded errors — they are independent systems
- Rate limiting queues requests rather than dropping them — set
maxConcurrent based on your provider's actual concurrency limits to avoid provider-side 429s
withDynamicPricing() makes an external HTTP call during build — ensure network access and handle build failures
- Daily budget resets at midnight UTC by default — to use a different timezone, configure it via
.withGateway({ timezone: "America/New_York" })