fix(sight): per-call token in audit records#681
Open
jfeng18 wants to merge 1 commit into
Open
Conversation
74832bd to
d2ac3a6
Compare
Contributor
Author
|
Gentle ping — this fixes #699 (per-call token always null in audit records). 3 discriminating unit tests + ECS E2E verified. Ready for review. |
9c2caf8 to
3558624
Compare
Contributor
Author
|
Per-call token audit (123 lines). Adds per-invocation token tracking to the audit path. |
The aggregate token count (agentsight token) was correct, but per-call audit records (agentsight audit --type llm --json) showed input_tokens=None / output_tokens=None for every call. Two root causes: 1. Token extraction was gated on is_sse — non-streaming LLM responses never had their usage JSON parsed. Relax the gate to also try parse_data() on single-object response bodies. 2. analyze_http() filtered out all non-SSE requests, so non- streaming completions never got an audit record at all. Add an is_llm_path check that admits known LLM API endpoints even when is_sse=false. Also fix a latent bug: is_sse was hardcoded to true in the audit record; changed to propagate http_record.is_sse. 3 discriminating unit tests added (non-SSE LLM → audit with correct is_sse=false + tokens; non-LLM path → no audit; SSE → is_sse=true). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
3558624 to
e79ff6e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
agentsight audit --type llm --jsonshowedinput_tokens=None / output_tokens=Nonefor every LLM call, even though the aggregate (agentsight token) reported the correct total. Users could see "how much did I spend today" but not "which prompt cost the most."Root causes (two independent gaps)
Token extraction gated on
is_sseonly — non-streaming LLM responses (stream:false) never had theirusageJSON parsed. The fallback inanalyze_aggregated()only tried the SSE chunk-array path.Audit record creation gated on
is_sseonly —analyze_http()returnedNonefor all non-SSE requests, so non-streaming completions never got an audit record at all. Also,is_ssewas hardcoded totruein the audit record struct.Fixes
parse_data()on single-object response bodies (non-streaming path).is_llm_pathcheck that admits known LLM API endpoints (/chat/completions,/v1/messages,/v1/completions,/api/v1/copilot/generate_copilot) even whenis_sse=false.is_sse: truehardcode tois_sse: http_record.is_sse.Testing
in=17074 out=1 model=qwen3-coder-plus is_sse=true(wasNone/None/None). Aggregate unchanged.is_sse=false+ correct token counts; (2) non-LLM path → no audit record; (3) SSE path →is_sse=true. Reverting either fix makes the corresponding test fail. 442 lib tests, zero regressions.stream:false) path not exercisable via cosh (always streams) or curl (short-lived process, not traced). Logic correctness covered by unit tests +parse_data()already validated in cache_shadow'ssaved_tokensfallback.Independent of #661–#668, #676, #680.
Fixes #699