feat(usage): meter embedding tokens as embeddings_queries on both paths
embeddings_queries now records the embedding request's token count (the unit upstream providers bill on) instead of an operation count, and fires on the indexing path too. Previously only semantic search recorded it (value=1), so a re-indexing run produced no embeddings_queries events at all — only pages_chunks. - Provider layer: additive embed_with_usage / embed_batch_with_usage surface the per-request token count (Mistral/OpenAI usage.total_tokens, Bedrock Titan inputTextTokenCount, Ollama prompt_eval_count); a char-based estimate is the fallback (Simple, and any provider/response without a token field). Gateway and EmbeddingService forward through. The count travels as a return value / a per-request SearchAlgorithm attribute — never on the singleton — so concurrent indexing + search can't mis-attribute bills. - Indexing (vector/processor.py): records embeddings_queries (value=batch tokens) alongside the existing pages_chunks event. - Search (server/semantic.py): value is now the query embedding's token count, relayed from BM25HybridSearchAlgorithm via query_token_count. The astrolabe_embeddings_queries Stripe meter (sum aggregation) now sums tokens with no CP/Terraform change. The meter "queries"->tokens naming/unit clarification (homelab-terraform #254) + CP rollup/portal copy is a follow-up. Deck #67. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
fe17994c4d
commit
64318f0b25
@@ -809,7 +809,10 @@ async def _index_document(
|
||||
embedding_service = get_embedding_service()
|
||||
embed_start = time.time()
|
||||
try:
|
||||
dense_embeddings = await embedding_service.embed_batch(chunk_texts)
|
||||
(
|
||||
dense_embeddings,
|
||||
embed_tokens,
|
||||
) = await embedding_service.embed_batch_with_usage(chunk_texts)
|
||||
except Exception:
|
||||
record_embedding(
|
||||
"dense", provider, time.time() - embed_start, status="error"
|
||||
@@ -833,30 +836,43 @@ async def _index_document(
|
||||
# keep Deck #67's future per-user attribution derivable from the
|
||||
# app DB without a re-migration.
|
||||
if settings.usage_metering_enabled:
|
||||
# Two billable events per indexed document: 'pages_chunks' is
|
||||
# the volume (chunks embedded); 'embeddings_queries' is the
|
||||
# token count of the embedding request — the same metric search
|
||||
# records, so the meter bills embedding tokens whether they were
|
||||
# incurred indexing a document or embedding a query (Deck #67).
|
||||
metering_metadata = {
|
||||
"provider": provider,
|
||||
"model": settings.get_embedding_model_name(),
|
||||
"doc_type": doc_task.doc_type,
|
||||
"user_id": doc_task.user_id,
|
||||
"total_chars": total_chars,
|
||||
}
|
||||
try:
|
||||
store = await UsageEventStore.shared()
|
||||
await store.record_usage_event(
|
||||
metric="pages_chunks",
|
||||
value=len(chunk_texts),
|
||||
metadata={
|
||||
"provider": provider,
|
||||
"model": settings.get_embedding_model_name(),
|
||||
"doc_type": doc_task.doc_type,
|
||||
"user_id": doc_task.user_id,
|
||||
"total_chars": total_chars,
|
||||
},
|
||||
metadata=metering_metadata,
|
||||
# The outer guard already confirmed the flag, so pass
|
||||
# enabled=True directly — the store then skips a second
|
||||
# uncached Settings build here (ADR-024).
|
||||
enabled=True,
|
||||
)
|
||||
await store.record_usage_event(
|
||||
metric="embeddings_queries",
|
||||
value=embed_tokens,
|
||||
metadata=metering_metadata,
|
||||
enabled=True,
|
||||
)
|
||||
except Exception:
|
||||
# Reached only when shared()/store construction itself
|
||||
# raises (record_usage_event swallows its own write
|
||||
# failures). Metering is on, so warn rather than hide the
|
||||
# "enabled but no billing data" case in DEBUG logs.
|
||||
logger.warning(
|
||||
"usage metering hook (pages_chunks) skipped", exc_info=True
|
||||
"usage metering hook (indexing embeddings) skipped",
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
async def generate_sparse_embeddings():
|
||||
|
||||
Reference in New Issue
Block a user