Skip to content

fix(mlx): surface engine prompt-cache reads in usage accounting - #288

Merged
dahai80 merged 2 commits into
mainfrom
fix/mlx-cache-usage-plumbing
Nov 20, 2026
Merged

dahai80 merged 2 commits into
mainfrom
fix/mlx-cache-usage-plumbing

Conversation

@dahai80

@dahai80 dahai80 commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Summary

Closes the cache_read_input_tokens = 0 anomaly from the token-efficiency baseline (#281).

Root cause chain, verified live against fusion-mlx 0.10.67:

  • Engine prompt caching works: identical request, 2nd/3rd call returns prompt_tokens_details.cached_tokens = 64 (of 82 prompt tokens)
  • Engine's Anthropic-style /v1/messages endpoint emits cache_read_input_tokens
  • fusion-code's OpenAI-endpoint translation layer dropped the data: MLXUsage lacked prompt_tokens_details, and both the streaming message_delta and the non-stream transform mapped only prompt_tokens/completion_tokens

Changes

  • fusion-mlx-types.ts: MLXUsage gains optional prompt_tokens_details block
  • fusion-mlx-stream.ts: streaming message_delta + non-stream transform map cached_tokens -> cache_read_input_tokens
  • Field omitted when cached_tokens is 0/absent - uncached runs stay byte-identical

Test plan

  • 4 new tests (streaming + non-stream, cached > 0 and cached = 0 paths)
  • bun test tests/services/api/ src/__tests__/llm/: 260 pass, 0 fail
  • bun run typecheck clean; bun run check full gate green

🤖 Generated with Claude Code

dahai80 and others added 2 commits November 20, 2026 16:03
The fusion-mlx engine reports prefix-cache hits (prompt_tokens_details.
cached_tokens, verified live: identical request 2nd/3rd call returns
cached_tokens=64), and its Anthropic-style /v1/messages endpoint emits
cache_read_input_tokens. The OpenAI-endpoint translation layer dropped
that data, so session jsonl recorded cache_read_input_tokens=0 across
2,365 real sessions (125M input tokens with zero cache accounting) and
efficiency baselines could not measure cache benefit.

- MLXUsage gains optional prompt_tokens_details block
- streaming message_delta maps cached_tokens -> cache_read_input_tokens
- non-stream transform does the same
- fields omitted when cached_tokens is 0/absent: uncached runs stay
  byte-identical

Found during token-efficiency baseline investigation (bench report #1).

Co-Authored-By: Claude Code <[email protected]>
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