Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

feat(triton): add embedding usage estimation for self-hosted responses (#25345)

Sameer Kankute6e6f5be3e4b0
diff --git a/litellm/llms/triton/embedding/transformation.py b/litellm/llms/triton/embedding/transformation.py
index 8ab0277e369..93d1c25f169 100644
--- a/litellm/llms/triton/embedding/transformation.py
+++ b/litellm/llms/triton/embedding/transformation.py
@@ -8,7 +8,8 @@ from litellm.llms.base_llm.embedding.transformation import (
LiteLLMLoggingObj,
)
from litellm.types.llms.openai import AllEmbeddingInputValues
-from litellm.types.utils import EmbeddingResponse
+from litellm.types.utils import EmbeddingResponse, Usage
+from litellm.utils import token_counter
from ..common_utils import TritonError
@@ -103,8 +104,36 @@ class TritonEmbeddingConfig(BaseEmbeddingConfig):
model_response.model = raw_response_json.get("model_name", "None")
model_response.data = _embedding_output
+ model_response.usage = self._build_embedding_usage(
+ model=model, request_data=request_data
+ )
return model_response
+ def _build_embedding_usage(self, model: str, request_data: dict) -> Usage:
+ input_data = request_data.get("inputs", [])
+ input_text_values: List[str] = []
+ for item in input_data:
+ if isinstance(item, dict) and item.get("name") == "input_text":
+ data_values = item.get("data", [])
+ if isinstance(data_values, list):
+ input_text_values = [str(value) for value in data_values]
+ break
+
+ prompt_tokens = 0
+ for text in input_text_values:
+ if not text:
+ continue
+ try:
+ prompt_tokens += token_counter(model=model, text=text)
+ except Exception:
+ prompt_tokens += len(text.split())
+
+ return Usage(
+ prompt_tokens=prompt_tokens,
+ completion_tokens=0,
+ total_tokens=prompt_tokens,
+ )
+
def get_error_class(
self, error_message: str, status_code: int, headers: Union[dict, httpx.Headers]
) -> BaseLLMException:
diff --git a/tests/llm_translation/test_triton.py b/tests/llm_translation/test_triton.py
index 8a3bbb4661c..8f3c936dce6 100644
--- a/tests/llm_translation/test_triton.py
+++ b/tests/llm_translation/test_triton.py
@@ -50,6 +50,138 @@ def test_split_embedding_by_shape_fails_with_shape_value_error():
)
+def test_triton_embedding_response_sets_usage_with_token_counter():
+ config = TritonEmbeddingConfig()
+ mock_http_response = MagicMock()
+ mock_http_response.status_code = 200
+ mock_http_response.json.return_value = {
+ "model_name": "gte-base-en-v1",
+ "outputs": [
+ {
+ "name": "embedding",
+ "shape": [1, 2],
+ "data": [0.1, 0.2],
+ }
+ ],
+ }
+ model_response = litellm.EmbeddingResponse()
+ request_data = {
+ "inputs": [
+ {
+ "name": "input_text",
+ "shape": [1],
+ "datatype": "BYTES",
+ "data": ["hello from triton"],
+ }
+ ]
+ }
+
+ with patch(
+ "litellm.llms.triton.embedding.transformation.token_counter",
+ return_value=7,
+ ):
+ transformed = config.transform_embedding_response(
+ model="triton/gte-base-en-v1",
+ raw_response=mock_http_response,
+ model_response=model_response,
+ logging_obj=MagicMock(),
+ request_data=request_data,
+ )
+
+ assert transformed.usage is not None
+ assert transformed.usage.prompt_tokens == 7
+ assert transformed.usage.completion_tokens == 0
+ assert transformed.usage.total_tokens == 7
+
+
+def test_triton_embedding_response_sets_usage_with_word_count_fallback():
+ config = TritonEmbeddingConfig()
+ mock_http_response = MagicMock()
+ mock_http_response.status_code = 200
+ mock_http_response.json.return_value = {
+ "model_name": "gte-base-en-v1",
+ "outputs": [
+ {
+ "name": "embedding",
+ "shape": [1, 2],
+ "data": [0.1, 0.2],
+ }
+ ],
+ }
+ model_response = litellm.EmbeddingResponse()
+ request_data = {
+ "inputs": [
+ {
+ "name": "input_text",
+ "shape": [1],
+ "datatype": "BYTES",
+ "data": ["hello from triton"],
+ }
+ ]
+ }
+
+ with patch(
+ "litellm.llms.triton.embedding.transformation.token_counter",
+ side_effect=Exception("tokenizer error"),
+ ):
+ transformed = config.transform_embedding_response(
+ model="triton/gte-base-en-v1",
+ raw_response=mock_http_response,
+ model_response=model_response,
+ logging_obj=MagicMock(),
+ request_data=request_data,
+ )
+
+ assert transformed.usage is not None
+ assert transformed.usage.prompt_tokens == 3
+ assert transformed.usage.completion_tokens == 0
+ assert transformed.usage.total_tokens == 3
+
+
+def test_triton_embedding_batch_usage_sums_per_input_token_counts():
+ """Batch inputs must not be joined before token counting (avoids extra newline tokens)."""
+ config = TritonEmbeddingConfig()
+ mock_http_response = MagicMock()
+ mock_http_response.status_code = 200
+ mock_http_response.json.return_value = {
+ "model_name": "gte-base-en-v1",
+ "outputs": [
+ {
+ "name": "embedding",
+ "shape": [2, 2],
+ "data": [0.1, 0.2, 0.3, 0.4],
+ }
+ ],
+ }
+ model_response = litellm.EmbeddingResponse()
+ request_data = {
+ "inputs": [
+ {
+ "name": "input_text",
+ "shape": [2],
+ "datatype": "BYTES",
+ "data": ["first input", "second input"],
+ }
+ ]
+ }
+
+ with patch(
+ "litellm.llms.triton.embedding.transformation.token_counter",
+ side_effect=[5, 7],
+ ):
+ transformed = config.transform_embedding_response(
+ model="triton/gte-base-en-v1",
+ raw_response=mock_http_response,
+ model_response=model_response,
+ logging_obj=MagicMock(),
+ request_data=request_data,
+ )
+
+ assert transformed.usage is not None
+ assert transformed.usage.prompt_tokens == 12
+ assert transformed.usage.total_tokens == 12
+
+
@pytest.mark.parametrize("stream", [True, False])
def test_completion_triton_generate_api(stream):
try: