Skip to content

feat(dashscope): add separate explicit and implicit cache pricing - #2

Draft
limdeng wants to merge 13 commits into
mainfrom
litellm_dashscope_qwen_cache_pricing
Draft

limdeng wants to merge 13 commits into
mainfrom
litellm_dashscope_qwen_cache_pricing

Conversation

@limdeng

@limdeng limdeng commented Sep 7, 2026 •

Copy link
Copy Markdown
Owner

TLDR

Feature this adds:

  • Add separate, configurable read prices for Qwen explicit and implicit Context Cache
  • Extend existing DashScope cache support with optional mode-aware pricing
  • Keep existing cache-read pricing when no implicit rate is configured

How it works:

  • Preserve provider-reported cache_type in normalized usage
  • Reuse cache_read_input_token_cost for explicit reads and cache_creation_input_token_cost for cache creation
  • Add optional implicit_cache_read_input_token_cost for implicit reads
  • Apply the selected rate consistently to request cost, cost breakdown, tiered pricing, and caching savings

Explicit (cache_control-based) and implicit (automatic prefix) caching are documented DashScope capabilities. This PR adds pricing support for distinguishing their cache reads in LiteLLM.

Provider documentation: https://www.alibabacloud.com/help/en/model-studio/context-cache

User Flow

Before: Qwen cache reads share one configured rate

  1. The proxy admin configures cache_read_input_token_cost at $0.10/M tokens; separate implicit-cache read pricing is not supported
  2. A developer sends POST http://localhost:4000/v1/chat/completions twice with the same long Qwen prompt and no cache_control
  3. The repeated response is 200 OK with cached_tokens and no cache_type
  4. The cache-read cost header uses the shared $0.10/M rate

After: the admin can opt into separate implicit-cache read pricing

  1. The proxy admin keeps cache_read_input_token_cost at $0.10/M and sets the new implicit_cache_read_input_token_cost to $0.20/M
  2. The developer repeats the same requests
  3. An implicit hit uses $0.20/M; an explicit hit with cache_type: "ephemeral" continues to use $0.10/M
  4. Omitting the new field preserves the existing shared-rate behavior

Relevant issues

Closes BerriAI#40090

Related transport and pricing work:

Affected release

Linear ticket

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The focused tests pass locally: the core cost, provider, and dispatcher suites report 608 passed; spend tracking reports 225 passed; savings reports 97 passed; types reports 39 passed
  • My PR passes all required CI/CD checks on current commit 2cafaa308f
  • 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 on the current commit (5/5 for 2cafaa308f)

Local verification:

  • Patch coverage is 100% across 88 changed executable Python lines
  • All make check gates passed on 2cafaa308f, including circular-import, type-discipline, test-quality, frontend, and schema.d.ts sync checks
  • GitHub reports every required check as passing. The separate non-required OSV scan reports mlflow 3.15.0 / PYSEC-2026-3865 from the unchanged uv.lock; OSV reports no fixed release
  • Local CodSpeed wall-time A/B improved OpenAI from 20.17µs to 14.47µs and Anthropic from 21.50µs to 18.14µs
  • A second npm run gen:api produced no generated diff

Delays in PR merge?

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

Screenshots / Feature Verification

This no-mock proof used real dashscope/qwen3.8-max calls. A local relay forwarded request bodies unchanged to the development gateway and removed only upstream cost headers, forcing each checkout to calculate its own cost from the unmodified provider usage.

Both checkouts used the same model configuration below. The new implicit_cache_read_input_token_cost field enables separate implicit-cache pricing in the PR checkout; the baseline uses the existing shared cache-read rate. These values are illustrative custom rates, not a statement of the provider's current model prices:

input_cost_per_token: 0.000001
output_cost_per_token: 0.000002
cache_creation_input_token_cost: 0.00000125
cache_read_input_token_cost: 0.0000001
implicit_cache_read_input_token_cost: 0.0000002

The implicit request contained a stable prompt longer than 1,024 tokens and no cache_control. The explicit request used the same text with cache_control: {"type": "ephemeral"}. Each curl loop sent the request twice and captured the second response.

Before (30f33a9)

