Commit b4acb6b
feat: add token usage tracking to api-proxy sidecar (#1539)
* feat: add token usage tracking to api-proxy sidecar
Intercept LLM API responses in the api-proxy to extract and log token
usage data. Supports both streaming (SSE) and non-streaming JSON
responses from OpenAI, Anthropic, and Copilot providers.
- Add token-tracker.js module with SSE and JSON usage extraction
- Integrate trackTokenUsage into server.js proxy response pipeline
- Write JSONL logs to /var/log/api-proxy/token-usage.jsonl
- Update input_tokens_total and output_tokens_total metrics
- Add 32 unit tests covering all extraction and normalization paths
Closes #1536
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* fix: address PR review feedback for token tracker
- Add token-tracker.js to Dockerfile COPY step
- Add try/catch require guard for graceful fallback
- Fix header comment to match actual architecture
- Guard empty usage objects from producing 0-token log entries
- Handle write backpressure in JSONL log writer
- Make closeLogStream() async to flush before process.exit
- Remove unused method/targetHost opts; drop hardcoded request_bytes
- Redirect test log output to temp dir to avoid /var/log noise
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent e31b23e commit b4acb6b
4 files changed
Lines changed: 884 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
21 | 33 | | |
22 | 34 | | |
23 | 35 | | |
| |||
423 | 435 | | |
424 | 436 | | |
425 | 437 | | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
426 | 447 | | |
427 | 448 | | |
428 | 449 | | |
| |||
851 | 872 | | |
852 | 873 | | |
853 | 874 | | |
854 | | - | |
| 875 | + | |
855 | 876 | | |
| 877 | + | |
856 | 878 | | |
857 | 879 | | |
858 | 880 | | |
859 | | - | |
| 881 | + | |
860 | 882 | | |
| 883 | + | |
861 | 884 | | |
862 | 885 | | |
863 | 886 | | |
| |||
0 commit comments