Four Integration Approaches
cs run
Zero configuration. Wrap your command and CostHQ handles the full session lifecycle automatically.
Proxy + env vars
Start the local proxy, set two environment variables, and every AI call your script makes is logged automatically.
CI/CD pipeline
Add
cs start and cs end to your pipeline steps to track every build and test run.Git hooks
Use a
post-commit hook to trigger tracking actions whenever your agent commits code.Approach 1: cs run
cs run is the zero-configuration option. It starts a session, launches a local proxy pre-configured with the right environment variables, runs your command, and prints a cost summary when it exits — all in one step.
- A new session is created (name defaults to the command string)
- A local proxy starts on port 3739
- Your command launches with
ANTHROPIC_BASE_URLandOPENAI_BASE_URLalready pointing at the proxy - All AI API calls are intercepted and logged to the session
- When the command exits, the session ends and a summary is printed
Approach 2: Proxy + Environment Variables
If you need more control over session boundaries, start the proxy manually and set the environment variables yourself before running your agent:cs run.
Approach 3: CI/CD Pipeline Tracking
Track every build and test pipeline by wrapping your pipeline steps withcs start and cs end:
Approach 4: Git Hooks
For agents that commit code, use apost-commit hook to run tracking actions after each commit. Add the following to .git/hooks/post-commit:
Concurrency and Multiple Repos
Multiple agents or repos on the same machine can safely run at the same time. CostHQ uses SQLite WAL mode so concurrent writes never block each other. Use--close-stale to avoid conflicts when starting a new session, and use -s <id> to target a specific session when logging from a separate process:
Crash Resilience
If an agent dies mid-session, the nextcs start won’t leave orphaned sessions behind:
Recommended Agent Lifecycle
Build your agent around this five-step sequence to get reliable, machine-readable output at every step:cs start "task" --close-stale --json→ capture the session ID from the responsecs note "phase 1: analysis" --json→ add a timestamped annotation- Agent performs its work (files and git commits are tracked automatically)
cs log-ai -p <provider> -m <model> --prompt-tokens <n> --completion-tokens <n> --json→ log each AI callcs end -n "summary" --json→ finalize the session and get the full cost totals
Failsafe: When cs Is Not Installed
Always check whether cs is available before trying to track. If CostHQ isn’t installed, skip tracking and let the agent continue its primary task:
On Windows, use
where cs instead of which cs. All cs commands work identically on Windows, macOS, and Linux.Further Reading
- JSON Output Reference — full schema for every
--jsonresponse, includingschemaVersion, error shapes, and exit codes - Node.js Programmatic API — the
AgentSessionclass andrunAgentSessionhelper for embedding CostHQ directly in your Node.js agent code