fleet-memory/hindsight-api/tests
Alexander Pinsker 29a542dc23
feat: Add per-request LLM token usage metrics (#117)
* feat: Record LLM token metrics via Prometheus

Wire up the existing token metrics infrastructure to actually record
token usage from LLM calls. The MetricsCollector already had
record_tokens() method and Prometheus counters (hindsight.tokens.input,
hindsight.tokens.output), but they were never being populated.

Changes:
- Import get_metrics_collector in llm_wrapper.py
- Call record_tokens() after successful LLM calls for:
  - OpenAI/Groq (using response.usage.prompt_tokens, completion_tokens)
  - Anthropic (using response.usage.input_tokens, output_tokens)
  - Gemini (using response.usage_metadata.prompt_token_count, candidates_token_count)
- Add test file to verify token metrics are recorded

Note: Ollama's native API doesn't return token usage, so metrics
are not recorded for that provider.

The token metrics will now be available via /metrics endpoint:
- hindsight_tokens_input_total
- hindsight_tokens_output_total

* feat: add per-request token usage tracking to retain and reflect endpoints

- Add TokenUsage model with input_tokens, output_tokens, total_tokens
- Return usage metrics in retain response (sync operations only)
- Return usage metrics in reflect response
- Update Python, TypeScript, and Rust clients
- Add API documentation for usage fields
- Add changelog entry
2026-01-08 10:36:58 +01:00
..
fixtures rename to hindsight (#2) 2025-11-25 19:28:26 +01:00
__init__.py rename to hindsight (#2) 2025-11-25 19:28:26 +01:00
conftest.py feat: extensions (#54) 2025-12-22 11:05:23 +01:00
test_admin_backup_restore.py feat: run db migrations offline (optionally) (#114) 2026-01-07 15:49:51 +01:00
test_agents_api.py feat: extensions (#54) 2025-12-22 11:05:23 +01:00
test_batch_chunking.py feat: extensions (#54) 2025-12-22 11:05:23 +01:00
test_causal_relationships.py fix: improve causal links detection (#111) 2026-01-07 11:16:24 +01:00
test_chunking.py improve retain performances, caching and tests 2025-12-08 18:21:56 +01:00
test_combined_scoring.py feat: extensions (#54) 2025-12-22 11:05:23 +01:00
test_custom_embedding_dimension.py feat: configurable embedding dimensions + OpenAI Embeddings (#101) 2026-01-05 14:43:05 +01:00
test_document_tracking.py feat: extensions (#54) 2025-12-22 11:05:23 +01:00
test_extensions.py feat: extensions (#54) 2025-12-22 11:05:23 +01:00
test_fact_extraction_quality.py Feature/graph viz (#85) 2026-01-02 16:27:29 +01:00
test_fact_ordering.py feat: extensions (#54) 2025-12-22 11:05:23 +01:00
test_http_api_integration.py feat: Add per-request LLM token usage metrics (#117) 2026-01-08 10:36:58 +01:00
test_link_utils.py prepare for release 2025-12-03 11:52:25 +01:00
test_llm_provider.py fix: ollama structured support (#63) 2025-12-22 16:35:24 +01:00
test_llm_token_metrics.py feat: Add per-request LLM token usage metrics (#117) 2026-01-08 10:36:58 +01:00
test_mcp_local.py feat: add local mcp server (#32) 2025-12-16 10:50:20 +01:00
test_mcp_routing.py feat: add local mcp server (#32) 2025-12-16 10:50:20 +01:00
test_observations.py feat: delete document from ui (#71) 2025-12-23 13:54:06 +01:00
test_query_analyzer.py fix: ci and ui build (#9) 2025-12-03 15:47:53 +01:00
test_retain.py misc: add mcp integration tests and increase test coverage (#98) 2026-01-05 11:16:55 +01:00
test_schema_isolation.py feat: extensions (#54) 2025-12-22 11:05:23 +01:00
test_search_trace.py feat: extensions (#54) 2025-12-22 11:05:23 +01:00
test_sql_schema_safety.py feat: extensions (#54) 2025-12-22 11:05:23 +01:00
test_temporal_ranges.py feat: extensions (#54) 2025-12-22 11:05:23 +01:00
test_think.py feat: extensions (#54) 2025-12-22 11:05:23 +01:00