Skip to content

Track cached input tokens in provider usage + Cost event#322

Merged
edwin-zvs merged 1 commit into
mainfrom
zarvis-cached-tokens
Jun 1, 2026
Merged

Track cached input tokens in provider usage + Cost event#322
edwin-zvs merged 1 commit into
mainfrom
zarvis-cached-tokens

Conversation

@edwin-zvs

Copy link
Copy Markdown
Contributor

What

Adds Usage.cached_tokens and a tokens_cached field on SessionEvent::Cost, populated from each provider that reports a cached-token count:

provider source field
codex-oauth usage.input_tokens_details.cached_tokens
OpenAI usage.prompt_tokens_details.cached_tokens
Anthropic usage.cache_read_input_tokens
Claude CLI adapter usage.cache_read_input_tokens

The cost modeline now shows cached=N alongside in/out.

Why

Benchmarking surfaced that zarvis reported 0 cached tokens while the Codex CLI on the same store:false backend reported ~97% of its input as cached. The cause was purely a parsing gap — the providers only read input_tokens/output_tokens and dropped the cached breakdown — so prompt-cache usage was invisible (and unmeasurable in trajectories/cost). This plumbs the existing data through so cache hit-rate can actually be observed per turn.

Compatibility

tokens_cached is #[serde(default)], so old transcripts/clients deserialize unchanged; Usage.cached_tokens defaults to 0 for providers that don't report it (e.g. Ollama).

Test plan

  • cargo build (workspace) + cargo test -p agentd-protocol -p agentd-adapter-zarvis -p agentd-adapter-claude — green.
  • Parsed identically to the existing input_tokens/output_tokens paths; field defaults to 0 when absent.

🤖 Generated with Claude Code

zarvis reported no prompt-cache usage because providers never parsed the
cached-token counts. Add `Usage.cached_tokens` and parse it from each
provider that exposes it — codex-oauth (`input_tokens_details.cached_tokens`),
OpenAI (`prompt_tokens_details.cached_tokens`), Anthropic + Claude CLI
(`cache_read_input_tokens`). Surface it on `SessionEvent::Cost` as
`tokens_cached` (serde default → backward-compatible), and show `cached=N`
in the cost modeline.
@edwin-zvs
edwin-zvs merged commit 921bb6c into main Jun 1, 2026
1 check passed
@edwin-zvs
edwin-zvs deleted the zarvis-cached-tokens branch June 1, 2026 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant