Skip to content
reaatech

@reaatech/agent-budget-types

npm v0.1.1

Zod-validated TypeScript types, enums, and error classes defining budget scopes, policies, enforcement actions, spend entries, and state transitions for the agent-budget-controller ecosystem. Exports interfaces, enums, and Zod schemas with zero runtime dependencies beyond `zod`.

@reaatech/agent-budget-types

npm version License: MIT CI

Status: Pre-1.0 — APIs may change in minor versions. Pin to a specific version in production.

Core domain types, Zod validation schemas, and error classes for the agent-budget-controller ecosystem. This package is the single source of truth for all budget shapes — scopes, policies, enforcement actions, spend entries, and state transitions — used throughout every @reaatech/agent-budget-* package.

Installation

terminal
npm install @reaatech/agent-budget-types
# or
pnpm add @reaatech/agent-budget-types

Feature Overview

  • 4 budget scopesTask, User, Session, Org with Zod-validated identifiers
  • Budget definition types — dollar limits, cap thresholds (soft/hard), downgrade rules, and tool exclusions
  • 5 enforcement actionsAllow, Warn, Degrade, DisableTools, HardStop as a typed state machine
  • 4-state lifecycleActive → Warned → Degraded → Stopped with transition history tracking
  • Spend entry schema — typed cost events with tokens, model IDs, providers, and timestamps
  • 4 typed error classesBudgetError, BudgetExceededError, BudgetValidationError with structured serialization
  • Zod schemas for every type — parse and validate at runtime boundaries (API, CLI, middleware)
  • Zero runtime dependencies beyond zod — tree-shakeable and lightweight

Quick Start

typescript
import { BudgetScope, BudgetPolicy, type SpendEntry } from '@reaatech/agent-budget-types';
 
const scope = { scopeType: BudgetScope.User, scopeKey: 'user-42' };
 
const policy: BudgetPolicy = {
  softCap: 0.8,
  hardCap: 1.0,
  autoDowngrade: [{ from: ['claude-opus-4-1'], to: 'claude-sonnet-4' }],
  disableTools: ['expensive-web-search'],
};

Exports

Budget Scopes

ExportKindDescription
BudgetScopeenumTask, User, Session, Org
ScopeIdentifierinterface{ scopeType; scopeKey }
ScopeResolver<TContext>type(context: TContext) => ScopeIdentifier | null

Budget Configuration

ExportKindDescription
BudgetPolicyinterfaceSoft/hard caps, downgrade rules, disabled tools
BudgetDefinitioninterfaceFull budget: scope, dollar limit, policy

Enforcement & State Machine

ExportKindDescription
EnforcementActionenumAllow, Warn, Degrade, DisableTools, HardStop
BudgetStateEnumenumActive, Warned, Degraded, Stopped
StateTransitioninterfaceRecords each state machine transition
EnforcementResultinterfaceOutcome of a budget enforcement check

Spend Tracking

ExportKindDescription
SpendEntryinterfaceSingle cost event: requestId, tokens, model, provider, cost
SpendQueryinterfaceFilter criteria for querying spend history
BudgetStateinterfaceFull runtime budget state with spent/remaining/breach count
BudgetSnapshotinterfaceLightweight budget status summary

Downgrade & Tools

ExportKindDescription
DowngradeRuleinterfaceMaps model IDs to cheaper alternatives (supports wildcards)
ToolCostConfiginterfaceAssigns cost weight to a tool
ToolFilterResultinterfaceResult of filtering tool lists

Budget Checks

ExportKindDescription
BudgetCheckRequestinterfacePre-flight check request with estimated cost
BudgetCheckResultinterfaceCheck result with allowed/blocked/suggested model

Error Classes

ExportKindDescription
BudgetErrorclassBase error with code and optional scope
BudgetExceededErrorclassHard-cap violation with spent/limit/remaining
BudgetValidationErrorclassInvalid input with optional field
BudgetErrorCodeenumBUDGET_EXCEEDED, BUDGET_VALIDATION, BUDGET_NOT_FOUND, BUDGET_INTERNAL

Usage Pattern

Every type has a corresponding Zod schema for runtime validation:

typescript
import { BudgetPolicySchema, BudgetDefinitionSchema } from '@reaatech/agent-budget-types';
 
const raw = JSON.parse(userInput);
const validated = BudgetDefinitionSchema.parse(raw); // throws BudgetValidationError on failure

Error classes support structured serialization for API responses:

typescript
import { BudgetExceededError } from '@reaatech/agent-budget-types';
 
throw new BudgetExceededError({
  scope: { scopeType: 'user', scopeKey: '42' },
  spent: 10.0,
  limit: 10.0,
  remaining: 0,
});
// BudgetExceededError.toJSON() → { name, message, code, spent, limit, ... }

License

MIT