Commit 7ca5357
feat: capture provider cost details (#521)
## Summary
- **fix**: add `upstream_inference_cost` fallback for total cost so BYOK
requests (where `usage.cost=0`) report the real spend
- **refactor**: stop aliasing `upstream_inference_prompt_cost` →
`input_cost`; keep upstream fields separate since
`upstream_inference_prompt_cost = input_cost + cached_input_cost`
(aliasing silently zeroed out `costCached`)
- **feat**: add three-tier cost dispatch in `applyUsageCostDetails` —
superset (per-bucket gateway fields) → normal (upstream
prompt/completions split with cache ratio) → minimal (proportional
fallback)
- **test**: expand coverage with edge cases — zero-cost non-BYOK,
OpenRouter markup (cost >> upstream sum), zero prompt tokens,
heavy-cache-hit ratio split, end-to-end BYOK and non-BYOK flows
## Test plan
- [x] All 1378 backend tests pass (`bun run test:force-all`)
- [x] Biome lint clean (`bunx biome lint .`)
- [x] Each commit independently passes lint + tests (verified via `git
rebase --exec`)
> **Note:** I mixed up the branches in #443, and also mixed up #520.
That PR couldn't be reopened due to a force-push. This replaces that PR.
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Matt Cowger <mcowger@users.noreply.github.com>
Co-authored-by: Matt Cowger <matt@cowger.us>1 parent fec6004 commit 7ca5357
6 files changed
Lines changed: 1210 additions & 87 deletions
File tree
- packages/backend/src
- services
- inspectors
- utils
- __tests__
Lines changed: 21 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
148 | 149 | | |
149 | 150 | | |
150 | 151 | | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
151 | 161 | | |
152 | 162 | | |
153 | 163 | | |
| |||
318 | 328 | | |
319 | 329 | | |
320 | 330 | | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
330 | 342 | | |
331 | 343 | | |
332 | 344 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
502 | 502 | | |
503 | 503 | | |
504 | 504 | | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
505 | 514 | | |
506 | 515 | | |
507 | 516 | | |
| |||
0 commit comments