This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
npm install # Install dependencies
npm run build # Build TypeScript to dist/
npm run dev # Build in watch mode
npm test # Run tests with Vitest
npm run test:ui # Run tests with Vitest UI
npm run test:coverage # Run tests with coverage report
npm run lint # Lint with ESLint
npm run typecheck # Type check without buildingnpx vitest -t "test name pattern" # Run tests matching pattern
npx vitest path/to/test.ts # Run specific test fileThis is a stateless rule engine for AI usage routing and cost control. The engine evaluates declarative rules against contexts to determine model routing and enforce limits.
-
Schemas (
src/schema/): Zod schemas define and validate all data structurescondition.ts: Matching patterns (equality, comparison, logical operators)action.ts: What happens when rules match (model selection, limits, fallbacks)rule.ts: Complete rule structure with if/thencontext.ts: Runtime data including usage and window tracking
-
Types (
src/types/): TypeScript types inferred from Zod schemas -
Engine (
src/engine/):evaluate.ts: Main entry point - evaluates rules by prioritymatcher.ts: Recursive condition matching with dot notation supportlimiter.ts: Checks usage limits against windows or raw usage
The engine doesn't track time windows itself. Instead:
- Rules define limits with windows (e.g., "50k tokens per day")
- The calling system tracks usage over time
- Window data is passed in
context.windowswith current usage and reset times - If no window data exists, falls back to checking raw
usagevalues
Example context with window tracking:
{
usage: { tokens: 60000, ... }, // Total usage (fallback)
windows: {
'tokens_day_user_123': { // Tracked by caller
current: 45000,
limit: 50000,
resets_at: '2025-07-28T00:00:00Z'
}
}
}- Rules are sorted by priority (higher first)
- First matching rule wins
- Same priority: earlier in array wins
Conditions support nested property access:
{ 'usage.cost': { lt: 100 } }checkscontext.usage.cost < 100- Implemented in
utils/path.ts
Tests use Vitest and cover:
- Tier-based routing
- Limit enforcement (with and without window data)
- Model downgrade on limit exceeded
- Priority resolution
- Complex logical conditions
- Window expiry
Note: Many tests demonstrate the fallback behavior where window tracking isn't provided, so limits check against raw usage values.