Skip to content

fix(openai): report cache write tokens as prompt_cache_creation_tokens - #2842

Open
BASIL K AJI (basil-k-aji-dev) wants to merge 1 commit into
braintrustdata:mainfrom
basil-k-aji-dev:fix/openai-cache-creation
Open

BASIL K AJI (basil-k-aji-dev) wants to merge 1 commit into
braintrustdata:mainfrom
basil-k-aji-dev:fix/openai-cache-creation

Conversation

@basil-k-aji-dev

Copy link
Copy Markdown

Summary

The OpenAI wrapper (and Groq, which uses the same helper) logged cache-write tokens as prompt_cache_write_tokens. Every other JS wrapper, and the Python SDK's OpenAI integration, uses the canonical name prompt_cache_creation_tokens (anthropic, langchain, mastra, the AI SDK and the agents trace processor all do), so OpenAI prompt-cache writes were not attributed in Braintrust.

parseMetricsFromUsage in js/src/openai-utils.ts now deletes prompt_cache_write_tokens and sets prompt_cache_creation_tokens, without overwriting a value that is already there. This mirrors Python's pop and setdefault in integrations/openai/tracing.py. Groq has no cache-write field, so nothing else changes for it.

Tests

Three new tests in openai-instrumentation.test.ts (the prompt_tokens_details and input_tokens_details shapes, and an existing prompt_cache_creation_tokens being kept), and the expectation in openai-decisions.test.ts is updated.

  • Without the change: 4 tests fail with prompt_cache_write_tokens where prompt_cache_creation_tokens is expected.
  • With the change: both files pass (112 tests). check:typings, lint, the formatting check and validate-changesets are clean. Emitting both names, overwriting an existing value, or always emitting 0 each fail a test.
  • The wider providers run shows one failure, a 15s timeout in livekit-plugin.test.ts that does not touch OpenAI code and passes on its own.

Things to know

  • 32 snapshot files under e2e/scenarios/openai-instrumentation/__snapshots__ contain the old key. I renamed it in place instead of regenerating them, because I could not run the e2e scenarios locally (they need a js build and per-scenario installs). The snapshots are key-sorted, and I checked that the new key sorts into the same position as the old one in all 580 metrics blocks, so a regenerated file should be byte-identical.
  • The OpenRouter parser still emits prompt_cache_write_tokens on its own path (8 snapshots); I left it alone.

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