Skip to content

feat(service-ai): optional per-turn daily chat quota (ADR-0040 §5) — default off#1760

Merged
os-zhuang merged 1 commit into
mainfrom
feat/agent-chat-daily-quota
Jun 12, 2026
Merged

feat(service-ai): optional per-turn daily chat quota (ADR-0040 §5) — default off#1760
os-zhuang merged 1 commit into
mainfrom
feat/agent-chat-daily-quota

Conversation

@os-zhuang

Copy link
Copy Markdown
Contributor

Why

ADR-0040 §5's perception rule: limits that can only manifest as refusal (quota exhausted) must be honest at the moment of impact — why, when it recovers, the way out — and must never degrade silently. This PR ships the mechanism for daily AI usage quotas; the policy (free/pro tier numbers) stays in deployment config and later plan entitlements. Companion to cloud#242 (blueprint scope cap) — together they are Phase 2a of the tiering plan.

What

  • AgentChatQuota hook on buildAgentRoutes(…, {quota}): check() before each user turn, consume() exactly once when admitted (per turn — not per tool call, not per token). No quota wired → byte-identical behavior.
  • Honest refusal, both modes: stream mode returns the copy as a normal assistant message through encodeVercelDataStream (zero client changes, chat never shows a raw error); JSON mode returns 429 with stable code: 'ai_quota_exhausted' + resetAt.
  • DailyMessageQuota: N user turns / user / environment / UTC day, persisted in the new ai_usage_daily platform object via IDataEngine (survives restarts, shared across instances). Fail-open on counter errors — a broken counter must never take chat down. Explicitly documented as an entitlement gate, not billing-grade metering.
  • Opt-in wiring: AI_DAILY_USER_MESSAGES=<N> env var; unset/invalid → quota disabled (logged).

Tests

  • 10 new tests: under/at limit, honest copy (why+when+way-out), insert-then-increment, fail-open, per-env/per-user scoping, route pass-through + consume-once, 429 envelope, streamed refusal framing, no-quota baseline, environmentId forwarding.
  • service-ai suite 344/345 in the worktree — the one failure (embedder binding) fails on the unmodified baseline in the same worktree (env-only, verified via git stash) and passes in the main checkout; CI is the arbiter.

Follow-ups

  • Plan-tier AgentChatQuota implementation (free/pro) once pricing tiers are decided — plugs into this gate unchanged.
  • Optional remaining surfacing in the chat UI (progressive disclosure only near the limit).

🤖 Generated with Claude Code

…t route (ADR-0040 §5)

Mechanism only, default OFF — no behavior change unless a deployment sets
AI_DAILY_USER_MESSAGES=<N>:

- AgentChatQuota hook on buildAgentRoutes: checked before each user turn,
  consumed exactly once when admitted (per turn, not per tool call).
- Refusal is honest at the moment of impact (perception rule): streaming
  clients receive the copy as a normal assistant message (no client change,
  no raw transport error); JSON clients get 429 + code 'ai_quota_exhausted'
  + resetAt.
- DailyMessageQuota: N user turns / user / environment / UTC day, backed by
  the new ai_usage_daily platform object via IDataEngine. Counter semantics
  (lost update under-counts one turn) — entitlement gate, not billing.
  Fail-open on store errors: a broken counter never takes chat down.
- Plan-tier policies (free vs pro) later implement AgentChatQuota and plug
  into the same gate; route and counter unchanged.

10 new tests (quota policy + route gate, both modes, env scoping, fail-open).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@vercel

vercel Bot commented Jun 12, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
spec Ready Ready Preview, Comment Jun 12, 2026 3:27am

Request Review

@os-zhuang
os-zhuang merged commit 57abe7a into main Jun 12, 2026
12 checks passed
@os-zhuang
os-zhuang deleted the feat/agent-chat-daily-quota branch June 12, 2026 03:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant