Add API cost tracking to admin dashboard
Track estimated Anthropic API costs per request across all Claude API call sites (recommender, analyze, artist-dive, generate-playlist, crate, rabbit-hole, playlist-fix, timeline, compatibility). Log token usage and estimated cost to the app logger. Aggregate costs in admin stats endpoint and display total/today costs and token usage in the admin dashboard.
This commit is contained in:
@@ -238,6 +238,15 @@ Return ONLY the JSON array, no other text."""
|
||||
messages=[{"role": "user", "content": prompt}],
|
||||
)
|
||||
|
||||
# Track API cost
|
||||
import logging
|
||||
api_logger = logging.getLogger("app")
|
||||
input_tokens = message.usage.input_tokens
|
||||
output_tokens = message.usage.output_tokens
|
||||
# Sonnet pricing: $3/M input, $15/M output
|
||||
cost = (input_tokens * 3 / 1_000_000) + (output_tokens * 15 / 1_000_000)
|
||||
api_logger.info(f"API_COST|model=claude-sonnet|input={input_tokens}|output={output_tokens}|cost=${cost:.4f}|user={user.id}|endpoint=generate_recommendations")
|
||||
|
||||
# Parse response
|
||||
response_text = message.content[0].text.strip()
|
||||
# Handle potential markdown code blocks
|
||||
|
||||
Reference in New Issue
Block a user