Repository navigation
Commit 627579b
authored
fix(deepseek): read documented top-level prompt-cache counters (#2252)
What:
- Add src/api/transform/chat-usage-normalization.ts, a shared normalizer that
resolves OpenAI-shaped chat-completions usage into
{ inputTokens, outputTokens, cacheReadTokens, cacheWriteTokens }.
- DeepSeekHandler.processUsageMetrics now uses the normalizer instead of
reading usage.prompt_tokens_details.cache_miss_tokens directly.
- Rewrite the deepseek spec fixtures to DeepSeek's documented wire shape and
add top-level-only, nested-only-legacy, and both-shapes-present cases.
Why:
DeepSeek's OpenAPI marks prompt_cache_hit_tokens and prompt_cache_miss_tokens
as required top-level members and declares only cached_tokens inside
prompt_tokens_details. The nested cache_miss_tokens field the provider read
does not exist, so cacheWriteTokens was permanently undefined. DeepSeek
catalog entries set inputPrice: 0 (inputs are priced as cache read/write
only), so calculateApiCostOpenAI priced every cache-miss input token at zero:
a permanent, near-total under-report of DeepSeek input cost.
The old spec hid this by mocking the non-existent nested shape and asserting
cacheWriteTokens from it, so the suite was green because it mocked the bug.
Read priority is per-field with no summing, because the two shapes describe
the same quantities:
- cacheWriteTokens: prompt_cache_miss_tokens, then nested cache_miss_tokens
(legacy fallback), then nested cache_write_tokens.
- cacheReadTokens: nested cached_tokens, then prompt_cache_hit_tokens, then
cache_read_input_tokens.
Impact:
DeepSeek input cost is now reported instead of collapsing to ~$0 for
cache-miss tokens. The helper is generic over OpenAI-shaped usage so the
family-wide provider rewiring can adopt it without a second implementation.
No behavior change for providers that do not use the helper.
Refs:
https://api-docs.deepseek.com/api/create-chat-completion
https://api-docs.deepseek.com/api/create-completion1 parent 07007b4 commit 627579b
4 files changed
Lines changed: 323 additions & 27 deletions
File tree
- src/api
- providers
- __tests__
- transform
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
31 | 30 | | |
32 | 31 | | |
| 32 | + | |
| 33 | + | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
| |||
111 | 112 | | |
112 | 113 | | |
113 | 114 | | |
114 | | - | |
115 | 115 | | |
116 | 116 | | |
| 117 | + | |
| 118 | + | |
117 | 119 | | |
118 | 120 | | |
119 | 121 | | |
| |||
517 | 519 | | |
518 | 520 | | |
519 | 521 | | |
520 | | - | |
521 | | - | |
522 | | - | |
523 | | - | |
524 | | - | |
525 | | - | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
526 | 525 | | |
| 526 | + | |
527 | 527 | | |
528 | | - | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
529 | 546 | | |
530 | | - | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
531 | 571 | | |
532 | 572 | | |
533 | 573 | | |
534 | 574 | | |
535 | 575 | | |
536 | 576 | | |
537 | 577 | | |
538 | | - | |
539 | | - | |
540 | | - | |
| 578 | + | |
541 | 579 | | |
542 | | - | |
543 | 580 | | |
544 | 581 | | |
545 | 582 | | |
546 | 583 | | |
547 | 584 | | |
548 | 585 | | |
549 | | - | |
550 | | - | |
551 | | - | |
552 | | - | |
553 | | - | |
554 | | - | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
555 | 595 | | |
556 | | - | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
557 | 599 | | |
| 600 | + | |
558 | 601 | | |
559 | 602 | | |
560 | 603 | | |
561 | 604 | | |
562 | | - | |
| 605 | + | |
563 | 606 | | |
564 | 607 | | |
565 | | - | |
| 608 | + | |
566 | 609 | | |
567 | 610 | | |
568 | 611 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
227 | 228 | | |
228 | 229 | | |
229 | 230 | | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
230 | 236 | | |
| 237 | + | |
| 238 | + | |
231 | 239 | | |
232 | 240 | | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
237 | 245 | | |
238 | 246 | | |
239 | 247 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
0 commit comments