Skip to content

ACP & Usage integration with T3 Code: per-turn PromptResponse.usage, local quota snapshot (#1161), and rate-limit reset metadata #1169

Open

Description

@ngthson553-create

Hi Command Code team! 👋

We are working on first-class Command Code (cmd) usage analytics and rate-limit recovery integration for T3 Code (related to CommandCodeAI/command-code#1161 and pingdotgg/t3code#29353).

After auditing command-code@1.93.2 (cmd acp and ~/.commandcode/projects/*/*.jsonl) against T3 Code's UsageService, UsageLimitRecoveryWorker, and provider-acp adapter, we found three concrete integration points that would make Command Code work seamlessly with T3 Code (and other ACP hosts):


1. cmd acp PromptResponse.usage vs _meta.usage (Per-Turn vs Cumulative Session Usage)

In command-code@1.93.2 (promptResult(e, n) in dist/cli.mjs), session/prompt currently returns:

  • Top-level result.usage: cumulative sessionUsage across all turns in the session ({ totalTokens, inputTokens, outputTokens, cachedReadTokens, cachedWriteTokens }).
  • result._meta.usage: the actual per-turn turnUsage ({ inputTokens, outputTokens, cacheReadTokens, cacheWriteTokens }).

In T3 Code (acpPerTurnPromptUsage in packages/provider-acp/src/server/adapter.ts), the ACP adapter reads PromptResponse.usage expecting per-turn token counts (whereas session/update with sessionUpdate: "usage_update" carries cumulative session context/token usage). Because result.usage in cmd acp is cumulative across turns, multi-turn ACP sessions double-count earlier turns when result.usage is summed per turn.

Proposal:

  • Either return per-turn usage in PromptResponse.usage (with standard ACP keys inputTokens, outputTokens, cachedReadTokens, cachedWriteTokens, totalTokens) and emit cumulative session usage via session/update (sessionUpdate: "usage_update"), or document result._meta.usage (cacheReadTokens / cacheWriteTokens) as the stable per-turn usage contract for ACP clients.

2. Local Session Transcript Contract (~/.commandcode/projects/<slug>/<sessionId>.jsonl)

T3 Code's UsageService (apps/server/src/usage/UsageService.ts) scans local provider transcript files incrementally by mtime + byte size to power the 30-day Settings → Usage dashboard (daily cost, model breakdown, cache hit ratio, latency).

Command Code's JSONL transcripts (type: "assistant" entries with messageId, sessionId, timestamp, model, provider, costUsd, durationMs, and usage) are already very close to what T3 Code needs. To rely on them without drift, could you confirm whether the following invariants in 1.93.2 are considered stable?

  1. usage.inputTokens already includes usage.cacheReadTokens + usage.cacheWriteTokens (consistent with toSessionUsage in cli.mjs), so uncached input is max(0, inputTokens - cacheReadTokens - cacheWriteTokens).
  2. usage.cacheWriteTokens1h is a subset of usage.cacheWriteTokens (1-hour TTL cache creation vs 5-minute default), so total cache creation tokens is usage.cacheWriteTokens.
  3. costUsd on each type: "assistant" event is the authoritative per-turn USD cost computed by estimateCatalogCostUsd.

3. Live Rate-Limit Quotas (#1161) & Structured ACP Rate-Limit Reset Metadata

T3 Code has two features that depend on quota windows (5-hour rolling, weekly, monthly, and extra credits):

  1. Live Usage Limits UI (ServerProviderUsageLimits in Settings → Usage).
  2. Automatic Rate-Limit Recovery (UsageLimitRecoveryWorker), which automatically pauses a thread when a turn hits a usage limit and schedules an automatic resume at resetsAt.

Currently, external tools have to read ~/.commandcode/auth.json and call undocumented internal endpoints (/alpha/whoami?limits=1, /alpha/billing/credits, /alpha/billing/subscriptions, /alpha/usage/summary) or run a local CLIProxyAPI shim.

Proposal:

  1. Local quota snapshot (#1161) or CLI subcommand: As proposed in #1161, writing the latest 5-hour / weekly / monthly window percentages and resetAt timestamps to ~/.commandcode/usage.json after each turn (or exposing cmd usage --json) would let T3 Code display live quota bars with zero extra API calls and without touching ~/.commandcode/auth.json.
  2. Structured ACP rate-limit error on session/prompt: When a turn fails due to a 5-hour, weekly, or monthly limit in cmd acp, including a machine-readable resetsAt (ISO-8601) and window identifier in the JSON-RPC error data (e.g., error.data = { kind: "usage_limit", window: "five_hour", resetsAt: "2026-..." }) would allow T3 Code to automatically pause and resume the agent turn as soon as the window resets.

Happy to test any changes or submit the corresponding adapter/usage PR on the T3 Code side!

Activity

  1. ahmadbilaldev commented on Nov 23, 2026

    @ahmadbilaldev
    Contributor

    Taking a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions