> For the complete documentation index, see [llms.txt](https://ai-os-and-trend-finder.gitbook.io/ai-os-and-trend-finder-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ai-os-and-trend-finder.gitbook.io/ai-os-and-trend-finder-docs/.spec_system/archive/phases/phase_21/session_03_authoritative_claude_usage_oauth.md).

# Session 03: Authoritative Claude Usage OAuth

**Session ID**: `phase21-session03-authoritative-claude-usage-oauth` **Status**: Not Started **Estimated Tasks**: \~18-25 **Estimated Duration**: 3-4 hours

***

## Objective

Add a null-safe, redacted, timeout-bounded Claude OAuth usage reader so the dashboard can prefer Claude Code's authoritative `/usage` numbers while silently falling back to local-log estimates on any failure.

***

## Source Reference Detail

* v2.3 defines `ClaudeAuthoritativeUsage`, `readClaudeCredential`, `detectClaudeCodeVersion`, and `fetchClaudeOAuthUsage`: [V23/scripts/aggregate.ts:588](https://github.com/moshehbenavraham/ai-os/blob/main/home/aiwithapex/projects/claudeos/claude-os-v2.3/scripts/aggregate.ts:588) through `:755`.
* v2.3 calls the reader during aggregation: [V23/scripts/aggregate.ts:1153](https://github.com/moshehbenavraham/ai-os/blob/main/home/aiwithapex/projects/claudeos/claude-os-v2.3/scripts/aggregate.ts:1153).
* v2.3 rebuilds `claudeWindow` with `source`, `resetsAt`, and the authoritative payload: [V23/scripts/aggregate.ts:1191](https://github.com/moshehbenavraham/ai-os/blob/main/home/aiwithapex/projects/claudeos/claude-os-v2.3/scripts/aggregate.ts:1191) through `:1267`.
* aios currently has no `oauth/usage` or `readClaudeCredential` implementation in `scripts/lib/` or `src/lib/`; it builds the Claude usage window from local heuristics in `AIOS/scripts/lib/usage-assembly.ts` and `AIOS/scripts/lib/subscription-detection.ts`.
* aios credential-read convention lives in [AIOS/scripts/lib/subscription-detection.ts:40](https://github.com/moshehbenavraham/ai-os/blob/main/home/aiwithapex/projects/aios/scripts/lib/subscription-detection.ts:40) through `:61` for `IS_MACOS`, `Bun.spawn`, and a two-second timeout race, and [AIOS/scripts/lib/subscription-detection.ts:67](https://github.com/moshehbenavraham/ai-os/blob/main/home/aiwithapex/projects/aios/scripts/lib/subscription-detection.ts:67) through `:89` for safe validators.
* The usage-panel target for the live-vs-estimate badge starts at [AIOS/src/components/usage-panel.tsx:23](https://github.com/moshehbenavraham/ai-os/blob/main/home/aiwithapex/projects/aios/src/components/usage-panel.tsx:23).

***

## Scope

### In Scope (MVP)

* Add `AIOS/scripts/lib/claude-oauth-usage.ts`.
* Read `~/.claude/.credentials.json` first.
* On macOS only, fall back to Keychain with: `security find-generic-password -s "Claude Code-credentials" -a "$USER" -w`.
* Use `Bun.spawn`, `IS_MACOS`, safe validators, and timeout-race style matching `subscription-detection.ts`; do not port v2.3's `execSync` pattern.
* Detect the live `claude --version` for a Claude Code user agent when safely available.
* Fetch `GET https://api.anthropic.com/api/oauth/usage` with the stored OAuth token and short six-second timeout.
* Return `null` on missing credential, missing CLI, non-200 response, timeout, parse failure, or any unexpected error.
* Wire the result into aggregate orchestration and `claudeWindow` so server-side `pct` and reset data are preferred when present.
* Add `source: "oauth" | "estimate"`, `resetsAt`, and a bounded `authoritative` payload where appropriate.
* Surface a "live" versus "estimate" badge in `AIOS/src/components/usage-panel.tsx`.
* Add the OAuth token to the same secret-redaction set used by local bridges or script logging.
* Add tests for file credential, Keychain credential, missing credential, non-200, timeout, parse failure, source switching, and token redaction.

### Out of Scope

* Any write or mutation to Anthropic or Claude Code.
* Browser-visible exposure of OAuth tokens or raw credential payloads.
* Hard failure of aggregate output when authoritative usage is unavailable.
* A generic third-party usage gateway.

***

## Prerequisites

* [ ] Sessions 01-02 complete or no conflicting usage-window contract changes pending.
* [ ] Identify the current usage-window type and validation path before adding `source`, `resetsAt`, or `authoritative` fields.
* [ ] Confirm the bridge/script redaction helper to extend.

***

## Deliverables

1. `claude-oauth-usage.ts` with file and Keychain credential reads.
2. Aggregate wiring that prefers authoritative Claude usage when available.
3. Usage-panel badge for live OAuth data versus local estimates.
4. Redaction and fallback tests proving the token is never logged and aggregate output stays available on failure.

***

## Success Criteria

* [ ] Missing credentials return `null` and preserve local-log estimates.
* [ ] Credential file and macOS Keychain reads are tested without printing tokens.
* [ ] Non-200 responses, timeouts, and malformed payloads silently fall back to estimates.
* [ ] `claudeWindow.source` flips between `oauth` and `estimate` correctly.
* [ ] `resetsAt` and authoritative percentage values are used only when the server payload is valid.
* [ ] Command output, logs, bridge redaction tests, and generated data contain no OAuth token values.
* [ ] Typecheck, script tests, validation tests, and usage-panel tests pass.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ai-os-and-trend-finder.gitbook.io/ai-os-and-trend-finder-docs/.spec_system/archive/phases/phase_21/session_03_authoritative_claude_usage_oauth.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
