token-budget

$npx mdskill add Tibsfox/gsd-skill-creator/token-budget

Enforces token budgets before API calls in convoy execution.

  • Prevents token overspend by checking budgets before API calls.
  • Depends on convoy and agent budget tracking state.
  • Checks projected token cost against remaining budget before execution.
  • Returns structured stop reasons when budget is exceeded.

SKILL.md

.github/skills/token-budgetView on GitHub ↗
---
name: token-budget
description: Token budget tracking and enforcement for Gastown convoy-level execution. Hard limits with pre-execution checking, per-convoy and per-agent tracking, structured stop reasons.
type: skill
category: state
status: stable
origin: tibsfox
modified: false
first_seen: 2026-04-04
first_path: .claude/skills/token-budget/SKILL.md
superseded_by: null
---
# Token Budget Enforcement

Pre-execution budget gating for multi-agent convoy execution. Prevents token overspend by checking budgets BEFORE API calls, not after. Identified by the 12 Primitives analysis (Primitive 5) as the #1 actionable improvement.

## Activation

This skill activates when:
- A convoy execution starts (mayor creates a convoy)
- Agents are spawned within a convoy
- Any agent is about to make an API call during convoy execution
- Budget reporting is requested during or after execution

## Architecture

### Budget Hierarchy

```
Convoy Budget (hard limit, default 500K tokens)
  |
  +-- Agent A budget (hard limit, default 100K tokens)
  +-- Agent B budget (hard limit, default 100K tokens)
  +-- Agent C budget (hard limit, default 100K tokens)
```

The convoy budget is the aggregate ceiling. Individual agent budgets prevent any single polecat from consuming a disproportionate share.

### Check-Before-Execute Pattern

Every API call in a convoy MUST follow this sequence:

1. **Estimate** the projected token cost for the call
2. **Check** `checkBudget(budget, agentId, projectedCost)` — returns `BudgetCheckResult`
3. **If `allowed: false`** — stop immediately, do NOT make the API call
4. **If `reason: 'warning_threshold'`** — proceed but log the warning
5. **If `reason: 'ok'`** — proceed normally
6. **After execution** — `recordUsage(budget, agentId, actualInput, actualOutput)`
7. **Persist** — `saveBudget(budget, budgetDir)` to survive crashes

### Structured Stop Reasons

| Reason | Meaning | Action |
|--------|---------|--------|
| `ok` | Under budget, no concerns | Proceed |
| `warning_threshold` | Past warning % but under hard limit | Proceed, log warning |
| `convoy_budget_exceeded` | Convoy would exceed hard limit | STOP, do not call API |
| `agent_budget_exceeded` | Agent would exceed its limit | STOP, do not call API |

## Core API

### Types

```typescript
interface TokenBudget {
  convoyId: string;
  maxTokensPerConvoy: number;      // Hard limit for entire convoy
  maxTokensPerAgent: number;       // Hard limit per polecat
  warningThresholdPercent: number;  // Warn at this % (e.g., 80)
  currentUsage: BudgetUsage;
  createdAt: string;               // ISO 8601
  updatedAt: string;               // ISO 8601
}

interface BudgetCheckResult {
  allowed: boolean;
  reason: 'ok' | 'warning_threshold' | 'convoy_budget_exceeded' | 'agent_budget_exceeded';
  remainingTokens: number;
  usagePercent: number;
}
```

### Functions

| Function | Signature | Description |
|----------|-----------|-------------|
| `createBudget` | `(convoyId, config?) => TokenBudget` | Initialize a budget for a convoy |
| `checkBudget` | `(budget, agentId, projectedCost) => BudgetCheckResult` | Pre-execution gate check |
| `recordUsage` | `(budget, agentId, input, output) => void` | Track actual usage after execution |
| `getBudgetReport` | `(budget) => BudgetReport` | Summary for logging/display |
| `saveBudget` | `(budget, budgetDir) => Promise<void>` | Persist to `.chipset/state/budgets/` |
| `loadBudget` | `(convoyId, budgetDir) => Promise<TokenBudget \| null>` | Load from disk |
| `deleteBudget` | `(convoyId, budgetDir) => Promise<void>` | Remove budget file |
| `listBudgets` | `(budgetDir) => Promise<string[]>` | List all persisted convoy budget IDs |

### Default Values

| Parameter | Default | Recalibration note |
|-----------|---------|--------------------|
| `maxTokensPerConvoy` | 500,000 tokens | Was set conservatively pre-INLINE-SERIAL pattern. v1.49.621 actuals: ~50-300K per convoy under inline-serial Opus/Sonnet authoring (~10-15% of this default). Default retained for safety margin; missions may explicitly cap lower based on convoy shape. |
| `maxTokensPerAgent` | 100,000 tokens | Pre-recursive-spawn-block default; with INLINE SERIAL the agent IS the convoy, so per-agent ≈ per-convoy. |
| `warningThresholdPercent` | 80% | Unchanged. |

**v1.49.621 retrospective lesson 3 — projection recalibration:** Wave 1+2+3+4 fleet token spend came in at ~26% of projected ceiling under INLINE SERIAL authoring. Future missions should project Opus convoys at ~75-300K and Sonnet convoys at ~30-150K based on output volume × ~1.5K tokens/100-line-of-output heuristic. Reserve 3-5× headroom over the projection for safety; do not 10× as this skill historically did.

