Skip to content

Commit 98c4eb2

Browse files
committed
prompt details
1 parent bc3e5b6 commit 98c4eb2

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

python/openinference-instrumentation/src/openinference/instrumentation/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
ImageMessageContent,
2121
Message,
2222
MessageContent,
23+
PromptDetails,
2324
TextMessageContent,
2425
TokenCount,
2526
Tool,
@@ -78,6 +79,7 @@
7879
"ImageMessageContent",
7980
"Message",
8081
"MessageContent",
82+
"PromptDetails",
8183
"TextMessageContent",
8284
"TokenCount",
8385
"Tool",

python/openinference-instrumentation/src/openinference/instrumentation/_attributes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,14 @@ def get_llm_token_count_attributes(
491491
attributes[LLM_TOKEN_COUNT_COMPLETION] = completion
492492
if (total := token_count.get("total")) is not None:
493493
attributes[LLM_TOKEN_COUNT_TOTAL] = total
494+
if (prompt_details := token_count.get("prompt_details")) is not None:
495+
if isinstance(prompt_details, dict):
496+
if (cache_write := prompt_details.get("cache_write")) is not None:
497+
attributes[LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_WRITE] = cache_write
498+
if (cache_read := prompt_details.get("cache_read")) is not None:
499+
attributes[LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_READ] = cache_read
500+
if (audio := prompt_details.get("audio")) is not None:
501+
attributes[LLM_TOKEN_COUNT_PROMPT_DETAILS_AUDIO] = audio
494502
return attributes
495503

496504

@@ -557,6 +565,11 @@ def get_llm_tool_attributes(
557565
LLM_SYSTEM = SpanAttributes.LLM_SYSTEM
558566
LLM_TOKEN_COUNT_COMPLETION = SpanAttributes.LLM_TOKEN_COUNT_COMPLETION
559567
LLM_TOKEN_COUNT_PROMPT = SpanAttributes.LLM_TOKEN_COUNT_PROMPT
568+
LLM_TOKEN_COUNT_PROMPT_DETAILS_AUDIO = SpanAttributes.LLM_TOKEN_COUNT_PROMPT_DETAILS_AUDIO
569+
LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_READ = SpanAttributes.LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_READ
570+
LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_WRITE = (
571+
SpanAttributes.LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_WRITE
572+
)
560573
LLM_TOKEN_COUNT_TOTAL = SpanAttributes.LLM_TOKEN_COUNT_TOTAL
561574
LLM_TOOLS = SpanAttributes.LLM_TOOLS
562575
METADATA = SpanAttributes.METADATA

python/openinference-instrumentation/src/openinference/instrumentation/_types.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,17 @@ class Message(TypedDict, total=False):
6868
tool_calls: "Sequence[ToolCall]"
6969

7070

71+
class PromptDetails(TypedDict, total=False):
72+
audio: int
73+
cache_read: int
74+
cache_write: int
75+
76+
7177
class TokenCount(TypedDict, total=False):
7278
prompt: int
7379
completion: int
7480
total: int
81+
prompt_details: PromptDetails
7582

7683

7784
class Tool(TypedDict, total=False):

python/openinference-instrumentation/tests/test_manual_instrumentation.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
ImageMessageContent,
3838
Message,
3939
OITracer,
40+
PromptDetails,
4041
TextMessageContent,
4142
TokenCount,
4243
Tool,
@@ -2235,7 +2236,16 @@ def test_get_llm_attributes_returns_expected_attributes() -> None:
22352236
contents=[TextMessageContent(type="text", text="Hi there!")],
22362237
)
22372238
]
2238-
token_count: TokenCount = TokenCount(prompt=10, completion=5, total=15)
2239+
token_count: TokenCount = TokenCount(
2240+
prompt=10,
2241+
completion=5,
2242+
total=15,
2243+
prompt_details=PromptDetails(
2244+
audio=3,
2245+
cache_read=2,
2246+
cache_write=1,
2247+
),
2248+
)
22392249
tools: Sequence[Tool] = [
22402250
Tool(
22412251
json_schema=json.dumps({"type": "object", "properties": {"query": {"type": "string"}}})
@@ -2252,7 +2262,6 @@ def test_get_llm_attributes_returns_expected_attributes() -> None:
22522262
token_count=token_count,
22532263
tools=tools,
22542264
)
2255-
22562265
assert attributes.pop(LLM_PROVIDER) == "openai"
22572266
assert attributes.pop(LLM_SYSTEM) == "openai"
22582267
assert attributes.pop(LLM_MODEL_NAME) == "gpt-4"
@@ -2319,6 +2328,9 @@ def test_get_llm_attributes_returns_expected_attributes() -> None:
23192328
== "Hi there!"
23202329
)
23212330
assert attributes.pop(LLM_TOKEN_COUNT_PROMPT) == 10
2331+
assert attributes.pop(LLM_TOKEN_COUNT_PROMPT_DETAILS_AUDIO) == 3
2332+
assert attributes.pop(LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_READ) == 2
2333+
assert attributes.pop(LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_WRITE) == 1
23222334
assert attributes.pop(LLM_TOKEN_COUNT_COMPLETION) == 5
23232335
assert attributes.pop(LLM_TOKEN_COUNT_TOTAL) == 15
23242336
assert (
@@ -2705,6 +2717,11 @@ def example_function( # type: ignore[no-untyped-def]
27052717
LLM_SYSTEM = SpanAttributes.LLM_SYSTEM
27062718
LLM_TOKEN_COUNT_COMPLETION = SpanAttributes.LLM_TOKEN_COUNT_COMPLETION
27072719
LLM_TOKEN_COUNT_PROMPT = SpanAttributes.LLM_TOKEN_COUNT_PROMPT
2720+
LLM_TOKEN_COUNT_PROMPT_DETAILS_AUDIO = SpanAttributes.LLM_TOKEN_COUNT_PROMPT_DETAILS_AUDIO
2721+
LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_READ = SpanAttributes.LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_READ
2722+
LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_WRITE = (
2723+
SpanAttributes.LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_WRITE
2724+
)
27082725
LLM_TOKEN_COUNT_TOTAL = SpanAttributes.LLM_TOKEN_COUNT_TOTAL
27092726
LLM_TOOLS = SpanAttributes.LLM_TOOLS
27102727
OPENINFERENCE_SPAN_KIND = SpanAttributes.OPENINFERENCE_SPAN_KIND

0 commit comments

Comments
 (0)