Skip to content

Commit 8ef912a

Browse files
📝 Add docstrings to fix/cache-token-attributes
Docstrings generation was requested by @hayke102. * #3442 (comment) The following files were modified: * `packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py` * `packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/span_utils.py`
1 parent 8b91ed7 commit 8ef912a

File tree

2 files changed

+47
-7
lines changed
  • packages
    • opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic
    • opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain

2 files changed

+47
-7
lines changed

packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,20 @@ async def _aset_token_usage(
185185
token_histogram: Histogram = None,
186186
choice_counter: Counter = None,
187187
):
188+
"""
189+
Record token usage and choice counts from an Anthropic response into the provided span and optional metric instruments.
190+
191+
This function will await coroutine responses and call parse() on wrapped responses when present. It extracts input (prompt + cache read + cache creation) and output token counts from response. If usage data is absent, it falls back to counting prompt or completion tokens via the provided Anthropic client. When metric instruments are provided, it records input/output token values to token_histogram and increments choice_counter with the number of choices. It then sets span attributes for input tokens, output tokens, total tokens, and cache-related input tokens.
192+
193+
Parameters:
194+
span: The tracing span to annotate with token and choice attributes.
195+
anthropic: Anthropic client or compatible object used to count tokens when usage metadata is not available.
196+
request: The original request object used to compute prompt token counts when needed.
197+
response: The response object, which may be a coroutine, a with_raw_response wrapper exposing parse(), or a concrete response containing usage/content/completion attributes.
198+
metric_attributes (dict): Additional attributes to attach to metric recordings.
199+
token_histogram (Histogram): Optional histogram instrument to record token counts.
200+
choice_counter (Counter): Optional counter instrument to record the number of generation choices.
201+
"""
188202
import inspect
189203

190204
# If we get a coroutine, await it
@@ -283,11 +297,11 @@ async def _aset_token_usage(
283297
set_span_attribute(span, SpanAttributes.LLM_USAGE_TOTAL_TOKENS, total_tokens)
284298

285299
set_span_attribute(
286-
span, GenAIAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cache_read_tokens
300+
span, SpanAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cache_read_tokens
287301
)
288302
set_span_attribute(
289303
span,
290-
GenAIAttributes.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
304+
SpanAttributes.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
291305
cache_creation_tokens,
292306
)
293307

@@ -302,6 +316,20 @@ def _set_token_usage(
302316
token_histogram: Histogram = None,
303317
choice_counter: Counter = None,
304318
):
319+
"""
320+
Record token usage metrics and set related span attributes for an Anthropic response.
321+
322+
Extracts input and output token counts from the response when available (or computes them from the request/response using the provided Anthropic client), records input/output token values to token_histogram with metric_attributes, increments choice_counter with the number of generated choices, and sets span attributes for input, output, total, and cache-related token counts. If the response is a coroutine or cannot be parsed, token processing is skipped.
323+
324+
Parameters:
325+
span: The tracing span on which to set token-related attributes.
326+
anthropic: Anthropic client/SDK instance used to compute token counts when response usage data is not present.
327+
request: The original request object used to compute prompt/input tokens if needed.
328+
response: The response object (or a wrapper with a parse() method); coroutine responses are ignored.
329+
metric_attributes (dict): Additional attributes to attach to recorded metrics.
330+
token_histogram (Histogram | None): Histogram instrument for recording token counts; if None, metrics are not recorded.
331+
choice_counter (Counter | None): Counter instrument for recording number of generated choices; if None, choice counts are not recorded.
332+
"""
305333
import inspect
306334

307335
# If we get a coroutine, we cannot process it in sync context
@@ -397,11 +425,11 @@ def _set_token_usage(
397425
set_span_attribute(span, SpanAttributes.LLM_USAGE_TOTAL_TOKENS, total_tokens)
398426

399427
set_span_attribute(
400-
span, GenAIAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cache_read_tokens
428+
span, SpanAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cache_read_tokens
401429
)
402430
set_span_attribute(
403431
span,
404-
GenAIAttributes.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
432+
SpanAttributes.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
405433
cache_creation_tokens,
406434
)
407435

@@ -874,4 +902,4 @@ def _uninstrument(self, **kwargs):
874902
unwrap(
875903
f"{wrap_package}.{wrap_object}",
876904
wrapped_method.get("method"),
877-
)
905+
)

packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/span_utils.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,18 @@ def set_chat_response_usage(
284284
record_token_usage: bool,
285285
model_name: str
286286
) -> None:
287+
"""
288+
Aggregate token usage from an LLM response, record it on the provided span, and optionally record token counts to a histogram.
289+
290+
This function sums input, output, total, and cache-read token counts from response.generations' usage metadata (looking for keys such as "input_tokens"/"prompt_tokens", "output_tokens"/"completion_tokens", and "input_token_details.cache_read"). If any of these counts are greater than zero, the corresponding span attributes are set. If record_token_usage is True, input and output token counts are also recorded in token_histogram with attributes identifying the vendor and response model. Any errors encountered while parsing usage metadata are ignored and do not stop span attribute recording.
291+
292+
Parameters:
293+
span (Span): The span on which to set usage attributes.
294+
response (LLMResult): The LLM response whose generations contain usage metadata.
295+
token_histogram (Histogram): Histogram to record token counts when record_token_usage is True.
296+
record_token_usage (bool): If True, record input/output token counts to token_histogram.
297+
model_name (str): The model name to attach to histogram records as the response model.
298+
"""
287299
input_tokens = 0
288300
output_tokens = 0
289301
total_tokens = 0
@@ -346,7 +358,7 @@ def set_chat_response_usage(
346358
)
347359
_set_span_attribute(
348360
span,
349-
GenAIAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
361+
SpanAttributes.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
350362
cache_read_tokens,
351363
)
352364
if record_token_usage:
@@ -414,4 +426,4 @@ def _set_chat_tool_calls(
414426
span,
415427
f"{tool_call_prefix}.arguments",
416428
json.dumps(tool_args, cls=CallbackFilteredJSONEncoder),
417-
)
429+
)

0 commit comments

Comments
 (0)