feat(trace): per-model cost estimation + fix token extraction#15
Open
kunalkushwaha wants to merge 1 commit into
Open
feat(trace): per-model cost estimation + fix token extraction#15kunalkushwaha wants to merge 1 commit into
kunalkushwaha wants to merge 1 commit into
Conversation
The trace fallback path read token attributes under keys the framework never emits (llm.usage.completion_tokens / llm.completion_tokens), so TotalTokens — and therefore the cost estimate — was always 0 for runs without a manifest. - Add internal/pricing: a published-list-price table (per 1M tokens) with exact + longest-prefix model matching (handles dated/variant ids and provider prefixes); local/unknown models return (0, false). Unit tested. - Read the real AgenticGoKit observability keys (agk.llm.tokens.input/output/total, agk.llm.model, agk.llm.cost.usd) plus legacy aliases for back-compat. - Replace the hardcoded `tokens * 0.00001` estimate: prefer a directly-reported cost, else a price-table estimate, else 0 for unknown/local models. Improves the accuracy of `agk trace list/view` and the `agk run` trace summary. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This was referenced Jun 21, 2026
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
Makes trace token counts and cost trustworthy in
agk trace list/viewand the newagk runsummary. This is feature A5 from theFEATURES.mdroadmap, and it fixes a latent bug along the way.The bug
The trace fallback path (
parseTraceFile, used when a run has nomanifest.json) read token usage under keys the framework never emits —llm.usage.completion_tokens/llm.completion_tokens. The real AgenticGoKit observability keys areagk.llm.tokens.input/agk.llm.tokens.output(seeinternal/observability/attributes.goin the core repo). As a result,TotalTokenswas always 0, and the cost — derived from it via a hardcodedtokens * 0.00001— was always 0 too.Changes
internal/pricingpackage: a published-list-price table (USD per 1M tokens, split input/output) with exact + longest-prefix model matching, so dated/variant ids (gpt-4o-2024-08-06,claude-sonnet-4-20250514) and provider prefixes (openai/gpt-4o) resolve to their base model. Local/unknown models return(0, false). Fully unit-tested (12 cases).cmd/trace.go: readsagk.llm.tokens.input/output/total,agk.llm.model, andagk.llm.cost.usd, keeping the old keys as back-compat aliases. Also tracks input/output separately.agk.llm.cost.usd, else a price-table estimate, else0for unknown/local models (instead of a misleading flat guess).Testing
go build,go vet,go test ./...,gofmtall green.internal/pricingunit tests cover exact/prefix/provider-prefix/case/zero/unknown.agk trace list→ Tokens 3000 (was 0),agk trace view→ Est. Cost $0.0150 (correct: 2000/1M×$2.50 + 1000/1M×$10.00).Note
Prices are approximate list prices for rough reporting, not billing; they drift and the table should be updated over time.
🤖 Generated with Claude Code