## State Persistence

**Path:** `.chipset/state/budgets/{convoyId}.json`

Follows the same durability contract as beads-state:
- Atomic writes (write temp -> fsync -> rename)
- JSON with sorted keys for git-friendly diffs
- Filesystem-only, no database dependencies
- Crash-recoverable (partial writes leave only temp files)

## Integration Points

### Mayor Coordinator

When the mayor creates a convoy, it should also create a token budget:

```typescript
const convoy = await stateManager.createConvoy('Sprint 1', beadIds);
const budget = createBudget(convoy.id, {
  maxTokensPerConvoy: 500_000,
  maxTokensPerAgent: 100_000,
});
await saveBudget(budget, '.chipset/state/budgets');
```

### Polecat Worker

Before each API call in GUPP autonomous mode:

```typescript
const budget = await loadBudget(convoyId, '.chipset/state/budgets');
const check = checkBudget(budget!, agentId, estimatedTokens);
if (!check.allowed) {
  // Structured stop — include reason in termination message
  return { stopped: true, reason: check.reason, remaining: check.remainingTokens };
}
// ... make API call ...
recordUsage(budget!, agentId, actualInput, actualOutput);
await saveBudget(budget!, '.chipset/state/budgets');
```

### Witness Observer

The witness can periodically check budget health:

```typescript
const budget = await loadBudget(convoyId, '.chipset/state/budgets');
const report = getBudgetReport(budget!);
if (report.warningActive) {
  // Alert: convoy approaching budget limit
}
```

## Module Location

- **Implementation:** `src/chipset/gastown/token-budget.ts`
- **Tests:** `src/chipset/gastown/token-budget.test.ts`
- **Barrel export:** `src/chipset/gastown/index.ts`

More from Tibsfox/gsd-skill-creator

SkillDescription
3d-interaction-designDesign heuristics for three-dimensional interaction — input modalities (6DoF controllers, gaze, gesture, voice), selection and manipulation techniques (ray-cast, virtual hand, go-go, world-in-miniature), navigation (teleport, continuous, redirected walking), and feedback loops (haptic, audio, visual). Covers Fitts' law in 3D, discoverability vs affordance, safe-zone design, and comfort/simulator sickness mitigation. Use when designing VR/AR interactions, immersive walkthroughs, or any input system where the user's body is the controller.
accessibility-patternsProvides web accessibility best practices for semantic HTML, ARIA, keyboard navigation, color contrast, and screen reader patterns. Use when building UI components, reviewing accessibility, or when user mentions 'a11y', 'accessibility', 'ARIA', 'screen reader', 'keyboard navigation', 'WCAG'.
active-listeningActive listening techniques for effective communication. Covers attending behaviors, paraphrasing, reflective listening, clarifying questions, empathic response, barriers to listening, listening in conflict, and cross-cultural listening. Use when building listening skills, improving understanding in conversation, mediating disputes, or analyzing communication breakdowns.
adversarial-pr-reviewAdversarial spec-compliance PR review — cross-references diffs against approved specs, verifies runtime claims against source, detects competing PRs, audits scope/convention compliance. Use before merging.
agile-methodsAgile and Lean software development methodologies for iterative, adaptive project execution. Covers Scrum (roles, ceremonies, artifacts), Kanban (WIP limits, flow metrics), XP (TDD, pair programming, CI), SAFe overview, Lean Software Development (Poppendieck's 7 principles), user stories (INVEST criteria), estimation via story points and velocity, and the Agile Manifesto's values and principles. Includes Cynefin framework for methodology selection and GSD's relationship to Agile as a structured Lean variant.
algebraic-reasoningSymbolic manipulation, equation solving, and algebraic structures for mathematical reasoning. Covers distributive law, factoring, completing the square, linear through polynomial equation solving, systems of equations (substitution, elimination, Gaussian elimination, matrix methods), algebraic structures (groups, rings, fields), modular arithmetic, polynomial theory, and inequalities. Use when solving equations, simplifying expressions, working with algebraic structures, or performing symbolic manipulation.
algorithmic-awarenessUnderstanding how algorithmic systems shape what users see, know, and do -- from recommendation feeds to search ranking to credit scoring to hiring software. Covers the mechanics of recommendation systems, algorithmic bias and its sources, personalization's effects on information diets, opacity and accountability, AI limitations (hallucination, confident wrongness), and the human-in-the-loop question. Use when a learner needs to think critically about why particular content reached them.
algorithms-data-structuresAlgorithms and data structures from first principles through advanced analysis. Covers sorting (bubble, insertion, selection, merge, quick, heap, radix), searching (linear, binary, BFS, DFS, Dijkstra, A*), fundamental data structures (arrays, linked lists, stacks, queues, hash tables, trees, heaps, graphs, tries), complexity analysis (Big-O, Big-Omega, Big-Theta, amortized), recurrence relations, and algorithm design paradigms (divide-and-conquer, greedy, dynamic programming, backtracking). Use when analyzing, selecting, implementing, or comparing algorithms and data structures.
aminet-browserAminet search and browse: full-text search, category tree navigation, architecture filtering, package detail, and curated collections. Use when searching, browsing, or managing package collections.
aminet-emulatorFS-UAE emulator configuration and launch: hardware profiles, ROM management, WHDLoad integration, config generation, and state snapshots. Use when configuring emulation, managing ROMs, or launching Amiga software.