Skip to content

feat: usage tracking — calls per 5h window (#268)#424

Open
yxlyx wants to merge 1 commit intomainfrom
feat/268-usage-tracking
Open

feat: usage tracking — calls per 5h window (#268)#424
yxlyx wants to merge 1 commit intomainfrom
feat/268-usage-tracking

Conversation

@yxlyx
Copy link
Copy Markdown
Collaborator

@yxlyx yxlyx commented Apr 10, 2026

Summary

  • UsageTracker (src/runtime/usage.zig): Thread-safe call counter with a rolling 5-hour window
    • record(backend): increment call count per backend
    • nearLimit(backend, quota, warn_pct): check if calls exceed threshold
    • callCount() / totalCalls(): current window counts
    • windowRemainingSeconds(): time left in window
    • Automatic window reset when 5h expires
    • Global singleton via usage.global() with lazy init
  • dispatch.zig: Every dispatch() call now records usage via usage.global().record()

This provides the counting layer (Layer 0 per issue spec). The load-balancing in resolve() (Layer 1) can use nearLimit() to overflow to the secondary provider.

Test plan

  • zig test src/runtime/usage.zig — 12/12 tests pass (9 usage + 3 types)
  • record: increments correct backend counter
  • nearLimit: triggers at threshold (80% of 10 = 8 calls)
  • nearLimit: does not trigger below threshold
  • nearLimit: zero quota returns false
  • Window reset: stale counts cleared after 5h
  • windowRemainingSeconds: within window (~18000s), expired (0)
  • Global singleton: same pointer, shared state
  • Concurrent safety: 4 threads x 100 records = 400 total

Closes #268

Made with Cursor

Adds UsageTracker to src/runtime/usage.zig:
- Rolling 5-hour window with automatic reset
- Thread-safe: all mutations guarded by mutex
- record(backend): increments call count for claude or codex
- nearLimit(backend, quota, warn_at_percent): checks if calls
  exceed threshold fraction of quota
- callCount/totalCalls: current window counts
- windowRemainingSeconds: time left in current window
- Global singleton via usage.global() with lazy init

Wires into dispatch.zig: every dispatch() call now records
usage for the dispatched backend.

9 new tests: record, nearLimit (trigger/below/zero-quota),
window reset, remaining seconds, global singleton,
concurrent safety with 4 threads x 100 records.
All 12 tests pass (9 usage + 3 types).

Made-with: Cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: usage tracking — calls per 5h window, overflow to secondary provider

1 participant