metergraph 0.6.17 → 0.6.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -302,4 +302,6 @@ calls made outside these supported paths.
302
302
 
303
303
  Calls record `service_tier` separately from `requested_service_tier`: requesting Flex does not prove it was served. Capture reads OpenAI's response tier, Anthropic's usage tier, and Google's usage tier or traffic type, retaining evidence across trailing stream events. Successful Batch API results record `batch` from the transport, without inventing a requested tier. Missing response evidence stays unknown.
304
304
 
305
+ Interrupted Anthropic streams retain available input/cache usage and message IDs, plus `http_request_id` when the SDK exposes it. `usage_complete: false` marks observed usage as incomplete; it does not establish the final charge or mean the request was free.
306
+
305
307
  Search usage is retained independently of text capture: OpenAI search-call counts, Anthropic server-tool usage, and Google grounding-query counts. Perplexity Responses capture uses invocation counters, with search-result items as a fallback. Clients using `https://api.perplexity.ai` are detected automatically; use `wrap(client, { provider: "perplexity" })` when a gateway hides the provider. Perplexity Agent and legacy Sonar calls are identified separately. Agent search type is recorded when explicitly set on the tool; missing evidence remains unknown. Prices are resolved by the receiver.
package/dist/capture.d.ts CHANGED
@@ -45,6 +45,7 @@ export declare class CaptureRuntime {
45
45
  responseText?: string;
46
46
  responseChunks?: unknown[];
47
47
  evidenceChunks?: unknown[];
48
+ httpRequestId?: string;
48
49
  }): void;
49
50
  }
50
51
  export {};
package/dist/capture.js CHANGED
@@ -81,7 +81,7 @@ function usageValue(value) {
81
81
  const aiInputDetails = get(value, "inputTokenDetails");
82
82
  const aiOutputDetails = get(value, "outputTokenDetails");
83
83
  const cacheCreation = get(value, "cache_creation") ?? get(value, "cacheCreation");
84
- return {
84
+ const counts = {
85
85
  input_tokens: number(get(value, "prompt_tokens") ?? get(value, "input_tokens")
86
86
  ?? get(value, "promptTokenCount") ?? get(aiInput, "total") ?? aiInput),
87
87
  output_tokens: number(get(value, "completion_tokens") ?? get(value, "output_tokens")
@@ -101,6 +101,14 @@ function usageValue(value) {
101
101
  ?? get(value, "thoughtsTokenCount") ?? get(aiOutput, "reasoning")
102
102
  ?? get(aiOutputDetails, "reasoningTokens")),
103
103
  };
104
+ // Native Gemini reports tool context and thinking outside its prompt/candidate counts.
105
+ if (number(get(value, "promptTokenCount")) !== undefined) {
106
+ counts.input_tokens = number(get(value, "promptTokenCount")) + (number(get(value, "toolUsePromptTokenCount")) ?? 0);
107
+ }
108
+ if (number(get(value, "candidatesTokenCount")) !== undefined) {
109
+ counts.output_tokens = number(get(value, "candidatesTokenCount")) + (number(get(value, "thoughtsTokenCount")) ?? 0);
110
+ }
111
+ return counts;
104
112
  }
105
113
  function usage(response) {
106
114
  const value = get(response, "usage") ?? get(response, "usage_metadata")
@@ -846,9 +854,13 @@ class CaptureRuntime {
846
854
  tool_calls: fullTools ? persistedTools : toolNames(state.request),
847
855
  tool_names: observedToolNames?.length ? observedToolNames : undefined,
848
856
  endpoint: state.endpoint,
857
+ http_request_id: extra.httpRequestId ?? get(extra.error, "request_id"),
858
+ usage_complete: extra.stream === true
859
+ ? extra.error === undefined && extra.status !== "error" && extra.status !== "abandoned"
860
+ : undefined,
849
861
  request_id: get(response, "_request_id") ?? get(response, "response_id")
850
862
  ?? get(response, "responseId") ?? get(response, "id")
851
- ?? get(get(response, "response"), "id"),
863
+ ?? get(get(response, "response"), "id") ?? get(extra.error, "request_id"),
852
864
  batch: state.request.batch === true,
853
865
  batch_custom_id: state.request.batch_custom_id,
854
866
  // Explicit false is the sensitive-operation opt-out; hosted ingestion
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "0.6.17";
1
+ export declare const SDK_VERSION = "0.6.19";
package/dist/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SDK_VERSION = void 0;
4
- exports.SDK_VERSION = "0.6.17";
4
+ exports.SDK_VERSION = "0.6.19";
package/dist/wrap.js CHANGED
@@ -229,6 +229,7 @@ function streamProxy(stream, state, capture, suppressUsageOnly) {
229
229
  let ttftMs;
230
230
  const parts = [];
231
231
  const chunks = [];
232
+ let helperSnapshot;
232
233
  const hasOutput = (chunk) => {
233
234
  if ((0, capture_js_1.chunkText)(chunk))
234
235
  return true;
@@ -264,7 +265,18 @@ function streamProxy(stream, state, capture, suppressUsageOnly) {
264
265
  return chunk;
265
266
  };
266
267
  const finish = (response = last, error, status) => {
268
+ let httpRequestId;
269
+ if (state.provider === "anthropic") {
270
+ try {
271
+ if (error || status === "abandoned")
272
+ response = stream.currentMessage ?? helperSnapshot ?? response;
273
+ if (typeof stream.request_id === "string")
274
+ httpRequestId = stream.request_id;
275
+ }
276
+ catch { /* Native stream getters must not affect request behavior. */ }
277
+ }
267
278
  finishCapture(capture, state, response, {
279
+ httpRequestId,
268
280
  error,
269
281
  status,
270
282
  stream: true,
@@ -274,6 +286,14 @@ function streamProxy(stream, state, capture, suppressUsageOnly) {
274
286
  });
275
287
  };
276
288
  if (typeof stream.on === "function") {
289
+ if (state.provider === "anthropic") {
290
+ stream.on("streamEvent", (_event, snapshot) => {
291
+ // Helpers consume the stream internally and may clear their snapshot
292
+ // before finalMessage() rejects. Keep its latest native usage evidence.
293
+ if (snapshot != null)
294
+ helperSnapshot = snapshot;
295
+ });
296
+ }
277
297
  stream.on("error", (error) => finish(last, error));
278
298
  }
279
299
  return new Proxy(stream, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metergraph",
3
- "version": "0.6.17",
3
+ "version": "0.6.19",
4
4
  "description": "Zero-dependency TypeScript SDK for function-level LLM cost attribution across major LLM providers and frameworks, including OpenAI, Anthropic, Gemini, and Vercel AI SDK.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",