Repository navigation
fix(translation): report cache writes in Chat Completions as cache_write_tokens - #947
Merged
Merged
Conversation
|
Contributor
elyasmnvidian
force-pushed
the
feat/harness-cost-accounting
branch
from
October 27, 2026 21:34
f58951a to
6a7a0b1
Compare
…ite_tokens Signed-off-by: Elyas Mehtabuddin <emehtabuddin@nvidia.com>
elyasmnvidian
force-pushed
the
feat/harness-cost-accounting
branch
from
October 27, 2026 22:24
6a7a0b1 to
3ff3d91
Compare
ayushag-nv
approved these changes
Oct 27, 2026
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.
When Switchyard translates an upstream response into Chat Completions, Pi and Oh My Pi see zero prompt-cache writes and show a lower cost than their configured rates give. Switchyard reports cache writes as
prompt_tokens_details.cache_creation_tokens, but both clients read cache writes only fromprompt_tokens_details.cache_write_tokens. They count the cache-write tokens as ordinary input instead.For example, an Anthropic upstream reports 50 input tokens, 20 cache-read tokens, 30 cache-write tokens, and 10 output tokens. Switchyard returns
prompt_tokens: 100withcache_creation_tokens: 30. Pi records 80 input tokens and 0 cache writes, so it never applies the model's cache-write rate.This PR renames the field to
cache_write_tokensin the Chat Completions usage encoder. Buffered and streamed responses use the same encoder, so both change. The Responses encoder already usescache_write_tokens. When Switchyard reads Chat Completions usage from an upstream, it still accepts both names.A client that reads
cache_creation_tokensfrom a translated Chat Completions response must readcache_write_tokensinstead. I kept one name instead of sending both because Pi, Oh My Pi, and Switchyard's own Responses encoder usecache_write_tokens, and no code in this repository readscache_creation_tokensfrom Switchyard's Chat Completions output.The translation test for Responses to Chat Completions now checks
cache_write_tokens. It fails with the old field name.Earlier versions of this PR also added session cost reporting. #1045 has since added that to
main, so this PR now contains only the field rename.Local run
A local HTTP stub served Anthropic Messages responses with the usage above for both buffered and streamed requests. Switchyard used this config:
The server ran with
switchyard-server --config routes.toml --host 127.0.0.1 --port 55099. Pi 0.84.3 used a customopenai-completionsprovider that points tohttp://127.0.0.1:55099/v1, with rates of $1 input, $5 output, $0.10 cache read, and $2 cache write per million tokens. Each run sent one buffered request and one streamed request withcurland onepi -p "hi" --mode jsonturn, then printed the usage.Before, on
mainatfd37aa762:After:
These rates are only for the example and do not reflect a real provider bill. I did not run Oh My Pi. I read its installed
openai-completionsprovider instead, and it reads cache writes only fromprompt_tokens_details.cache_write_tokens.