Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from openlit.instrumentation.gradient.gradient import _resolve_endpoint
from openlit.instrumentation.gradient.utils import (
_new_scope,
apply_cache_usage,
common_chat_logic,
process_chat_response,
process_chunk,
Expand Down Expand Up @@ -65,6 +66,8 @@ def __init__(
self._input_tokens = 0
self._output_tokens = 0
self._reasoning_tokens = 0
self._cache_read_input_tokens = 0
self._cache_creation_input_tokens = 0
self._start_time = time.time()
self._end_time = None
self._finalized = False
Expand Down Expand Up @@ -287,6 +290,7 @@ async def wrapper(wrapped, instance, args, kwargs):
usage = response_dict.get("usage") or {}
scope._input_tokens = usage.get("prompt_tokens", 0) or 0
scope._output_tokens = usage.get("completion_tokens", 0) or 0
apply_cache_usage(scope, usage)
common_chat_logic(
scope,
pricing_info,
Expand Down
4 changes: 4 additions & 0 deletions sdk/python/src/openlit/instrumentation/gradient/gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
)
from openlit.instrumentation.gradient.utils import (
_new_scope,
apply_cache_usage,
common_chat_logic,
process_chat_response,
process_chunk,
Expand Down Expand Up @@ -103,6 +104,8 @@ def __init__(
self._input_tokens = 0
self._output_tokens = 0
self._reasoning_tokens = 0
self._cache_read_input_tokens = 0
self._cache_creation_input_tokens = 0
self._start_time = time.time()
self._end_time = None
self._finalized = False
Expand Down Expand Up @@ -323,6 +326,7 @@ def wrapper(wrapped, instance, args, kwargs):
usage = response_dict.get("usage") or {}
scope._input_tokens = usage.get("prompt_tokens", 0) or 0
scope._output_tokens = usage.get("completion_tokens", 0) or 0
apply_cache_usage(scope, usage)
common_chat_logic(
scope,
pricing_info,
Expand Down
51 changes: 50 additions & 1 deletion sdk/python/src/openlit/instrumentation/gradient/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ def emit_inference_event(
attributes[SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS] = value
elif key == "output_tokens":
attributes[SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS] = value
elif key == "cache_read_input_tokens":
attributes[SemanticConvention.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] = (
value
)
elif key == "cache_creation_input_tokens":
attributes[
SemanticConvention.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS
] = value
elif key == "error_type":
attributes[SemanticConvention.ERROR_TYPE] = value
elif key == "system_instructions":
Expand All @@ -324,6 +332,23 @@ def emit_inference_event(
logger.warning("Failed to emit inference event: %s", exc, exc_info=True)


def apply_cache_usage(scope, usage):
"""Read prompt-cache counts from a chat-completions `usage` payload."""
reported = [
count
for count in (
usage.get("cache_read_input_tokens"),
(usage.get("prompt_tokens_details") or {}).get("cached_tokens"),
)
if count is not None
]
if reported:
scope._cache_read_input_tokens = next((count for count in reported if count), 0)
created = usage.get("cache_created_input_tokens")
if created is not None:
scope._cache_creation_input_tokens = created


def _normalize_stop(stop):
if stop is None:
return []
Expand Down Expand Up @@ -395,6 +420,7 @@ def process_chunk(scope, chunk):
if usage:
scope._input_tokens = usage.get("prompt_tokens") or scope._input_tokens
scope._output_tokens = usage.get("completion_tokens") or scope._output_tokens
apply_cache_usage(scope, usage)


def process_response_chunk(scope, chunk):
Expand Down Expand Up @@ -462,7 +488,13 @@ def common_chat_logic(
scope._output_tokens = general_tokens(scope._llmresponse or "")

cost = get_chat_model_cost(
request_model, pricing_info, scope._input_tokens, scope._output_tokens
request_model,
pricing_info,
scope._input_tokens,
scope._output_tokens,
cache_read_tokens=getattr(scope, "_cache_read_input_tokens", 0) or 0,
cache_creation_tokens=getattr(scope, "_cache_creation_input_tokens", 0) or 0,
prompt_tokens_include_cache=True,
)

common_span_attributes(
Expand Down Expand Up @@ -576,6 +608,14 @@ def common_chat_logic(
scope._input_tokens + scope._output_tokens,
)
scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
scope._span.set_attribute(
SemanticConvention.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
getattr(scope, "_cache_read_input_tokens", 0) or 0,
)
scope._span.set_attribute(
SemanticConvention.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
getattr(scope, "_cache_creation_input_tokens", 0) or 0,
)
if getattr(scope, "_reasoning_tokens", 0):
scope._span.set_attribute(
SemanticConvention.GEN_AI_USAGE_REASONING_TOKENS, scope._reasoning_tokens
Expand Down Expand Up @@ -654,6 +694,12 @@ def common_chat_logic(
"top_p": body.get("top_p"),
"input_tokens": scope._input_tokens,
"output_tokens": scope._output_tokens,
"cache_read_input_tokens": getattr(scope, "_cache_read_input_tokens", 0)
or None,
"cache_creation_input_tokens": getattr(
scope, "_cache_creation_input_tokens", 0
)
or None,
**version_extras,
}
if capture_message_content and system_instr:
Expand Down Expand Up @@ -711,6 +757,8 @@ def _new_scope(body, span, start_time, server_address, server_port, response_dic
scope._input_tokens = 0
scope._output_tokens = 0
scope._reasoning_tokens = 0
scope._cache_read_input_tokens = 0
scope._cache_creation_input_tokens = 0
if response_dict is not None:
scope._response_id = response_dict.get("id", "") or ""
scope._response_model = response_dict.get("model", "") or ""
Expand Down Expand Up @@ -757,6 +805,7 @@ def process_chat_response(
usage = response_dict.get("usage") or {}
scope._input_tokens = usage.get("prompt_tokens", 0) or 0
scope._output_tokens = usage.get("completion_tokens", 0) or 0
apply_cache_usage(scope, usage)

common_chat_logic(
scope,
Expand Down
Loading
Loading