mirror of
https://github.com/teamchong/pxpipe.git
synced 2026-07-22 02:02:51 +02:00
cb8377a6e4
ProxyEvent gains two new fields: - usage: Anthropic's input/output/cache_creation/cache_read tokens. - firstByteMs: ms to upstream response headers (durationMs now measures end-to-end since the event fires after usage is extracted). Implementation (teeForUsage): - Tee the response body. Client gets one side immediately; we read the other in the background. - SSE: scan up to 64 KiB for 'event: message_start' + the data: payload. Usage is always in the first event so this terminates within ms of upstream first byte. - JSON: buffer up to 4 MiB and parse. - Error responses (status >= 400) and bodyless responses skip extraction. - Drains the tee fully in the background to avoid backpressure. Without this, the proxy had no way to validate its own value proposition. Now we can compute cache hit rate, real input-token savings, etc. directly from log events. Tests: +3 (JSON usage, SSE message_start usage, error skips extraction).