Skip to content

Commit 9e276bf

Browse files
robinebersclaude
andauthored
Standardize provider documentation to minimal format (#52)
Restructure Claude, Codex, and Cursor provider docs to follow a consistent template with Overview, Endpoints, and Authentication sections. Consolidate field documentation into jsonc comments. Remove code examples and redundant field tables to reduce noise while preserving all technical details. Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
1 parent b2495ad commit 9e276bf

File tree

3 files changed

+141
-492
lines changed

3 files changed

+141
-492
lines changed

docs/providers/claude.md

Lines changed: 41 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
1-
# Claude Code Usage API
1+
# Claude Code
22

3-
Claude Code uses Anthropic's OAuth-based API to fetch usage data. The usage endpoint returns rate limit windows and optional extra credits information as JSON.
3+
> Reverse-engineered, undocumented API. May change without notice.
44
5-
**Note:** This is a reverse-engineered, undocumented API. It may change without notice.
5+
## Overview
66

7-
## Endpoint
7+
- **Protocol:** REST (plain JSON)
8+
- **Base URL:** `https://api.anthropic.com`
9+
- **Auth provider:** `platform.claude.com` (OAuth 2.0)
10+
- **Client ID:** `9d1c250a-e61b-44d9-88ed-5944d1962f5e`
11+
- **Beta header required:** `anthropic-beta: oauth-2025-04-20`
12+
- **Utilization:** integer percentage (0-100)
13+
- **Credits:** cents (divide by 100 for dollars)
14+
- **Timestamps:** ISO 8601 (response), unix milliseconds (credentials file)
815

9-
```
10-
GET https://api.anthropic.com/api/oauth/usage
11-
```
16+
## Endpoints
1217

13-
### Required Headers
18+
### GET /api/oauth/usage
1419

15-
```
16-
Authorization: Bearer <access_token>
17-
Accept: application/json
18-
Content-Type: application/json
19-
anthropic-beta: oauth-2025-04-20
20-
```
20+
Returns rate limit windows and optional extra credits.
2121

22-
### Response
22+
#### Headers
23+
24+
| Header | Required | Value |
25+
|---|---|---|
26+
| Authorization | yes | `Bearer <access_token>` |
27+
| Accept | yes | `application/json` |
28+
| Content-Type | yes | `application/json` |
29+
| anthropic-beta | yes | `oauth-2025-04-20` |
30+
31+
#### Response
2332

2433
```jsonc
2534
{
@@ -31,88 +40,51 @@ anthropic-beta: oauth-2025-04-20
3140
"utilization": 40, // % used in 7-day window
3241
"resets_at": "2026-02-01T00:00:00Z"
3342
},
34-
"seven_day_opus": { // separate weekly limit for Opus (optional)
43+
"seven_day_opus": { // separate weekly Opus limit (optional, plan-dependent)
3544
"utilization": 0,
3645
"resets_at": "2026-02-01T00:00:00Z"
3746
},
38-
"extra_usage": { // on-demand / overage credits (optional)
47+
"extra_usage": { // on-demand overage credits (optional)
3948
"is_enabled": true,
40-
"used_credits": 500, // cents -- amount spent
41-
"monthly_limit": 10000, // cents -- monthly cap
49+
"used_credits": 500, // cents spent
50+
"monthly_limit": 10000, // cents cap (0 = unlimited)
4251
"currency": "USD"
4352
}
4453
}
4554
```
4655

47-
#### Rate Limit Windows
48-
49-
The API tracks multiple concurrent usage windows:
50-
51-
| Window | Field | Duration | Description |
52-
|---|---|---|---|
53-
| **Primary** | `five_hour` | 5 hours | Short-term rolling limit. Resets continuously |
54-
| **Secondary** | `seven_day` | 7 days | Weekly rolling limit. Resets continuously |
55-
| **Opus** | `seven_day_opus` | 7 days | Separate weekly limit for Claude Opus model (when present) |
56-
57-
All windows are enforced simultaneously -- hitting any limit throttles the user.
58-
59-
#### extra_usage: On-Demand Credits
60-
61-
Optional object for on-demand overage spending. Fields:
62-
63-
| Field | Type | Description |
64-
|---|---|---|
65-
| `is_enabled` | boolean | Whether on-demand credits are active |
66-
| `used_credits` | number | Amount spent in cents |
67-
| `monthly_limit` | number | Monthly cap in cents (0 = unlimited) |
68-
| `currency` | string | Currency code (e.g. "USD") |
56+
All windows are enforced simultaneously — hitting any limit throttles the user.
6957

7058
## Authentication
7159

72-
Claude Code uses OAuth tokens issued by Anthropic's auth system.
73-
74-
### Token Locations (macOS)
60+
### Token Location
7561

76-
**Primary: Credentials file**
77-
78-
```
79-
~/.claude/.credentials.json
80-
```
81-
82-
File structure:
62+
**Primary:** `~/.claude/.credentials.json`
8363

8464
```jsonc
8565
{
8666
"claudeAiOauth": {
8767
"accessToken": "<jwt>", // OAuth access token (Bearer)
88-
"refreshToken": "<token>", // used to obtain new access tokens
89-
"expiresAt": 1738300000000, // unix ms -- token expiration
90-
"scopes": ["..."], // granted OAuth scopes
91-
"subscriptionType": "pro", // plan tier
92-
"rateLimitTier": "..." // rate limit tier
68+
"refreshToken": "<token>",
69+
"expiresAt": 1738300000000, // unix ms
70+
"scopes": ["..."],
71+
"subscriptionType": "pro",
72+
"rateLimitTier": "..."
9373
}
9474
}
9575
```
9676

97-
**Fallback: macOS Keychain**
98-
99-
Service name: `Claude Code-credentials`
100-
101-
The keychain entry contains the same JSON structure as the credentials file.
77+
**Fallback:** macOS Keychain, service name `Claude Code-credentials` (same JSON structure).
10278

10379
### Token Refresh
10480

105-
Access tokens are short-lived JWTs. The `expiresAt` field indicates when the token expires (unix milliseconds). If expired, the plugin will automatically refresh using the `refreshToken`.
106-
107-
**Refresh endpoint:**
81+
Access tokens are short-lived JWTs. Refreshed proactively 5 minutes before expiration, or reactively on 401/403.
10882

10983
```
11084
POST https://platform.claude.com/v1/oauth/token
11185
Content-Type: application/json
11286
```
11387

114-
**Request body:**
115-
11688
```json
11789
{
11890
"grant_type": "refresh_token",
@@ -122,110 +94,10 @@ Content-Type: application/json
12294
}
12395
```
12496

125-
**Response:**
126-
127-
```json
97+
```jsonc
12898
{
12999
"access_token": "<new_jwt>",
130-
"refresh_token": "<new_refresh_token>",
131-
"expires_in": 3600
100+
"refresh_token": "<new_refresh_token>", // may be same as previous
101+
"expires_in": 3600 // seconds
132102
}
133103
```
134-
135-
| Field | Type | Description |
136-
|---|---|---|
137-
| `access_token` | string | New OAuth access token |
138-
| `refresh_token` | string | New refresh token (may be same as previous) |
139-
| `expires_in` | number | Token lifetime in seconds |
140-
141-
The plugin refreshes proactively when the token is within 5 minutes of expiration, or reactively on 401/403 responses. Updated credentials are persisted back to the original source (file or keychain).
142-
143-
## Usage Example (curl)
144-
145-
```bash
146-
ACCESS_TOKEN=$(python3 -c "import json; print(json.load(open('$HOME/.claude/.credentials.json'))['claudeAiOauth']['accessToken'])")
147-
148-
curl -s "https://api.anthropic.com/api/oauth/usage" \
149-
-H "Authorization: Bearer $ACCESS_TOKEN" \
150-
-H "Accept: application/json" \
151-
-H "Content-Type: application/json" \
152-
-H "anthropic-beta: oauth-2025-04-20" | python3 -m json.tool
153-
```
154-
155-
## Usage Example (TypeScript)
156-
157-
```typescript
158-
import { readFileSync, existsSync } from "fs";
159-
import { homedir } from "os";
160-
import { join } from "path";
161-
162-
interface ClaudeOAuth {
163-
accessToken: string;
164-
refreshToken?: string;
165-
expiresAt?: number;
166-
subscriptionType?: string;
167-
}
168-
169-
interface UsageWindow {
170-
utilization: number;
171-
resets_at?: string;
172-
}
173-
174-
interface UsageResponse {
175-
five_hour?: UsageWindow;
176-
seven_day?: UsageWindow;
177-
seven_day_opus?: UsageWindow;
178-
extra_usage?: {
179-
is_enabled?: boolean;
180-
used_credits?: number;
181-
monthly_limit?: number;
182-
currency?: string;
183-
};
184-
}
185-
186-
function getClaudeCredentials(): ClaudeOAuth | null {
187-
const credPath = join(homedir(), ".claude", ".credentials.json");
188-
if (!existsSync(credPath)) return null;
189-
190-
const data = JSON.parse(readFileSync(credPath, "utf-8"));
191-
return data.claudeAiOauth ?? null;
192-
}
193-
194-
async function getClaudeUsage(): Promise<UsageResponse> {
195-
const creds = getClaudeCredentials();
196-
if (!creds?.accessToken) throw new Error("Claude credentials not found");
197-
198-
const res = await fetch("https://api.anthropic.com/api/oauth/usage", {
199-
headers: {
200-
Authorization: `Bearer ${creds.accessToken}`,
201-
Accept: "application/json",
202-
"Content-Type": "application/json",
203-
"anthropic-beta": "oauth-2025-04-20",
204-
},
205-
});
206-
207-
if (!res.ok) throw new Error(`HTTP ${res.status}`);
208-
return res.json();
209-
}
210-
```
211-
212-
## Technical Details
213-
214-
- **Protocol:** REST (plain JSON)
215-
- **HTTP method:** GET (usage), POST (token refresh)
216-
- **Usage domain:** `api.anthropic.com`
217-
- **OAuth domain:** `platform.claude.com`
218-
- **Beta header:** `anthropic-beta: oauth-2025-04-20` (required for usage endpoint)
219-
- **Client ID:** `9d1c250a-e61b-44d9-88ed-5944d1962f5e`
220-
- **Utilization is a percentage** (0-100)
221-
- **Credits are in cents** (divide by 100 for dollars)
222-
- **Timestamps are ISO 8601** (not unix)
223-
- **Expiration times are unix milliseconds** (in credentials file)
224-
- **Token refresh:** JSON body (not form-encoded)
225-
226-
## Open Questions
227-
228-
- [x] What OAuth refresh endpoint does Claude Code use? → `https://platform.claude.com/v1/oauth/token`
229-
- [ ] Is `seven_day_opus` always present, or only for certain plans?
230-
- [ ] Are there additional rate limit windows for different plan tiers (e.g. Max)?
231-
- [x] What scopes are required for the usage endpoint? → `user:inference` (minimum), full set: `user:profile user:inference user:sessions:claude_code user:mcp_servers`

0 commit comments

Comments
 (0)