@agenticprimitives/rate-control
v0.0.0-alpha.1
Published
Vendor-neutral traffic-rate-limit + hard-usage-budget controls: VerifiedAuthorityContext, typed RateControlProfiles, cost units, HMAC-keyed buckets, and SoftRateLimiter/HardBudgetStore/ConcurrencyLimiter interfaces with in-memory reference impls. Cloudfla
Maintainers
Readme
@agenticprimitives/rate-control
Vendor-neutral traffic-rate-limit + hard-usage-budget controls — the two operational controls
that are separate from the authority/replay controls (delegation usageLimit + JTI). Every invocation
must satisfy both the authority controls AND the operational limits; a platform limiter never replaces the
authority decision, and its rejection never consumes a delegation use.
See spec 290.
What's in the box (W1 core)
VerifiedAuthorityContext— the trustworthy context emitted by the verify path; every post-verification limiter keys on this, never a claimed address.QuotaSubject— principal / actor / sponsor (they may differ: an Org delegates to a Treasury Agent while the Org pays).RateControlProfile— typedattemptLimits(pre-verification: ip/request-hash/capability) +verifiedLimits(post-verification: principal/actor/sponsor/delegation/capability) +concurrency+hardBudget+failMode. This is what@sa-rate-limitresolves to.InvocationCost+costToUnits— counts protect throughput; cost units protect RPC/KMS/provider/ model spend.rateKey— HMAC-SHA256 opaque bucket keys, so raw addresses / delegation hashes / entitlement refs never appear in infra logs or dashboards.- Three interfaces —
SoftRateLimiter,HardBudgetStore,ConcurrencyLimiter— with in-memory reference implementations (createMemorySoftRateLimiter/createMemoryHardBudgetStore/createMemoryConcurrencyLimiter). The hard-budget store is the reserve/commit/release contract the CloudflareSmartAgentBudgetDOmust match.
import {
quotaSubjectFromContext, rateKey, costToUnits, createMemoryHardBudgetStore,
} from '@agenticprimitives/rate-control';
const subject = quotaSubjectFromContext(verifiedCtx); // principal/actor/sponsor
const key = await rateKey(SECRET, { environment, chainId, principalAgent: subject.principalAgent, capabilityId });
const budget = createMemoryHardBudgetStore({ limitUnits: 10_000 });
const r = await budget.reserve({ sponsorAgent: subject.sponsorAgent, capabilityId, estimatedUnits: costToUnits({ base: 1, kmsOperations: 1 }), idempotencyKey: requestId });
try { /* …execute… */ await budget.commit(r.reservationId!, actualUnits); }
catch (e) { await budget.release(r.reservationId!); throw e; }Not in the core
- The Cloudflare (Worker rate-limiter +
SmartAgentBudgetDO), Vercel, and Redis adapters — ship as@agenticprimitives/rate-control-{cloudflare,vercel,redis}(spec 290 §7–§8). The core imports no vendor SDK. - The Phase-1.5 usage refactor (delegation/a2a reserve-commit-release of JTI / message-id) — a separate core-auth change.
License
MIT
