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
@@ -280,6 +280,46 @@ async def test_openai_empty_batch():
|
||||
assert embeddings == []
|
||||
|
||||
|
||||
def _embed_item(embedding, index):
|
||||
item = MagicMock()
|
||||
item.embedding = embedding
|
||||
item.index = index
|
||||
return item
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
async def test_openai_embed_batch_with_usage_reports_tokens(mock_openai_client):
|
||||
"""embed_batch_with_usage returns the response's total_tokens."""
|
||||
response = MagicMock()
|
||||
response.data = [_embed_item([0.1, 0.2], 0), _embed_item([0.3, 0.4], 1)]
|
||||
response.usage = MagicMock(total_tokens=9)
|
||||
mock_openai_client.embeddings.create = AsyncMock(return_value=response)
|
||||
|
||||
provider = OpenAIProvider(
|
||||
api_key="test-key", embedding_model="text-embedding-3-small"
|
||||
)
|
||||
embeddings, tokens = await provider.embed_batch_with_usage(["a", "b"])
|
||||
|
||||
assert embeddings == [[0.1, 0.2], [0.3, 0.4]]
|
||||
assert tokens == 9
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
async def test_openai_with_usage_estimates_when_usage_absent(mock_openai_client):
|
||||
"""Missing usage falls back to the char-based estimate."""
|
||||
response = MagicMock()
|
||||
response.data = [_embed_item([0.1], 0)]
|
||||
response.usage = None
|
||||
mock_openai_client.embeddings.create = AsyncMock(return_value=response)
|
||||
|
||||
provider = OpenAIProvider(
|
||||
api_key="test-key", embedding_model="text-embedding-3-small"
|
||||
)
|
||||
_, tokens = await provider.embed_with_usage("abcdefgh") # 8 chars → 2 tokens
|
||||
|
||||
assert tokens == 2
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
async def test_openai_close(mock_openai_client):
|
||||
"""Test OpenAI client close."""
|
||||
|
||||
Reference in New Issue
Block a user