Skip to main content
← Back to list
01Issue
FeatureOpenExtensionsPublic
AssigneesNone

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 the usage, modelUsage and total_cost_usd that Claude Code reports with --output-format json are never captured.
  • Codex is started as codex exec … -o <file> without --json, so its token counts are lost too.
  • ClaudeConfigSchema only accepts allowedTools/disallowedTools, so a caller can't turn this on through providerConfig.

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, set output to the envelope's result text (so existing callers that parse output keep working), and record usage: { model, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, cost_usd } from usage/modelUsage/total_cost_usd. Optionally keep num_turns, duration_ms and duration_api_ms.
  • Codex: run exec --json (keeping -o for the final message) and record token counts from the turn usage events. Leave cost_usd absent, since Codex doesn't report a price.
  • When the CLI doesn't report usage (older version, error, parse failure), leave usage absent 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 run into 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
02Bog Flow
◉OPEN○TRIAGED○IN PROGRESS○SHIPPED

Open

10/27/2026, 11:34:34 PM

No activity in this phase yet.

03Sludge Pulse
Editable. Press Enter to edit.

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.