UNPKG

@anthropic-ai/sdk

Version:
44 lines • 2.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.INPUT_TOKEN_COUNTS = void 0; exports.setUsageSumAttributes = setUsageSumAttributes; const values_1 = require("../../../internal/utils/values.js"); const attributes_1 = require("./attributes.js"); /** * Sets the attributes that are sums over `usage`, the usage of a Message as the API sent it. * `setResponseAttributes` sets the attributes that one field gives. The compactions' tokens are final * only from the `message_delta` of a streamed Message on, and are set only when `complete` is true. */ function setUsageSumAttributes(span, usage, complete) { if (!(0, values_1.isObj)(usage)) { return; } if (typeof usage['input_tokens'] === 'number') { span.setAttribute(attributes_1.ATTR_GEN_AI_USAGE_INPUT_TOKENS, inputTokens(usage)); } if (!complete) { return; } // The usage above leaves out what the compactions used, which only the iterations have. const iterations = Array.isArray(usage['iterations']) ? usage['iterations'] : []; const compactions = iterations.filter((each) => (0, values_1.isObj)(each) && each['type'] === 'compaction'); if (compactions.length > 0) { span.setAttribute(attributes_1.ATTR_ANTHROPIC_USAGE_COMPACTION_INPUT_TOKENS, compactions.reduce((sum, each) => sum + inputTokens(each), 0)); span.setAttribute(attributes_1.ATTR_ANTHROPIC_USAGE_COMPACTION_OUTPUT_TOKENS, compactions.reduce((sum, each) => sum + count(each, 'output_tokens'), 0)); } } /** The token counts of a usage, or of one of its iterations, that add up to its input tokens. */ exports.INPUT_TOKEN_COUNTS = [ 'input_tokens', 'cache_read_input_tokens', 'cache_creation_input_tokens', ]; function inputTokens(usage) { return exports.INPUT_TOKEN_COUNTS.reduce((sum, name) => sum + count(usage, name), 0); } /** The token count `name` of `usage`, or 0 when it has none. */ function count(usage, name) { const value = usage[name]; return typeof value === 'number' ? value : 0; } //# sourceMappingURL=response.js.map