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:
@@ -106,6 +106,13 @@ Return ONLY a valid JSON object with "decades" and "insight" keys. No other text
|
||||
messages=[{"role": "user", "content": prompt}],
|
||||
)
|
||||
|
||||
# Track API cost (Haiku: $0.80/M input, $4/M output)
|
||||
api_logger = logging.getLogger("app")
|
||||
input_tokens = message.usage.input_tokens
|
||||
output_tokens = message.usage.output_tokens
|
||||
cost = (input_tokens * 0.80 / 1_000_000) + (output_tokens * 4 / 1_000_000)
|
||||
api_logger.info(f"API_COST|model=claude-haiku|input={input_tokens}|output={output_tokens}|cost=${cost:.4f}|user={user.id}|endpoint=timeline")
|
||||
|
||||
response_text = message.content[0].text.strip()
|
||||
|
||||
# Try to extract JSON if wrapped in markdown code blocks
|
||||
|
||||
Reference in New Issue
Block a user