Skip to content

Commit 6e6c95b

Browse files
committed
docs: add docs/API_REFERENCE.md (JSON envelope, error shape, output format contract)
1 parent 8574168 commit 6e6c95b

2 files changed

Lines changed: 175 additions & 1 deletion

File tree

PARITY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,5 +213,5 @@ Repo documentation suite shipped during extended dogfood audit. Status: present/
213213
| CODE_OF_CONDUCT.md | ✅ Present | Low | Contributor Covenant v2.1 |
214214
| .github/PULL_REQUEST_TEMPLATE.md | ✅ Present | Medium | Standardizes PR descriptions |
215215
| docs/CONFIGURATION.md | ✅ Present | High | env vars, settings.json, provider config — relates to #283, #285 |
216-
| docs/API_REFERENCE.md | ❌ Missing | Medium | JSON envelope schema, output format contract |
216+
| docs/API_REFERENCE.md | ✅ Present | Medium | JSON envelope schema, output format contract#288, #266, #168c |
217217
| .github/ISSUE_TEMPLATE/bug_report.md | ✅ Present | #431 | Standard bug template with repro steps, environment, context sections |

docs/API_REFERENCE.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# API Reference — JSON Output Envelope Contract
2+
3+
This document describes the machine-readable JSON output emitted by `claw` when
4+
`--output-format json` is passed. All JSON envelopes are written to **stdout**.
5+
Stderr is reserved for non-contractual diagnostics only (see pinpoint #168c).
6+
7+
---
8+
9+
## Output Format Flag
10+
11+
```
12+
claw [command] --output-format json
13+
claw [command] --output-format text # default
14+
```
15+
16+
When `json` is active, **all** output (success and error) is emitted as a single
17+
JSON object on stdout. Consumers must not parse stderr for errors.
18+
19+
---
20+
21+
## Success Envelope — `claw -p <prompt>`
22+
23+
Full non-compact run (default):
24+
25+
```json
26+
{
27+
"message": "<final assistant text>",
28+
"model": "claude-opus-4-5",
29+
"iterations": 3,
30+
"auto_compaction": null,
31+
"tool_uses": [...],
32+
"tool_results": [...],
33+
"prompt_cache_events": [...],
34+
"usage": {
35+
"input_tokens": 1234,
36+
"output_tokens": 567,
37+
"cache_creation_input_tokens": 0,
38+
"cache_read_input_tokens": 0
39+
},
40+
"estimated_cost": "$0.0123"
41+
}
42+
```
43+
44+
Compact run (`--compact`):
45+
46+
```json
47+
{
48+
"message": "<final assistant text>",
49+
"compact": true,
50+
"model": "claude-opus-4-5",
51+
"usage": {
52+
"input_tokens": 1234,
53+
"output_tokens": 567,
54+
"cache_creation_input_tokens": 0,
55+
"cache_read_input_tokens": 0
56+
}
57+
}
58+
```
59+
60+
### Field Reference
61+
62+
| Field | Type | Description |
63+
|---|---|---|
64+
| `message` | string | Final assistant reply text |
65+
| `model` | string | Model identifier used for the turn |
66+
| `iterations` | integer | Number of tool-use / re-prompt iterations |
67+
| `compact` | boolean | Present and `true` when `--compact` mode was active |
68+
| `auto_compaction` | object\|null | Non-null when auto-compaction fired (see below) |
69+
| `tool_uses` | array | Tool calls made during the turn (TODO: verify schema) |
70+
| `tool_results` | array | Results returned to the model (TODO: verify schema) |
71+
| `prompt_cache_events` | array | Cache-hit/miss events (TODO: verify schema) |
72+
| `usage.input_tokens` | integer | Input tokens billed |
73+
| `usage.output_tokens` | integer | Output tokens billed |
74+
| `usage.cache_creation_input_tokens` | integer | Tokens written to prompt cache |
75+
| `usage.cache_read_input_tokens` | integer | Tokens served from prompt cache |
76+
| `estimated_cost` | string | Human-readable USD cost estimate (e.g. `"$0.0123"`) |
77+
78+
#### `auto_compaction` sub-object
79+
80+
```json
81+
{
82+
"removed_messages": 12,
83+
"notice": "Auto-compacted: removed 12 messages to free context."
84+
}
85+
```
86+
87+
---
88+
89+
## Error Envelope
90+
91+
When a command fails under `--output-format json`, an error envelope is written
92+
to **stdout** (pinpoint #168c / #288):
93+
94+
```json
95+
{
96+
"type": "error",
97+
"error": "<short human-readable reason>",
98+
"kind": "<snake_case error kind token>",
99+
"hint": "<optional actionable hint>"
100+
}
101+
```
102+
103+
### Error Envelope Fields
104+
105+
| Field | Type | Description |
106+
|---|---|---|
107+
| `type` | string | Always `"error"` |
108+
| `error` | string | Short prose description of the failure |
109+
| `kind` | string | Machine-readable snake_case token (see §Error Kinds) |
110+
| `hint` | string\|null | Optional remediation hint |
111+
112+
### Error Kinds (selected)
113+
114+
`kind` values are classified by `classify_error_kind()`. Common tokens include:
115+
116+
- `not_yet_implemented` — command stub not yet shipped
117+
- `config_error` — configuration file parse / validation failure
118+
- `auth_error` — API key or credential problem
119+
- `permission_denied` — tool-use permission denied
120+
- `model_error` — upstream model API error
121+
122+
See pinpoint #266 (typed-error-kind) for the full taxonomy.
123+
124+
---
125+
126+
## Streaming Behavior
127+
128+
`claw` always uses streaming internally (HTTP chunked transfer to the Anthropic
129+
API) but the **JSON output envelope is emitted once**, after the turn completes.
130+
There is no per-token or per-chunk JSON stream exposed to the caller.
131+
132+
In REPL / interactive mode (`claw` with no `-p`) the JSON format applies only to
133+
structured sub-commands, not to the interactive session itself.
134+
135+
---
136+
137+
## Status Snapshot (`claw status`)
138+
139+
```json
140+
{
141+
"kind": "status",
142+
"status": "ok",
143+
"config_load_error": null,
144+
"model": "claude-opus-4-5",
145+
"model_source": "config",
146+
"model_raw": null,
147+
"permission_mode": "default",
148+
"usage": {
149+
"messages": 42,
150+
"turns": 10,
151+
"latest_total": 5678,
152+
"cumulative_input": 12345,
153+
"cumulative_output": 4567,
154+
"cumulative_total": 16912,
155+
"estimated_tokens": 16912
156+
},
157+
"workspace": {
158+
"cwd": "/Users/you/project",
159+
"project_root": "/Users/you/project",
160+
"git_branch": "main",
161+
"git_state": "clean",
162+
"changed_files": 0
163+
}
164+
}
165+
```
166+
167+
---
168+
169+
## Related Pinpoints
170+
171+
- **#288** — error-envelope stdout emission contract
172+
- **#266** — typed-error-kind taxonomy
173+
- **#168c**`--output-format json` routes error envelopes to stdout
174+
- **#247** — JSON envelope field preservation (hint / help text)

0 commit comments

Comments
 (0)