Skip to content

feat(backend): per-app token usage & cost attribution#13

Open
varunmaggo wants to merge 1 commit into
aws-samples:mainfrom
varunmaggo:feat/token-cost-attribution
Open

feat(backend): per-app token usage & cost attribution#13
varunmaggo wants to merge 1 commit into
aws-samples:mainfrom
varunmaggo:feat/token-cost-attribution

Conversation

@varunmaggo

Copy link
Copy Markdown

Extends the existing per-app metrics pipeline (which already tracks request counts, latency percentiles, and error rates) to also attribute LLM token usage and computed cost to each published agent app.

  • New cost-model module: pure pricing table (USD/1M tokens per model) with a flagged fallback for unknown models. Cost is computed in integer micro-USD so it accumulates safely with DynamoDB atomic ADD counters (no float drift). Negative / non-finite token counts clamp to zero.
  • AccessLogEntry gains optional model / inputTokens / outputTokens; older log lines without them are treated as zero (backwards compatible).
  • computeMetrics sums totalInputTokens / totalOutputTokens / totalCostMicroUsd; upsert ADDs them to the hourly METRICS# item.
  • getAppMetrics surfaces token totals and cost (USD) plus per-bucket token/cost in the time series.
  • GraphQL AppMetrics / AppMetricsBucket extended with the new fields (all default to 0, so existing buckets and the dashboard keep working).
  • Unit tests for the cost model and token/cost aggregation; existing metrics property tests remain green.

Frontend dashboard surfacing and per-app budget alerting are follow-ups.

Claude-Session: https://claude.ai/code/session_01Jo23wVkjxvhMhEbyWxngEv

Issue #, if available:

Description of changes:

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Extends the existing per-app metrics pipeline (which already tracks
request counts, latency percentiles, and error rates) to also attribute
LLM token usage and computed cost to each published agent app.

- New cost-model module: pure pricing table (USD/1M tokens per model)
  with a flagged fallback for unknown models. Cost is computed in integer
  micro-USD so it accumulates safely with DynamoDB atomic ADD counters
  (no float drift). Negative / non-finite token counts clamp to zero.
- AccessLogEntry gains optional model / inputTokens / outputTokens; older
  log lines without them are treated as zero (backwards compatible).
- computeMetrics sums totalInputTokens / totalOutputTokens /
  totalCostMicroUsd; upsert ADDs them to the hourly METRICS# item.
- getAppMetrics surfaces token totals and cost (USD) plus per-bucket
  token/cost in the time series.
- GraphQL AppMetrics / AppMetricsBucket extended with the new fields
  (all default to 0, so existing buckets and the dashboard keep working).
- Unit tests for the cost model and token/cost aggregation; existing
  metrics property tests remain green.

Frontend dashboard surfacing and per-app budget alerting are follow-ups.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jo23wVkjxvhMhEbyWxngEv
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.

2 participants