Skip to content

feat(trace): per-model cost estimation + fix token extraction#15

Open
kunalkushwaha wants to merge 1 commit into
mainfrom
feat/trace-cost
Open

feat(trace): per-model cost estimation + fix token extraction#15
kunalkushwaha wants to merge 1 commit into
mainfrom
feat/trace-cost

Conversation

@kunalkushwaha

Copy link
Copy Markdown
Member

What

Makes trace token counts and cost trustworthy in agk trace list/view and the new agk run summary. This is feature A5 from the FEATURES.md roadmap, and it fixes a latent bug along the way.

The bug

The trace fallback path (parseTraceFile, used when a run has no manifest.json) read token usage under keys the framework never emitsllm.usage.completion_tokens / llm.completion_tokens. The real AgenticGoKit observability keys are agk.llm.tokens.input / agk.llm.tokens.output (see internal/observability/attributes.go in the core repo). As a result, TotalTokens was always 0, and the cost — derived from it via a hardcoded tokens * 0.00001 — was always 0 too.

Changes

  • New internal/pricing package: 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).
  • Correct attribute keys in cmd/trace.go: reads agk.llm.tokens.input/output/total, agk.llm.model, and agk.llm.cost.usd, keeping the old keys as back-compat aliases. Also tracks input/output separately.
  • Honest cost: prefer a directly-reported agk.llm.cost.usd, else a price-table estimate, else 0 for unknown/local models (instead of a misleading flat guess).

Testing

  • go build, go vet, go test ./..., gofmt all green.
  • internal/pricing unit tests cover exact/prefix/provider-prefix/case/zero/unknown.
  • End-to-end against a synthetic trace (2× gpt-4o spans, 2000 in / 1000 out):
    agk trace listTokens 3000 (was 0), agk trace viewEst. 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.

Branched from main, independent of #13/#14. A related upstream bug (the framework's own manifest.json never populates tokens/cost) is being filed separately on the AgenticGoKit repo.

🤖 Generated with Claude Code

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant