Repository navigation
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
Open
BASIL K AJI (basil-k-aji-dev) wants to merge 1 commit into
BASIL K AJI (basil-k-aji-dev) wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 nameprompt_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.parseMetricsFromUsageinjs/src/openai-utils.tsnow deletesprompt_cache_write_tokensand setsprompt_cache_creation_tokens, without overwriting a value that is already there. This mirrors Python'spopandsetdefaultinintegrations/openai/tracing.py. Groq has no cache-write field, so nothing else changes for it.Tests
Three new tests in
openai-instrumentation.test.ts(theprompt_tokens_detailsandinput_tokens_detailsshapes, and an existingprompt_cache_creation_tokensbeing kept), and the expectation inopenai-decisions.test.tsis updated.prompt_cache_write_tokenswhereprompt_cache_creation_tokensis expected.check:typings,lint, the formatting check andvalidate-changesetsare clean. Emitting both names, overwriting an existing value, or always emitting 0 each fail a test.providersrun shows one failure, a 15s timeout inlivekit-plugin.test.tsthat does not touch OpenAI code and passes on its own.Things to know
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 ajsbuild 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.prompt_cache_write_tokenson its own path (8 snapshots); I left it alone.