Skip to main content
All CostHQ errors follow a consistent JSON shape so your agent scripts can handle them programmatically without brittle string matching. Whether a command fails because there is no active session or because a model is unknown, the structure is always the same — only error.code changes.

Error Shape

The schemaVersion and CostHQVersion fields are present on every response, including errors. Parse error.code to branch your logic. Never branch on error.message — it is human-readable prose that may change between versions.

Exit Codes

There is no ambiguity: any non-zero exit code means the command failed and an error object is present in the JSON output.

Error Codes

Parsing Error Codes

When a command exits with code 1, execSync throws. The error object’s .stdout property still contains the full --json error payload — read it from there, not from stderr.

Failsafe Pattern for Agents

Check that cs is installed before attempting any tracking. If it is absent, skip gracefully rather than blocking your agent’s primary task.
The same check in Node.js:
Never string-compare error.message. The human-readable message text may change between CostHQ versions. Always branch on error.code, which is part of the stable contract.