Relationships
#4057 agent-runner: record token usage and cost on runResult (Claude --output-format json, Codex --json)
Opened by rogueco · 10/27/2026
Problem
@swamp/agent-runner run throws away token usage and cost, so there's no way to measure what an agent call cost from its runResult.
- Claude is started as
claude -p <prompt>with text output, so theusage,modelUsageandtotal_cost_usdthat Claude Code reports with--output-format jsonare never captured. - Codex is started as
codex exec … -o <file>without--json, so its token counts are lost too. ClaudeConfigSchemaonly acceptsallowedTools/disallowedTools, so a caller can't turn this on throughproviderConfig.
Checked against the latest release, 2026.10.23.1, and main (agent-runner/extensions/models/_lib/providers/{claude,codex}.ts, runner.ts, schemas.ts). Neither captures usage, and no open PR adds it.
We drive a multi-stage delivery factory (Stagecraft) through run and want cost and tokens per stage, role and work item. Today that is "unknown" for every call.
Proposed solution
Add an optional usage object to runResult, without changing the current output contract:
- Claude: run with
--output-format json, setoutputto the envelope'sresulttext (so existing callers that parseoutputkeep working), and recordusage: { model, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, cost_usd }fromusage/modelUsage/total_cost_usd. Optionally keepnum_turns,duration_msandduration_api_ms. - Codex: run
exec --json(keeping-ofor the final message) and record token counts from the turn usage events. Leavecost_usdabsent, since Codex doesn't report a price. - When the CLI doesn't report usage (older version, error, parse failure), leave
usageabsent rather than zero, and never fail the run because of it. - Attach the same numbers to the existing tracing spans if useful.
Alternatives considered
- Copying
runinto a local metered method: duplicates binary download, checksum verification, auth and provider config, and drifts from upstream. - Reading Claude's session transcripts or OTEL metrics from outside: fragile, provider-specific, and not linked to the
runResult.
Upstream repository: https://github.com/swamp-club/swamp-extensions
Environment
- Extension:
@swamp/agent-runner@2026.10.22.1 - swamp:
20261022.021804.0-sha.983e2929 - OS:
darwin(aarch64) - Deno:
2.9.8 - Shell:
/bin/zsh
Open
No activity in this phase yet.
system commented 10/27/2026, 11:34:34 PM
Classified automatically when this issue was filed.
- Source: Extensions
If you feel this classification is incorrect, add a ripple to tell us so.
Sign in to post a ripple.