Skip to content

feat(events): report prompt-cache hits as cached_tokens - #332

Open
raymondginger2018-sudo wants to merge 1 commit into
HKUDS:mainfrom
raymondginger2018-sudo:pr/cache-usage
Open

raymondginger2018-sudo wants to merge 1 commit into
HKUDS:mainfrom
raymondginger2018-sudo:pr/cache-usage

Conversation

@raymondginger2018-sudo

Copy link
Copy Markdown
Contributor

Summary

UsageInfo folded everything the provider reported into prompt_tokens, so a host could not tell whether a long prefix was actually cached. The providers already report the hit count — under different names per vendor — and that count is a subset of prompt_tokens.

UsageInfo.cached_tokens now carries it, so a UI or a cost report can show cache effectiveness without re-parsing provider payloads.

Changes

  • core/events/llm_events.py
    • UsageInfo.cached_tokens: int = 0 — defaults keep every existing construction site working.
    • _cached_tokens(usage) reads the first field that carries a count: cached_tokens, prompt_cache_hit_tokens (DeepSeek), cache_read_input_tokens (Anthropic), prompt_tokens_details.cached_tokens (OpenAI). Missing or non-numeric values read as 0.
    • llm_response_to_events fills the field from the response usage; serialization carries it.
  • tests/test_llm_events.py — DeepSeek, OpenAI, and the uncached default.

Compatibility

  • Additive field with a default; no caller changes required.
  • prompt_tokens keeps its existing meaning (cache hits remain included, as providers report them).
  • No new dependency or configuration.

Tests

tests/test_llm_events.py — 8 passed on this revision; pre-commit (ruff 0.15.21 + ruff-format) clean.

`UsageInfo` folded everything into `prompt_tokens`, so a host could not show
whether a long prefix was actually cached. Providers already report the hit
count under different names (DeepSeek `prompt_cache_hit_tokens`, OpenAI
`prompt_tokens_details.cached_tokens`, Anthropic `cache_read_input_tokens`);
read the first one that carries a count and expose it as `cached_tokens`, a
subset of `prompt_tokens`.

Tests: DeepSeek and OpenAI shapes, plus the uncached default.

This branch has not been deployed

No deployments
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