Skip to content

feat(cache): add cache token audit logging behind OPENCODE_EXPERIMENTAL_CACHE_AUDIT#27007

Open
martinffx wants to merge 5 commits into
anomalyco:devfrom
martinffx:feat/cache-audit-logging
Open

feat(cache): add cache token audit logging behind OPENCODE_EXPERIMENTAL_CACHE_AUDIT#27007
martinffx wants to merge 5 commits into
anomalyco:devfrom
martinffx:feat/cache-audit-logging

Conversation

@martinffx
Copy link
Copy Markdown

Issue for this PR

Closes #5416, #5224
Related: #14065, #5422, #14203, #14743

Part of the stacked PR decomposition of the prompt caching fix (1 of 4). Adds cache token audit logging as observability foundation, the metrics logged here are what we use to verify cache hit improvements in subsequent PRs.

Type of change

  • New feature

What does this PR do?

Adds cache token audit logging behind OPENCODE_EXPERIMENTAL_CACHE_AUDIT=1, providing per-LLM-call cache hit/miss visibility. This PR is purely observability — it does not change any caching behavior.

Changes:

  1. packages/core/src/flag/flag.ts — adds OPENCODE_EXPERIMENTAL_CACHE_AUDIT flag.

  2. packages/opencode/src/session/session.ts — adds rawInputTokens to the usage return value (sum of input + cache read + cache write), needed by both the log and sidebar display.

  3. packages/opencode/src/session/processor.ts — after each LLM call's getUsage(), if the flag is set, logs a [CACHE] line with total input tokens, cache read/write/new breakdown, hit percentage, and output tokens.

  4. packages/opencode/src/cli/cmd/tui/feature-plugins/sidebar/context.tsx — when the flag is set, displays a "Cache Audit" box in the TUI sidebar showing the same token breakdown and hit rate in real time.

Why this PR first: The subsequent PRs in the stack improve cache hit rates. This PR provides the measurement tooling to verify those improvements work.

How did you verify your code works?

Set OPENCODE_EXPERIMENTAL_CACHE_AUDIT=1 bun dev, send a prompt, and confirm:

  • A [CACHE] log line appears per LLM call in the log output
  • The sidebar shows the "Cache Audit" box with token breakdown

Tests: bun test packages/opencode/test/session/session.test.ts passes.

Screenshots

Screenshot 2026-05-12 at 08 50 19

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Co-authored-by: Bhagirathsinh Vaghela bhagirathsinh.vaghela.001@gmail.com

@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:title labels May 12, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Hey! Your PR title Feat/cache audit logging doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@martinffx martinffx changed the title Feat/cache audit logging feat(cache): add cache token audit logging behind OPENCODE_EXPERIMENTAL_CACHE_AUDIT May 12, 2026
@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label May 12, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

bhagirathsinh-vaghela and others added 5 commits May 13, 2026 20:53
…NCODE_CACHE_AUDIT

Co-authored-by: Bhagirathsinh Vaghela <bhagirathsinh.vaghela.001@gmail.com>
…NCODE_CACHE_AUDIT

Co-authored-by: Bhagirathsinh Vaghela <bhagirathsinh.vaghela.001@gmail.com>
A conflict during the rebase incorrectly removed the
const references = yield* Reference.Service line from
the prompt.ts layer generator, causing 7 typecheck errors.
Adding it back restores the Reference.Service dependency
needed by resolvePromptParts.
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.

[FEATURE]: Anthropic (and others) caching improvement

3 participants