Implicit automatic cache hit

  1. Send the implicit request twice to the baseline main checkout (30f33a949b):

    for _ in 1 2; do
      curl --fail-with-body --silent --show-error \
        --dump-header before-implicit.headers \
        --output before-implicit.json \
        --header "Authorization: Bearer $LOCAL_PROXY_KEY" \
        --header "Content-Type: application/json" \
        --data-binary @implicit.json \
        http://127.0.0.1:4011/v1/chat/completions
    done
  2. Observe 50,176 cached tokens with no cache_type, priced using the existing shared cache-read rate:

    HTTP/1.1 200 OK
    x-litellm-response-cost-cache-read: 0.0050176
    usage.prompt_tokens: 50664
    usage.prompt_tokens_details.cached_tokens: 50176
    usage.prompt_tokens_details.cache_type: null
    

    50176 * 0.0000001 = 0.0050176

Explicit ephemeral cache hit

  1. Send the explicit request twice to the same main checkout:

    for _ in 1 2; do
      curl --fail-with-body --silent --show-error \
        --dump-header before-explicit.headers \
        --output before-explicit.json \
        --header "Authorization: Bearer $LOCAL_PROXY_KEY" \
        --header "Content-Type: application/json" \
        --data-binary @explicit.json \
        http://127.0.0.1:4011/v1/chat/completions
    done
  2. Observe 50,654 cached tokens with cache_type: "ephemeral", priced using the existing explicit cache-read rate:

    HTTP/1.1 200 OK
    x-litellm-response-cost-cache-read: 0.005065399999999999
    usage.prompt_tokens: 50668
    usage.prompt_tokens_details.cached_tokens: 50654
    usage.prompt_tokens_details.cache_creation_input_tokens: 0
    usage.prompt_tokens_details.cache_type: ephemeral
    

    50654 * 0.0000001 = 0.0050654

After (2cafaa3)

Implicit automatic cache hit

  1. Send the identical implicit request twice to the current PR checkout:

    for _ in 1 2; do
      curl --fail-with-body --silent --show-error \
        --dump-header after-implicit.headers \
        --output after-implicit.json \
        --header "Authorization: Bearer $LOCAL_PROXY_KEY" \
        --header "Content-Type: application/json" \
        --data-binary @implicit.json \
        http://127.0.0.1:4012/v1/chat/completions
    done
  2. Observe the same 50,176 cached tokens, now priced using the separately configured implicit-cache read rate:

    HTTP/1.1 200 OK
    x-litellm-response-cost-cache-read: 0.0100352
    usage.prompt_tokens: 50664
    usage.prompt_tokens_details.cached_tokens: 50176
    usage.prompt_tokens_details.cache_type: null
    

    50176 * 0.0000002 = 0.0100352

Explicit ephemeral cache hit

  1. Send the identical explicit request twice to the same PR checkout:

    for _ in 1 2; do
      curl --fail-with-body --silent --show-error \
        --dump-header after-explicit.headers \
        --output after-explicit.json \
        --header "Authorization: Bearer $LOCAL_PROXY_KEY" \
        --header "Content-Type: application/json" \
        --data-binary @explicit.json \
        http://127.0.0.1:4012/v1/chat/completions
    done
  2. Observe the same 50,654 explicit cached tokens still using the existing explicit rate:

    HTTP/1.1 200 OK
    x-litellm-response-cost-cache-read: 0.005065399999999999
    usage.prompt_tokens: 50668
    usage.prompt_tokens_details.cached_tokens: 50654
    usage.prompt_tokens_details.cache_creation_input_tokens: 0
    usage.prompt_tokens_details.cache_type: ephemeral
    

    50654 * 0.0000001 = 0.0050654

Type

🆕 New Feature

Caveats (if any)

Low

  • Separate implicit pricing is opt-in per model
  • Missing implicit rates preserve existing cache-read pricing

Final Attestation

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

@limdeng
limdeng force-pushed the litellm_dashscope_qwen_cache_pricing branch from 69b1718 to 07dc234 Compare September 7, 2026 09:03
@limdeng
limdeng changed the base branch from litellm_internal_staging to main September 14, 2026 03:31
@limdeng limdeng changed the title fix(dashscope): distinguish qwen cache pricing modes feat(dashscope): add separate explicit and implicit cache pricing Sep 17, 2026

This branch had an error being deployed

1 failed (outdated) deployment
e2e-changed — 645241a8 Deployed Sep 14, 2026 by limdeng via Run changed e2e tests against the stage-mirror stack #5
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