innercontext/backend
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
..
alembic feat(api): add enhanced token metrics logging for Gemini API 2026-03-06 12:17:13 +01:00
innercontext feat(api): add enhanced token metrics logging for Gemini API 2026-03-06 12:17:13 +01:00
jobs/2026-03-02__17-12-31 feat(api): add short_id column for consistent LLM UUID handling 2026-03-06 10:58:26 +01:00
tests style: apply black and isort formatting 2026-03-06 10:17:00 +01:00
.coverage feat(api): add short_id column for consistent LLM UUID handling 2026-03-06 10:58:26 +01:00
.env.example fix: load .env via python-dotenv; SQLite default for local dev 2026-02-26 20:51:13 +01:00
.python-version Initial commit: backend API, data models, and test suite 2026-02-26 15:10:24 +01:00
alembic.ini feat(backend): add Alembic migrations 2026-02-28 20:14:57 +01:00
db.py Initial commit: backend API, data models, and test suite 2026-02-26 15:10:24 +01:00
main.py style: apply black and isort formatting 2026-03-06 10:17:00 +01:00
pgloader.config feat(api): add short_id column for consistent LLM UUID handling 2026-03-06 10:58:26 +01:00
pyproject.toml chore(backend): enable psycopg binary dependency 2026-03-04 21:46:38 +01:00
README.md Initial commit: backend API, data models, and test suite 2026-02-26 15:10:24 +01:00
skincare.yaml Initial commit: backend API, data models, and test suite 2026-02-26 15:10:24 +01:00
test_query.py refactor(llm): optimize Gemini config profiles for extraction and creativity 2026-03-03 21:24:23 +01:00
uv.lock chore(backend): enable psycopg binary dependency 2026-03-04 21:46:38 +01:00

See the root README for setup and usage instructions.