innercontext/backend/alembic/versions
Piotr Oleszczyk 3bf19d8acb feat(api): add enhanced token metrics logging for Gemini API
Add comprehensive token breakdown logging to understand MAX_TOKENS behavior
and verify documentation claims about thinking tokens.

New Fields Added to ai_call_logs:
- thoughts_tokens: Thinking tokens (thoughtsTokenCount) - documented as
  separate from output budget
- tool_use_prompt_tokens: Tool use overhead (toolUsePromptTokenCount)
- cached_content_tokens: Cached content tokens (cachedContentTokenCount)

Purpose:
Investigate token counting mystery from production logs where:
  prompt_tokens: 4400
  completion_tokens: 589
  total_tokens: 8489  ← Should be 4400 + 589 = 4989, missing 3500!

According to Gemini API docs (Polish translation):
  totalTokenCount = promptTokenCount + candidatesTokenCount
  (thoughts NOT included in total)

But production logs show 3500 token gap. New logging will reveal:
1. Are thinking tokens actually separate from max_output_tokens limit?
2. Where did the 3500 missing tokens go?
3. Does MEDIUM thinking level consume output budget despite docs?
4. Are tool use tokens included in total but not shown separately?

Changes:
- Added 3 new integer columns to ai_call_logs (nullable)
- Enhanced llm.py to capture all usage_metadata fields
- Used getattr() for safe access (fields may not exist in all responses)
- Database migration: 7e6f73d1cc95

This will provide complete data for future LLM calls to diagnose:
- MAX_TOKENS failures
- Token budget behavior
- Thinking token costs
- Tool use overhead
2026-03-06 12:17:13 +01:00
..
1f7e3b9c4a2d_add_user_profile_table.py feat(profile): add profile settings and LLM user context 2026-03-05 15:57:21 +01:00
7c91e4b2af38_replace_price_tier_with_objective_price_fields.py feat(products): compute price tiers from objective price/use 2026-03-04 14:47:18 +01:00
7e6f73d1cc95_add_enhanced_token_metrics_to_ai_call_.py feat(api): add enhanced token metrics logging for Gemini API 2026-03-06 12:17:13 +01:00
8e4c1b7a9d2f_drop_usage_notes_and_contraindications_from_products.py refactor(products): remove usage notes and contraindications fields 2026-03-05 10:11:24 +01:00
27b2c306b0c6_add_short_id_column_to_products.py feat(api): add short_id column for consistent LLM UUID handling 2026-03-06 10:58:26 +01:00
60c8e1ade29d_add_validation_fields_to_ai_call_logs.py feat(api): add LLM response validation and input sanitization 2026-03-06 10:16:47 +01:00
2697b4f1972d_add_reasoning_chain_to_ai_call_logs.py feat(api): implement Phase 2 token optimization and reasoning capture 2026-03-06 10:26:29 +01:00
a1b2c3d4e5f6_add_ai_call_logs.py feat(routines): add minoxidil beard/mustache option to routine suggestions 2026-03-01 19:46:07 +01:00
b2c3d4e5f6a1_add_finish_reason_to_ai_call_logs.py fix(llm): log and handle non-STOP finish_reason from Gemini 2026-03-01 20:08:22 +01:00
c2d626a2b36c_initial_schema.py fix(backend): apply black/isort formatting and fix ruff noqa annotations 2026-03-01 17:27:07 +01:00
d3e4f5a6b7c8_add_tool_trace_to_ai_call_logs.py feat(api): add INCI tool-calling with normalized tool traces 2026-03-04 11:35:19 +01:00
e4f5a6b7c8d9_drop_product_interaction_columns.py refactor(products): remove obsolete interaction fields across stack 2026-03-04 12:42:12 +01:00
f1a2b3c4d5e6_add_async_pricing_jobs_and_snapshot_fields.py fix(backend): create pricetier enum before migration 2026-03-04 23:16:55 +01:00