Commit 78136e4
committed
fix(ccr): fold dropped continuation rounds' usage into cost_with_headroom
handle_response returns only the final continuation round, so the dropped
rounds' usage was captured into /stats ccr_overhead but excluded from
cost_with_headroom. The cost.py comment claiming it was 'already inside
cost_with_headroom' did not hold: handle_response returns only current_response,
the handler adopts it as resp_json, and the outcome funnel reads usage from
that single final-round response. The intermediate rounds go through a raw
api_call_fn and skip the funnel.
Fold the dropped rounds' usage into the billed totals, cache-split per
provider, reusing the proactive-expansion ContextVar pattern already in this
PR. Cache-split matters: Anthropic input_tokens is the uncached portion, but
OpenAI prompt_tokens is gross (includes cached_tokens), so collapsing to one
input number would price cached continuation tokens at full list instead of
the discounted rate.
- response_handler.handle_response: sum each dropped round's usage into locals
as (uncached_input, cache_read, cache_write, output) via the new
_extract_round_cost_usage (provider-aware; mirrors the per-handler parsing),
and publish via set_pending_ccr_continuation_usage before returning. Locals,
not the shared instance counters, so concurrent requests on one handler do
not mix. The instance accumulators stay for the /stats attribution view.
Signature and all call sites unchanged.
- outcome.emit_request_outcome: consume it and add each bucket to the field
cost_with_headroom prices (uncached_input_tokens at list, cache_read_tokens
discounted, cache_write_tokens premium; output_tokens to the budget/metrics
total, since cost_with_headroom itself is input-only). Client-facing
response.usage is untouched. The >=500 short-circuit still books nothing.
- cost.py: fix the comment.
OpenAI cache_write inference (the _infer_openai_cache_write_tokens heuristic)
is not replicated in _extract_round_cost_usage; only explicitly reported
cache_creation is credited. The batch path (multiple handle_response calls,
one emit) is a known follow-up.
Verified: 148 passed across CCR / proactive-expansion / stats / outcome, 0
regressions.1 parent fcdebb5 commit 78136e4
4 files changed
Lines changed: 532 additions & 3 deletions
File tree
- headroom
- ccr
- proxy
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
459 | 459 | | |
460 | 460 | | |
461 | 461 | | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
462 | 475 | | |
463 | 476 | | |
464 | 477 | | |
| |||
541 | 554 | | |
542 | 555 | | |
543 | 556 | | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
544 | 562 | | |
545 | 563 | | |
546 | 564 | | |
| |||
555 | 573 | | |
556 | 574 | | |
557 | 575 | | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
558 | 598 | | |
559 | 599 | | |
560 | 600 | | |
| |||
616 | 656 | | |
617 | 657 | | |
618 | 658 | | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
619 | 712 | | |
620 | 713 | | |
621 | 714 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
670 | 670 | | |
671 | 671 | | |
672 | 672 | | |
673 | | - | |
674 | | - | |
675 | | - | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
676 | 683 | | |
677 | 684 | | |
678 | 685 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 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 | + | |
59 | 88 | | |
60 | 89 | | |
61 | 90 | | |
| |||
362 | 391 | | |
363 | 392 | | |
364 | 393 | | |
| 394 | + | |
| 395 | + | |
365 | 396 | | |
366 | 397 | | |
367 | 398 | | |
| |||
385 | 416 | | |
386 | 417 | | |
387 | 418 | | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
388 | 425 | | |
389 | 426 | | |
390 | 427 | | |
| |||
403 | 440 | | |
404 | 441 | | |
405 | 442 | | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
406 | 462 | | |
407 | 463 | | |
408 | 464 | | |
| |||
0 commit comments