Skip to content

fix(snowflake): pin Cortex Claude cache token fields on usage - #41608

Closed
BizerNotNull wants to merge 1 commit into
BerriAI:mainfrom
BizerNotNull:litellm_snowflake_cache_usage_35229
Closed

BizerNotNull wants to merge 1 commit into
BerriAI:mainfrom
BizerNotNull:litellm_snowflake_cache_usage_35229

Conversation

@BizerNotNull

Copy link
Copy Markdown

TLDR

Problem this solves:

  • Snowflake Claude usage dropped cache_creation_input_tokens and cache_read_input_tokens
  • A cache hit then looked like a tiny uncached prompt, so spend and cache stats were wrong

How it solves it:

  • Types those Cortex usage fields on the Anthropic-shaped response block
  • Asserts they survive transform_response and JSON serialization, including a cache-hit payload

User Flow

Before: a developer sends a cached Claude request through Snowflake Cortex and cannot see cache reads on the usage object

  1. They send POST https://litellm-domain/v1/chat/completions for snowflake/claude-opus-4-5 with cache_control on a long prompt
  2. Cortex returns usage with input_tokens 2, output_tokens 256, and cache_read_input_tokens 2941
  3. The proxy response usage only has input/output counts, so the client records 2 prompt tokens and no cache read

After: the same request keeps Cortex cache counts on usage

  1. They send the same POST https://litellm-domain/v1/chat/completions request
  2. Cortex still returns input_tokens 2, output_tokens 256, and cache_read_input_tokens 2941
  3. The proxy usage includes cache_creation_input_tokens 0 and cache_read_input_tokens 2941, with prompt_tokens 2943

Relevant issues

Fixes #35229

Affected release

Linear ticket

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Shared setup: no Snowflake credentials were present in this environment, so this PR could not curl a live Cortex account. The mapping itself was already live-checked on #39453 against paid Cortex. This change pins the named fields from #35229 so a cache-hit usage object cannot lose them again

Before (main)

  1. Cortex Claude usage mapping already copies cache counts through the shared Anthropic calculator
  2. Tests only asserted prompt_tokens_details.cache_creation_tokens / cached_tokens, not cache_creation_input_tokens / cache_read_input_tokens on the serialized usage object

After (bbc4ee6)

  1. _AnthropicUsageBlock includes cache_creation_input_tokens and cache_read_input_tokens
  2. test_prompt_cache_read_usage_is_surfaced feeds usage {input_tokens: 2, output_tokens: 256, cache_creation_input_tokens: 0, cache_read_input_tokens: 2941} and requires those fields on both the Usage object and model_dump()
  3. python -m pytest tests/test_litellm/llms/snowflake/test_snowflake_native_endpoints.py tests/test_litellm/llms/snowflake/chat/test_snowflake_chat_transformation.py -v -> 75 passed

Type

Bug Fix

Caveats (if any)

Low

  • Live Cortex curl was not rerun here because no Snowflake credentials were available
  • Streaming still takes cache_creation from the final usage delta, not message_start. That is outside this issue

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Keep cache_creation_input_tokens and cache_read_input_tokens on the
typed Cortex usage block and assert they survive transform_response
serialization, including a cache-hit payload.
@BizerNotNull
BizerNotNull requested a review from a team September 17, 2026 16:03
@greptile-apps

greptile-apps Bot commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge, with focused typing and regression-test changes that preserve the existing usage normalization contract

Summary

This PR extends Snowflake Cortex Claude usage typing to include cache creation and cache read token fields, then strengthens coverage for normalized and serialized usage

  • Preserves the Anthropic-shaped cache token fields in the Snowflake response contract
  • Verifies cache-hit prompt totals and both provider-specific and normalized cache details
  • Adds coverage for cache-read usage in streaming responses

Reviews (1) · Last reviewed commit: "fix(snowflake): pin Cortex Claude cache ..."

@codecov

codecov Bot commented Sep 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing BizerNotNull:litellm_snowflake_cache_usage_35229 (bbc4ee6) with main (4b368bf)

Open in CodSpeed

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.

[Bug]: Snowflake Claude provider drops cache_creation_input_tokens and cache_read_input_tokens from response usage

1 participant