risicare 0.9.2 → 0.11.0

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.
Files changed (73) hide show
  1. package/CHANGELOG.md +254 -0
  2. package/README.md +194 -58
  3. package/dist/frameworks/instructor.cjs +383 -117
  4. package/dist/frameworks/instructor.cjs.map +1 -1
  5. package/dist/frameworks/instructor.js +383 -117
  6. package/dist/frameworks/instructor.js.map +1 -1
  7. package/dist/frameworks/langchain.cjs +396 -130
  8. package/dist/frameworks/langchain.cjs.map +1 -1
  9. package/dist/frameworks/langchain.js +396 -130
  10. package/dist/frameworks/langchain.js.map +1 -1
  11. package/dist/frameworks/langgraph.cjs +396 -130
  12. package/dist/frameworks/langgraph.cjs.map +1 -1
  13. package/dist/frameworks/langgraph.js +396 -130
  14. package/dist/frameworks/langgraph.js.map +1 -1
  15. package/dist/frameworks/llamaindex.cjs +396 -130
  16. package/dist/frameworks/llamaindex.cjs.map +1 -1
  17. package/dist/frameworks/llamaindex.js +396 -130
  18. package/dist/frameworks/llamaindex.js.map +1 -1
  19. package/dist/index.cjs +1136 -240
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.d.cts +151 -26
  22. package/dist/index.d.ts +151 -26
  23. package/dist/index.js +1132 -240
  24. package/dist/index.js.map +1 -1
  25. package/dist/providers/anthropic/index.cjs +467 -137
  26. package/dist/providers/anthropic/index.cjs.map +1 -1
  27. package/dist/providers/anthropic/index.js +467 -137
  28. package/dist/providers/anthropic/index.js.map +1 -1
  29. package/dist/providers/bedrock/index.cjs +409 -128
  30. package/dist/providers/bedrock/index.cjs.map +1 -1
  31. package/dist/providers/bedrock/index.js +409 -128
  32. package/dist/providers/bedrock/index.js.map +1 -1
  33. package/dist/providers/cerebras/index.cjs +414 -133
  34. package/dist/providers/cerebras/index.cjs.map +1 -1
  35. package/dist/providers/cerebras/index.js +414 -133
  36. package/dist/providers/cerebras/index.js.map +1 -1
  37. package/dist/providers/cohere/index.cjs +414 -133
  38. package/dist/providers/cohere/index.cjs.map +1 -1
  39. package/dist/providers/cohere/index.js +414 -133
  40. package/dist/providers/cohere/index.js.map +1 -1
  41. package/dist/providers/google/index.cjs +414 -133
  42. package/dist/providers/google/index.cjs.map +1 -1
  43. package/dist/providers/google/index.js +414 -133
  44. package/dist/providers/google/index.js.map +1 -1
  45. package/dist/providers/groq/index.cjs +414 -133
  46. package/dist/providers/groq/index.cjs.map +1 -1
  47. package/dist/providers/groq/index.js +414 -133
  48. package/dist/providers/groq/index.js.map +1 -1
  49. package/dist/providers/huggingface/index.cjs +414 -133
  50. package/dist/providers/huggingface/index.cjs.map +1 -1
  51. package/dist/providers/huggingface/index.js +414 -133
  52. package/dist/providers/huggingface/index.js.map +1 -1
  53. package/dist/providers/mistral/index.cjs +414 -133
  54. package/dist/providers/mistral/index.cjs.map +1 -1
  55. package/dist/providers/mistral/index.js +414 -133
  56. package/dist/providers/mistral/index.js.map +1 -1
  57. package/dist/providers/ollama/index.cjs +414 -133
  58. package/dist/providers/ollama/index.cjs.map +1 -1
  59. package/dist/providers/ollama/index.js +414 -133
  60. package/dist/providers/ollama/index.js.map +1 -1
  61. package/dist/providers/openai/index.cjs +452 -135
  62. package/dist/providers/openai/index.cjs.map +1 -1
  63. package/dist/providers/openai/index.js +452 -135
  64. package/dist/providers/openai/index.js.map +1 -1
  65. package/dist/providers/together/index.cjs +414 -133
  66. package/dist/providers/together/index.cjs.map +1 -1
  67. package/dist/providers/together/index.js +414 -133
  68. package/dist/providers/together/index.js.map +1 -1
  69. package/dist/providers/vercel-ai/index.cjs +398 -132
  70. package/dist/providers/vercel-ai/index.cjs.map +1 -1
  71. package/dist/providers/vercel-ai/index.js +398 -132
  72. package/dist/providers/vercel-ai/index.js.map +1 -1
  73. package/package.json +1 -1
@@ -37,6 +37,35 @@ module.exports = __toCommonJS(anthropic_exports);
37
37
  // src/client.ts
38
38
  var import_node_crypto3 = require("crypto");
39
39
 
40
+ // src/exporters/retry.ts
41
+ var RETRY_STATUSES = /* @__PURE__ */ new Set([408, 429, 500, 502, 503, 504]);
42
+ var TOO_LARGE_STATUS = 413;
43
+ var RETRY_BACKOFF_BASE_S = 1;
44
+ var RETRY_BACKOFF_CAP_S = 30;
45
+ var DEFAULT_RETRY_HORIZON_S = 300;
46
+ var DEFAULT_SHUTDOWN_TIMEOUT_S = 10;
47
+ var RETRY_WARN_AFTER_ATTEMPTS = 3;
48
+ var jitter = { fraction: () => Math.random() };
49
+ function backoffDelayS(retryNumber, fraction) {
50
+ const exponent = Math.min(Math.max(retryNumber, 0), 62);
51
+ const ceiling = Math.min(RETRY_BACKOFF_CAP_S, RETRY_BACKOFF_BASE_S * 2 ** exponent);
52
+ return Math.min(Math.max(fraction, 0), 1) * ceiling;
53
+ }
54
+ var SECONDS = /^[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/;
55
+ function parseRetryAfterS(value, nowMs = Date.now()) {
56
+ if (value === null || value === void 0) return null;
57
+ const text = String(value).trim();
58
+ if (text === "") return null;
59
+ if (SECONDS.test(text)) {
60
+ const seconds = Number(text);
61
+ return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
62
+ }
63
+ if (!/[A-Za-z]/.test(text)) return null;
64
+ const when = Date.parse(text);
65
+ if (!Number.isFinite(when)) return null;
66
+ return Math.max(0, (when - nowMs) / 1e3);
67
+ }
68
+
40
69
  // src/globals.ts
41
70
  var import_node_async_hooks = require("async_hooks");
42
71
  var G = globalThis;
@@ -160,11 +189,70 @@ function hasCredentials(url) {
160
189
  }
161
190
  }
162
191
 
192
+ // src/masking.ts
193
+ var CONTENT_IN_JSON_OBJECT = /* @__PURE__ */ new Map([
194
+ [
195
+ "llm.invocation_parameters",
196
+ /* @__PURE__ */ new Set([
197
+ "additionalinstructions",
198
+ // OpenAI assistants run
199
+ "chathistory",
200
+ // Cohere
201
+ "contents",
202
+ // Gemini
203
+ "documents",
204
+ // Cohere RAG
205
+ "functions",
206
+ "input",
207
+ "inputtext",
208
+ // Bedrock Titan
209
+ "instructions",
210
+ "message",
211
+ // Cohere
212
+ "messages",
213
+ "preamble",
214
+ // Cohere
215
+ "prediction",
216
+ // OpenAI predicted output
217
+ "prompt",
218
+ "system",
219
+ // Anthropic
220
+ "systeminstruction",
221
+ // Gemini
222
+ "texts",
223
+ // Cohere embed
224
+ "toolconfig",
225
+ // Gemini
226
+ "tools"
227
+ ])
228
+ ]
229
+ ]);
230
+ var VALUE_RULE_PREFIXES = new Set(
231
+ [...CONTENT_IN_JSON_OBJECT.keys()].flatMap((key) => {
232
+ const segments = key.split(".");
233
+ return segments.slice(1).map((_, i) => segments.slice(0, i + 1).join("."));
234
+ })
235
+ );
236
+
163
237
  // src/exporters/batch.ts
238
+ var SHUTDOWN_BUDGET_MS = DEFAULT_SHUTDOWN_TIMEOUT_S * 1e3;
164
239
  var MAX_TIMEOUT_MS = 2 ** 31 - 1;
165
240
  var MAX_BATCH_BYTES = 8 * 1024 * 1024;
166
241
  var utf8ByteLength = typeof Buffer !== "undefined" && typeof Buffer.byteLength === "function" ? (s) => Buffer.byteLength(s, "utf8") : (s) => s.length * 3;
167
242
 
243
+ // src/context/storage.ts
244
+ function storage() {
245
+ return getContextStorage();
246
+ }
247
+ function getContext() {
248
+ return storage().getStore() ?? {};
249
+ }
250
+ function runWithContext(overrides, fn) {
251
+ const parent = getContext();
252
+ const merged = { ...parent, ...overrides };
253
+ return storage().run(merged, fn);
254
+ }
255
+
168
256
  // src/ids.ts
169
257
  var import_node_crypto = require("crypto");
170
258
 
@@ -469,19 +557,6 @@ function classifyError(error) {
469
557
  return CANONICAL_ERROR_CODES.has(GENERIC_FALLBACK) ? GENERIC_FALLBACK : "";
470
558
  }
471
559
 
472
- // src/context/storage.ts
473
- function storage() {
474
- return getContextStorage();
475
- }
476
- function getContext() {
477
- return storage().getStore() ?? {};
478
- }
479
- function runWithContext(overrides, fn) {
480
- const parent = getContext();
481
- const merged = { ...parent, ...overrides };
482
- return storage().run(merged, fn);
483
- }
484
-
485
560
  // src/tracer.ts
486
561
  function attachErrorCode(span, error) {
487
562
  try {
@@ -496,7 +571,7 @@ function attachErrorCode(span, error) {
496
571
  }
497
572
 
498
573
  // src/version.ts
499
- var SDK_VERSION = "0.9.2";
574
+ var SDK_VERSION = "0.11.0";
500
575
 
501
576
  // src/exporters/quota.ts
502
577
  var QUOTA_REASON = "monthly_quota_exceeded";
@@ -604,12 +679,18 @@ var QuotaPause = class {
604
679
 
605
680
  // src/exporters/http.ts
606
681
  var CAPACITY_REFUSAL_STATUSES = /* @__PURE__ */ new Set([429, 503]);
607
- var RETRY_AFTER_CEILING_MS = 1e4;
608
- var REFUSAL_BACKOFF_BASE_MS = 1e3;
609
- var REFUSAL_NO_HINT = -1;
610
682
  var MISROUTE_WARN_WINDOW_MS = 3e5;
611
683
  var AUTH_REFUSAL_STATUSES = /* @__PURE__ */ new Set([401, 403]);
612
684
  var FAILURE_WARN_WINDOW_MS = 3e5;
685
+ var MAX_TIMER_MS = 2 ** 31 - 1;
686
+ function validHorizonS(value) {
687
+ if (value === void 0) return DEFAULT_RETRY_HORIZON_S;
688
+ if (typeof value === "number" && Number.isFinite(value) && value >= 0) return value;
689
+ warn(
690
+ `Risicare: retryHorizonS=${String(value)} is not a finite number of 0 or more; using the default, ${DEFAULT_RETRY_HORIZON_S} s.`
691
+ );
692
+ return DEFAULT_RETRY_HORIZON_S;
693
+ }
613
694
  var HttpExporter = class {
614
695
  name = "http";
615
696
  _endpoint;
@@ -619,20 +700,28 @@ var HttpExporter = class {
619
700
  _timeoutMs;
620
701
  _maxRetries;
621
702
  _compress;
703
+ _retryHorizonS;
704
+ /** Total HTTP requests that this exporter sent (each retry and each half of a split counts). */
705
+ attempts = 0;
622
706
  // Circuit breaker (CON-201 / B-16). Threshold and cooldown sized for
623
- // a "30-second prod restart" scenario: with default 1s batch flush a
624
- // 5-failure threshold opens the circuit ~5s into an outage and the 60s
625
- // cooldown gives the gateway time to come back before the half-open
626
- // probe fires. Lower numbers (was 3@30s) tripped on transient blips.
707
+ // a "30-second prod restart" scenario. Since W2-1c only answers that a retry
708
+ // cannot change move it (see the block above).
627
709
  _consecutiveFailures = 0;
628
710
  _lastMisrouteWarnAt = -Infinity;
629
711
  _lastFailureWarnAt = -Infinity;
712
+ _lastExhaustedWarnAt = -Infinity;
713
+ /** "log once" (W2-1c): the WARNING for a `too_large` drop is written one time. */
714
+ _tooLargeWarned = false;
630
715
  /**
631
716
  * The kind of failure that the last outage WARNING named ('auth', 'http', 'none',
632
717
  * 'unknown'). `null` after a success. A new kind in the same outage writes one more
633
718
  * WARNING, also inside the window.
634
719
  */
635
720
  _failureKind = null;
721
+ /** `endRetries()` was called, and `shutdown()` has not run since. */
722
+ _noWait = false;
723
+ /** The wake-ups of the backoff waits in progress. `endRetries()` calls each one. */
724
+ _waits = /* @__PURE__ */ new Set();
636
725
  /** The endpoint as a log line may show it: no userinfo, no query (see `safeUrl`). */
637
726
  _safeEndpoint;
638
727
  /** The endpoint holds userinfo. `fetch` refuses such a URL. */
@@ -643,6 +732,8 @@ var HttpExporter = class {
643
732
  _acks = /* @__PURE__ */ new WeakMap();
644
733
  /** The requests that each finished call sent, by its batch. Read once by `takeAttempts(spans)`. */
645
734
  _requests = /* @__PURE__ */ new WeakMap();
735
+ /** The drop reason of each failed call, by its batch. Read once by `takeDropReason(spans)`. */
736
+ _dropReasons = /* @__PURE__ */ new WeakMap();
646
737
  /** The batch processor dropped span(s) from a full queue in the quota pause (S-1). */
647
738
  noteQuotaDrop(count = 1) {
648
739
  this._quota.addDrops(count);
@@ -667,8 +758,16 @@ var HttpExporter = class {
667
758
  this._projectId = options.projectId;
668
759
  this._environment = options.environment;
669
760
  this._timeoutMs = options.timeoutMs ?? 5e3;
670
- this._maxRetries = options.maxRetries ?? 3;
761
+ let maxRetries = options.maxRetries;
762
+ if (maxRetries !== void 0 && !(Number.isInteger(maxRetries) && maxRetries >= 1)) {
763
+ warn(
764
+ `Risicare: maxRetries=${String(maxRetries)} is not a whole number of 1 or more; the exporter uses no count limit (the retry horizon is the limit).`
765
+ );
766
+ maxRetries = void 0;
767
+ }
768
+ this._maxRetries = maxRetries;
671
769
  this._compress = options.compress ?? false;
770
+ this._retryHorizonS = validHorizonS(options.retryHorizonS);
672
771
  this._safeEndpoint = safeUrl(this._endpoint);
673
772
  this._endpointHasCredentials = hasCredentials(this._endpoint);
674
773
  }
@@ -690,16 +789,18 @@ var HttpExporter = class {
690
789
  [/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")]() {
691
790
  return `HttpExporter { endpoint: '${this._safeEndpoint}' }`;
692
791
  }
693
- /** The most requests that one `export()` call sends (`maxRetries`). */
792
+ /**
793
+ * The most attempts for one request body (`maxRetries`). `undefined` when no count is set:
794
+ * the retry horizon is the limit.
795
+ */
694
796
  get attemptsPerExport() {
695
797
  return this._maxRetries;
696
798
  }
697
799
  /**
698
800
  * Returns the number of HTTP requests that the `export()` call for `spans` sent, and clears
699
- * it. This is the number that was sent, not the most that the call may send
700
- * (`attemptsPerExport`): a 401 or 403 stops the loop after one request, and an open circuit
701
- * breaker sends none. Returns `null` when no call is on record for `spans`. The answer is
702
- * kept per batch, as `takeAck` keeps the ack.
801
+ * it. Each retry and each half of a 413 split counts one; a call that the open circuit
802
+ * breaker or the quota pause stops sends none. Returns `null` when no call is on record for
803
+ * `spans`. The answer is kept per batch, as `takeAck` keeps the ack.
703
804
  */
704
805
  takeAttempts(spans) {
705
806
  const n = this._requests.get(spans) ?? null;
@@ -716,6 +817,12 @@ var HttpExporter = class {
716
817
  this._acks.delete(spans);
717
818
  return ack;
718
819
  }
820
+ /** Returns why the `export()` call for `spans` failed (W2-1c), and clears it. See `SpanExporter`. */
821
+ takeDropReason(spans) {
822
+ const reason = this._dropReasons.get(spans) ?? null;
823
+ this._dropReasons.delete(spans);
824
+ return reason;
825
+ }
719
826
  /** Returns the spans of `spans` that could not be serialized and were not sent, and clears them. */
720
827
  takeUnserializable(spans) {
721
828
  const bad = this._unserializable.get(spans) ?? [];
@@ -724,7 +831,7 @@ var HttpExporter = class {
724
831
  }
725
832
  /**
726
833
  * Cancel the `export()` call for `spans` (all calls when `spans` is not given): its
727
- * request, its retries and its backoff sleep. The batch processor calls it when
834
+ * request, its retries and its backoff wait. The batch processor calls it when
728
835
  * `shutdown()` abandons the batch, so that the call does not keep the process alive. The
729
836
  * cancelled call returns FAILURE, changes no counter (the circuit breaker included) and
730
837
  * logs no WARNING.
@@ -734,24 +841,39 @@ var HttpExporter = class {
734
841
  else for (const call of this._calls.values()) call.cancel.abort();
735
842
  }
736
843
  /**
737
- * Send one batch. The limits are the request timeout and `maxRetries`.
844
+ * End the retry waits of this exporter until its next `shutdown()`. The batch processor
845
+ * calls it when `shutdown()` starts. A call that waits for a retry makes one more attempt
846
+ * at once. An attempt that fails after this is not retried: the call fails with the drop
847
+ * reason `shutdown_residue`. So a drain against a dead endpoint costs one attempt for each
848
+ * batch, not the shutdown budget.
849
+ */
850
+ endRetries() {
851
+ this._noWait = true;
852
+ for (const wake of [...this._waits]) wake();
853
+ }
854
+ /**
855
+ * Send one batch, with the W2-1c retry policy (see the block above the class).
856
+ *
857
+ * Resolves SUCCESS when the backend accounted for the batch: `takeAck` has the counts (and
858
+ * the spans that a 413 split dropped, in `dropped`). Resolves FAILURE when no span was
859
+ * accounted for: `takeDropReason` then says why, and the batch processor drops the batch
860
+ * with that reason.
738
861
  *
739
862
  * ⚠ NO CALLER BUDGET. `flush(timeoutMs)` only stops WAITING at its deadline; it never
740
- * cuts a request that is already sent. A short flush budget that cut the request made a
741
- * healthy, slow server look like a failure: the span used a retry, the breaker moved, and
742
- * the server stored the same spans again. Only `abort()` stops the call, and only
863
+ * cuts a request that is already sent. Only `abort()` stops the call, and only
743
864
  * `shutdown()` calls it.
744
865
  */
745
866
  async export(spans) {
746
867
  if (spans.length === 0) return "success" /* SUCCESS */;
747
868
  const call = {
748
869
  cancel: new AbortController(),
749
- refusalAfterMs: null,
870
+ retryAfterS: null,
750
871
  misroute: null,
751
872
  noRetry: false,
752
873
  status: null,
753
874
  error: null,
754
- attempts: 0
875
+ attempts: 0,
876
+ ack: null
755
877
  };
756
878
  this._calls.set(spans, call);
757
879
  try {
@@ -788,83 +910,236 @@ var HttpExporter = class {
788
910
  );
789
911
  }
790
912
  if (parts.length === 0) return "success" /* SUCCESS */;
791
- const payload = `{"spans":[${parts.join(",")}]` + (this._projectId ? `,"projectId":${JSON.stringify(this._projectId)}` : "") + (this._environment ? `,"environment":${JSON.stringify(this._environment)}` : "") + "}";
792
- const now = Date.now();
793
- let isHalfOpen = false;
794
- if (this._consecutiveFailures >= this._circuitBreakerThreshold) {
795
- if (now < this._circuitOpenUntil) {
796
- return "failure" /* FAILURE */;
913
+ if (this._consecutiveFailures >= this._circuitBreakerThreshold && Date.now() < this._circuitOpenUntil) {
914
+ this._dropReasons.set(spans, "transport_refused");
915
+ return "failure" /* FAILURE */;
916
+ }
917
+ const out = { delivered: [], dropped: {}, failed: [], cancelled: false };
918
+ const start = performance.now();
919
+ await this._deliver(parts, start, call, out, true);
920
+ if (cancelled.aborted || out.cancelled) {
921
+ this._acks.delete(spans);
922
+ return "failure" /* FAILURE */;
923
+ }
924
+ return this._finish(spans, out, call.attempts);
925
+ }
926
+ /** The request body for `parts` (the serialized spans of one part of a batch). */
927
+ _payload(parts) {
928
+ return `{"spans":[${parts.join(",")}]` + (this._projectId ? `,"projectId":${JSON.stringify(this._projectId)}` : "") + (this._environment ? `,"environment":${JSON.stringify(this._environment)}` : "") + "}";
929
+ }
930
+ /**
931
+ * Send one part of a batch with the retry policy, and record its outcome in `out`.
932
+ * `maySplit` is true only for the whole batch: a 413 splits it in two one time. A half
933
+ * that gets 413 again is dropped as `too_large`.
934
+ */
935
+ async _deliver(parts, start, call, out, maySplit) {
936
+ const n = parts.length;
937
+ if (!maySplit && this._quota.paused()) {
938
+ out.delivered.push({ n, ack: { accepted: 0, rejected: 0, quotaDropped: n } });
939
+ return;
940
+ }
941
+ const result = await this._sendWithRetries(this._payload(parts), n, start, call);
942
+ if (result.kind === "delivered") {
943
+ out.delivered.push({ n, ack: result.ack });
944
+ } else if (result.kind === "too_large") {
945
+ if (maySplit && n > 1) {
946
+ const mid = Math.ceil(n / 2);
947
+ debug(`Ingest answered HTTP 413 for ${n} span(s): sending two halves of ${mid} and ${n - mid}`);
948
+ await this._deliver(parts.slice(0, mid), start, call, out, false);
949
+ if (!out.cancelled) await this._deliver(parts.slice(mid), start, call, out, false);
950
+ } else {
951
+ out.dropped.too_large = (out.dropped.too_large ?? 0) + n;
797
952
  }
798
- isHalfOpen = true;
953
+ } else if (result.kind === "cancelled") {
954
+ out.cancelled = true;
955
+ } else {
956
+ out.failed.push({ n, reason: result.kind, answer: result.answer });
799
957
  }
800
- const maxAttempts = isHalfOpen ? 1 : this._maxRetries;
801
- for (let attempt = 0; attempt < maxAttempts; attempt++) {
802
- call.refusalAfterMs = null;
958
+ }
959
+ /** Send one request body until it is delivered or the policy stops. */
960
+ async _sendWithRetries(body, spansSent, start, call) {
961
+ const cancelled = call.cancel.signal;
962
+ let retryNumber = 0;
963
+ let attemptsHere = 0;
964
+ for (; ; ) {
965
+ if (cancelled.aborted) return { kind: "cancelled" };
966
+ call.retryAfterS = null;
803
967
  call.misroute = null;
804
968
  call.noRetry = false;
805
969
  call.status = null;
806
970
  call.error = null;
971
+ call.ack = null;
807
972
  call.attempts++;
808
- const result = await this._sendRequest(payload, parts.length, call, spans);
809
- if (cancelled.aborted) {
810
- this._acks.delete(spans);
811
- return "failure" /* FAILURE */;
973
+ attemptsHere++;
974
+ this.attempts++;
975
+ const result = await this._sendRequest(body, spansSent, call);
976
+ if (cancelled.aborted) return { kind: "cancelled" };
977
+ if (result === "success" /* SUCCESS */) return { kind: "delivered", ack: call.ack };
978
+ if (call.status === TOO_LARGE_STATUS) return { kind: "too_large" };
979
+ const answer = {
980
+ status: call.status,
981
+ error: call.error,
982
+ misroute: call.misroute,
983
+ zeroAck: call.noRetry,
984
+ sent: spansSent
985
+ };
986
+ if (!this._retryable(call)) return { kind: "transport_refused", answer };
987
+ if (attemptsHere === RETRY_WARN_AFTER_ATTEMPTS) this._warnOutage(answer, true);
988
+ if (this._noWait) return { kind: "shutdown_residue", answer };
989
+ if (this._maxRetries !== void 0 && attemptsHere >= this._maxRetries) {
990
+ return { kind: "retry_exhausted", answer };
812
991
  }
813
- if (result === "success" /* SUCCESS */) {
814
- this._consecutiveFailures = 0;
815
- this._failureKind = null;
816
- return result;
817
- }
818
- if (call.misroute !== null || call.noRetry) break;
819
- if (call.status !== null && AUTH_REFUSAL_STATUSES.has(call.status)) break;
820
- if (attempt < maxAttempts - 1) {
821
- const refusal = call.refusalAfterMs;
822
- const delay = refusal === null ? 100 * Math.pow(2, attempt) : refusal === REFUSAL_NO_HINT ? REFUSAL_BACKOFF_BASE_MS * Math.pow(2, attempt) : refusal;
823
- await sleep(delay, cancelled);
824
- if (cancelled.aborted) return "failure" /* FAILURE */;
992
+ const remainingS = this._retryHorizonS - (performance.now() - start) / 1e3;
993
+ if (remainingS <= 0) return { kind: "retry_exhausted", answer };
994
+ const hint = call.retryAfterS;
995
+ const waitS = hint !== null && hint > 0 ? hint : backoffDelayS(retryNumber, jitter.fraction());
996
+ const delayS = Math.min(waitS, remainingS);
997
+ retryNumber++;
998
+ debug(
999
+ `Ingest attempt ${attemptsHere} failed (${answer.status ?? answer.error ?? "unknown"}); next attempt in ${delayS.toFixed(2)} s (${Math.round(remainingS)} s of the horizon left)`
1000
+ );
1001
+ await this._retryWait(delayS * 1e3, cancelled);
1002
+ }
1003
+ }
1004
+ /** True when the answer of the last attempt of `call` can change on a retry. */
1005
+ _retryable(call) {
1006
+ if (call.misroute !== null || call.noRetry) return false;
1007
+ if (call.status !== null) return RETRY_STATUSES.has(call.status);
1008
+ if (this._endpointHasCredentials) return false;
1009
+ return call.error !== null;
1010
+ }
1011
+ /**
1012
+ * Wait `ms` before the next attempt. `abort()` and `endRetries()` end the wait at once. The
1013
+ * timer does not keep the process alive: when nothing else runs, `beforeExit` starts the
1014
+ * SDK's `shutdown()`, which ends the wait.
1015
+ */
1016
+ _retryWait(ms, signal) {
1017
+ return new Promise((resolve) => {
1018
+ if (signal.aborted || this._noWait || ms <= 0) return resolve();
1019
+ const done = () => {
1020
+ clearTimeout(timer);
1021
+ signal.removeEventListener("abort", done);
1022
+ this._waits.delete(done);
1023
+ resolve();
1024
+ };
1025
+ const timer = setTimeout(done, Math.min(ms, MAX_TIMER_MS));
1026
+ timer.unref?.();
1027
+ signal.addEventListener("abort", done, { once: true });
1028
+ this._waits.add(done);
1029
+ });
1030
+ }
1031
+ /** Turn the outcome of the parts of one batch into the result of `export()`. */
1032
+ _finish(spans, out, attempts) {
1033
+ const reasons = new Set(out.failed.map((f) => f.reason));
1034
+ if (out.delivered.length === 0 && Object.keys(out.dropped).length === 0 && reasons.size === 1) {
1035
+ const reason = out.failed[0].reason;
1036
+ const nFailed = out.failed.reduce((sum, f) => sum + f.n, 0);
1037
+ this._dropReasons.set(spans, reason);
1038
+ return this._fail(reason, out.failed[out.failed.length - 1].answer, nFailed, attempts);
1039
+ }
1040
+ let ack;
1041
+ if (out.delivered.length === 1 && Object.keys(out.dropped).length === 0 && out.failed.length === 0) {
1042
+ ack = out.delivered[0].ack;
1043
+ } else {
1044
+ let accepted = 0;
1045
+ let rejected = 0;
1046
+ let quotaDropped = 0;
1047
+ for (const part of out.delivered) {
1048
+ accepted += part.ack !== null ? part.ack.accepted : part.n;
1049
+ rejected += part.ack !== null ? part.ack.rejected : 0;
1050
+ quotaDropped += part.ack?.quotaDropped ?? 0;
825
1051
  }
1052
+ const dropped = { ...out.dropped };
1053
+ for (const f of out.failed) dropped[f.reason] = (dropped[f.reason] ?? 0) + f.n;
1054
+ ack = { accepted, rejected };
1055
+ if (quotaDropped > 0) ack.quotaDropped = quotaDropped;
1056
+ if (Object.keys(dropped).length > 0) ack.dropped = dropped;
826
1057
  }
827
- if (call.refusalAfterMs !== null) {
828
- warn(
829
- `Risicare: the ingest endpoint refused this batch for capacity (HTTP 429/503) after ${call.attempts} attempt(s). This is a healthy server shedding load, not an outage. This export of the batch failed; the circuit breaker does not count it. Reduce batch size or export rate if it persists.`
830
- );
1058
+ if (ack !== null) this._acks.set(spans, ack);
1059
+ this._consecutiveFailures = 0;
1060
+ this._failureKind = null;
1061
+ const tooLarge = out.dropped.too_large ?? 0;
1062
+ if (tooLarge > 0) this._warnTooLarge(tooLarge);
1063
+ for (const f of out.failed) {
1064
+ if (f.reason === "retry_exhausted") this._warnExhausted(f.n, attempts, f.answer);
1065
+ }
1066
+ return "success" /* SUCCESS */;
1067
+ }
1068
+ /** End a call in which no span was accounted for. Always returns FAILURE. */
1069
+ _fail(reason, answer, nSpans, attempts) {
1070
+ if (reason === "shutdown_residue") {
1071
+ debug(`Export ended by shutdown after ${attempts} attempt(s)`);
831
1072
  return "failure" /* FAILURE */;
832
1073
  }
833
- const misroute = call.misroute;
834
- if (misroute !== null && Date.now() - this._lastMisrouteWarnAt > MISROUTE_WARN_WINDOW_MS) {
835
- this._lastMisrouteWarnAt = Date.now();
836
- warn(
837
- `Risicare: spans are NOT reaching ${this._safeEndpoint}. It answered POST /v1/spans with ${misroute}. That is not the Risicare ingest gateway, so the endpoint is probably wrong (for example the dashboard URL instead of the ingest gateway URL). The SDK does not follow redirects, and this batch is counted as NOT delivered. Set endpoint or RISICARE_ENDPOINT to the ingest gateway URL.`
838
- );
1074
+ if (reason === "retry_exhausted") {
1075
+ if (answer.status === null || !CAPACITY_REFUSAL_STATUSES.has(answer.status)) this._stepBreaker();
1076
+ this._warnOutage(answer, true);
1077
+ this._warnExhausted(nSpans, attempts, answer);
1078
+ return "failure" /* FAILURE */;
839
1079
  }
1080
+ this._stepBreaker();
1081
+ this._warnOutage(answer, false);
1082
+ return "failure" /* FAILURE */;
1083
+ }
1084
+ /** Count one failed call toward the circuit breaker. */
1085
+ _stepBreaker() {
840
1086
  this._consecutiveFailures++;
841
- if (misroute === null && !call.noRetry) {
842
- const status = call.status;
843
- const kind = status !== null && AUTH_REFUSAL_STATUSES.has(status) ? "auth" : status !== null ? "http" : call.error !== null ? "none" : "unknown";
844
- let write = false;
845
- if (this._consecutiveFailures === 1 || this._failureKind === null) {
846
- if (Date.now() - this._lastFailureWarnAt > FAILURE_WARN_WINDOW_MS) {
847
- this._lastFailureWarnAt = Date.now();
848
- write = true;
849
- }
850
- this._failureKind = kind;
851
- } else if (kind !== this._failureKind) {
852
- this._failureKind = kind;
853
- write = true;
854
- }
855
- if (write) this._warnOutage(kind, status, call.error);
856
- }
857
1087
  if (this._consecutiveFailures >= this._circuitBreakerThreshold) {
858
1088
  this._circuitOpenUntil = Date.now() + this._circuitBreakerCooldownMs;
859
1089
  warn(
860
1090
  `HTTP exporter circuit breaker opened after ${this._consecutiveFailures} failures. Cooldown: ${this._circuitBreakerCooldownMs / 1e3}s`
861
1091
  );
862
1092
  }
863
- return "failure" /* FAILURE */;
864
1093
  }
865
- /** The outage WARNING for one kind of failure. */
866
- _warnOutage(kind, status, error) {
1094
+ /**
1095
+ * The outage WARNING: at the first failure of an outage, and when its kind changes. At most
1096
+ * one in the window for the first failure; a misroute has its own WARNING, at most one in
1097
+ * the window. `retrying` is true for a retryable answer (written after
1098
+ * `RETRY_WARN_AFTER_ATTEMPTS` failed attempts of one batch, or when its retries end before
1099
+ * that), false for an answer that a retry cannot change (one attempt). The WARNING names the last HTTP status, or the error
1100
+ * code when there was no answer. It never names the key or the response body, and names
1101
+ * the API key as a cause only for a 401 or 403. An ack of 0 and 0 has its WARNING from
1102
+ * `checkAck`.
1103
+ */
1104
+ _warnOutage(answer, retrying) {
1105
+ if (answer.misroute !== null) {
1106
+ if (Date.now() - this._lastMisrouteWarnAt > MISROUTE_WARN_WINDOW_MS) {
1107
+ this._lastMisrouteWarnAt = Date.now();
1108
+ warn(
1109
+ `Risicare: spans are NOT reaching ${this._safeEndpoint}. It answered POST /v1/spans with ${answer.misroute}. That is not the Risicare ingest gateway, so the endpoint is probably wrong (for example the dashboard URL instead of the ingest gateway URL). The SDK does not follow redirects, and this batch is counted as NOT delivered. Set endpoint or RISICARE_ENDPOINT to the ingest gateway URL.`
1110
+ );
1111
+ }
1112
+ return;
1113
+ }
1114
+ if (answer.zeroAck) return;
1115
+ const status = answer.status;
1116
+ const kind = status !== null && AUTH_REFUSAL_STATUSES.has(status) ? "auth" : status !== null ? "http" : answer.error !== null ? "none" : "unknown";
1117
+ let write = false;
1118
+ if (this._failureKind === null || !retrying && this._consecutiveFailures === 1) {
1119
+ if (Date.now() - this._lastFailureWarnAt > FAILURE_WARN_WINDOW_MS) {
1120
+ this._lastFailureWarnAt = Date.now();
1121
+ write = true;
1122
+ }
1123
+ this._failureKind = kind;
1124
+ } else if (kind !== this._failureKind) {
1125
+ this._failureKind = kind;
1126
+ write = true;
1127
+ }
1128
+ if (!write) return;
867
1129
  const where = `Risicare: spans are NOT reaching ${this._safeEndpoint}`;
1130
+ if (retrying) {
1131
+ let limit = `${this._retryHorizonS} s from its first attempt (retryHorizonS)`;
1132
+ if (this._maxRetries !== void 0) limit = `${this._maxRetries} attempt(s) (maxRetries) or ${limit}`;
1133
+ const retryText = `The SDK sends each batch again with a growing wait, for up to ${limit}, and then drops it (reason=retry_exhausted).`;
1134
+ if (kind === "http") {
1135
+ warn(`${where}: it answered HTTP ${status}. ${retryText} Set debug: true to see each attempt.`);
1136
+ } else {
1137
+ warn(
1138
+ `${where}: no answer (${answer.error}). The endpoint cannot be reached, or it did not answer in time. ${retryText} Set debug: true to see each attempt.`
1139
+ );
1140
+ }
1141
+ return;
1142
+ }
868
1143
  const tail = "Tracing continues, but the SDK drops the spans that it cannot send.";
869
1144
  if (kind === "auth") {
870
1145
  warn(
@@ -872,15 +1147,11 @@ var HttpExporter = class {
872
1147
  );
873
1148
  } else if (kind === "http") {
874
1149
  warn(
875
- `${where}: it answered HTTP ${status}. The server did not take the batch. Check the endpoint and the server. ${tail} Set debug: true to see each attempt.`
1150
+ `${where}: it answered HTTP ${status}. The server did not take the batch, and a retry cannot change that answer. Check the endpoint and the server. ${tail} Set debug: true to see each attempt.`
876
1151
  );
877
1152
  } else if (kind === "none" && this._endpointHasCredentials) {
878
1153
  warn(
879
- `${where}: no answer (${error}). The endpoint URL holds credentials (a user name or a password before the host), and fetch does not send a request to such a URL. Remove the credentials from the endpoint URL. ${tail}`
880
- );
881
- } else if (kind === "none") {
882
- warn(
883
- `${where}: no answer (${error}). The endpoint cannot be reached, or it did not answer in time. Check the endpoint and the network. ${tail} Set debug: true to see each attempt.`
1154
+ `${where}: no answer (${answer.error}). The endpoint URL holds credentials (a user name or a password before the host), and fetch does not send a request to such a URL. Remove the credentials from the endpoint URL. ${tail}`
884
1155
  );
885
1156
  } else {
886
1157
  warn(
@@ -888,33 +1159,38 @@ var HttpExporter = class {
888
1159
  );
889
1160
  }
890
1161
  }
1162
+ /** The WARNING for spans dropped as `retry_exhausted`. At most one in the window. */
1163
+ _warnExhausted(nSpans, attempts, answer) {
1164
+ if (Date.now() - this._lastExhaustedWarnAt <= FAILURE_WARN_WINDOW_MS) return;
1165
+ this._lastExhaustedWarnAt = Date.now();
1166
+ const last = answer.status !== null ? `HTTP ${answer.status}` : `no answer (${answer.error})`;
1167
+ const capacity = answer.status !== null && CAPACITY_REFUSAL_STATUSES.has(answer.status) ? " (over capacity)" : "";
1168
+ const limit = this._maxRetries !== void 0 && attempts >= this._maxRetries ? `the maxRetries limit of ${this._maxRetries} attempt(s)` : `the retry horizon of ${this._retryHorizonS} s (retryHorizonS)`;
1169
+ warn(
1170
+ `Risicare: ${nSpans} span(s) DISCARDED (reason=retry_exhausted): ${this._safeEndpoint} did not take them in ${attempts} attempt(s), and ${limit} is used up. The last answer: ${last}${capacity}. A gateway outage that is longer than the retry horizon loses spans. To wait longer, raise retryHorizonS.`
1171
+ );
1172
+ }
1173
+ /** The WARNING for spans dropped as `too_large`. One time for each exporter. */
1174
+ _warnTooLarge(nSpans) {
1175
+ if (this._tooLargeWarned) return;
1176
+ this._tooLargeWarned = true;
1177
+ warn(
1178
+ `Risicare: ${nSpans} span(s) DISCARDED (reason=too_large): ${this._safeEndpoint} answered HTTP 413 (the request is too large) for a batch, and again for a half of it after one split. The SDK does not send these spans again. Make the span attributes smaller. This WARNING is written one time; droppedSpansByReason.too_large has the count.`
1179
+ );
1180
+ }
891
1181
  /** Record a MISROUTE for the retry loop and the breaker. Always returns FAILURE. */
892
1182
  _noteMisroute(call, answer) {
893
1183
  call.misroute = this._scrub(answer, 300);
894
1184
  debug(`Ingest misroute: ${call.misroute}`);
895
1185
  return "failure" /* FAILURE */;
896
1186
  }
897
- /**
898
- * Record a CAPACITY REFUSAL for the retry loop. Returns true if it was one.
899
- *
900
- * `Retry-After` is honoured only in its delta-seconds form (RFC 9110 §10.2.3 also permits an
901
- * HTTP-date); anything else falls back to our own backoff rather than guessing at a date
902
- * parse.
903
- */
904
- _noteRefusal(call, status, retryAfterRaw) {
905
- if (!CAPACITY_REFUSAL_STATUSES.has(status)) return false;
906
- let delay = REFUSAL_NO_HINT;
907
- if (retryAfterRaw) {
908
- const secs = Number(retryAfterRaw);
909
- if (Number.isFinite(secs) && secs >= 0) {
910
- delay = Math.min(secs * 1e3, RETRY_AFTER_CEILING_MS);
911
- }
912
- }
913
- call.refusalAfterMs = delay;
914
- debug(`Ingest refused for capacity: HTTP ${status} (Retry-After=${this._scrub(retryAfterRaw)})`);
915
- return true;
1187
+ /** Record the `Retry-After` wait of a retryable answer for the retry loop. */
1188
+ _noteRetryAfter(call, status, retryAfterRaw) {
1189
+ if (!RETRY_STATUSES.has(status)) return;
1190
+ call.retryAfterS = parseRetryAfterS(retryAfterRaw);
1191
+ debug(`Ingest answered HTTP ${status} (Retry-After=${this._scrub(retryAfterRaw)})`);
916
1192
  }
917
- async _sendRequest(body, spansSent, call, spans) {
1193
+ async _sendRequest(body, spansSent, call) {
918
1194
  const cancelled = call.cancel.signal;
919
1195
  const timeoutMs = this._timeoutMs;
920
1196
  const url = `${this._endpoint}/v1/spans`;
@@ -982,14 +1258,15 @@ var HttpExporter = class {
982
1258
  } else if (ack.result === "success" /* SUCCESS */) {
983
1259
  this._quota.accepted(`the ingest endpoint ${this._safeEndpoint}`);
984
1260
  }
985
- if (ack.result === "success" /* SUCCESS */) this._acks.set(spans, ack.counts);
1261
+ if (ack.result === "success" /* SUCCESS */) call.ack = ack.counts;
986
1262
  if (ack.final) call.noRetry = true;
987
1263
  return ack.result;
988
- } else if (response.status === 408 || response.status === 504) {
1264
+ }
1265
+ this._noteRetryAfter(call, response.status, response.headers?.get?.("Retry-After") ?? null);
1266
+ if (response.status === 408 || response.status === 504) {
989
1267
  debug(`HTTP export timeout: ${response.status}`);
990
1268
  return "timeout" /* TIMEOUT */;
991
1269
  }
992
- this._noteRefusal(call, response.status, response.headers.get("Retry-After"));
993
1270
  debug(`HTTP export failed: ${response.status}`);
994
1271
  return "failure" /* FAILURE */;
995
1272
  } catch (e) {
@@ -1003,20 +1280,9 @@ var HttpExporter = class {
1003
1280
  }
1004
1281
  }
1005
1282
  shutdown() {
1283
+ this._noWait = false;
1006
1284
  }
1007
1285
  };
1008
- function sleep(ms, signal) {
1009
- return new Promise((resolve) => {
1010
- if (signal?.aborted) return resolve();
1011
- const done = () => {
1012
- clearTimeout(timer);
1013
- signal?.removeEventListener("abort", done);
1014
- resolve();
1015
- };
1016
- const timer = setTimeout(done, ms);
1017
- signal?.addEventListener("abort", done, { once: true });
1018
- });
1019
- }
1020
1286
  function asCount(v) {
1021
1287
  return typeof v === "number" && Number.isInteger(v) && v >= 0 ? v : void 0;
1022
1288
  }
@@ -1097,7 +1363,7 @@ function className(error) {
1097
1363
  }
1098
1364
  return typeof error;
1099
1365
  }
1100
- function responseReadFailed(provider, site, error, span) {
1366
+ function responseReadFailed(provider, site, error, span, part = "response") {
1101
1367
  try {
1102
1368
  const cls = className(error);
1103
1369
  span?.setAttribute("risicare.observer_error", cls);
@@ -1105,7 +1371,7 @@ function responseReadFailed(provider, site, error, span) {
1105
1371
  const key = `${provider}\0${site}\0${cls}`;
1106
1372
  if (logged.has(key) || logged.size >= LOGGED_MAX) return;
1107
1373
  logged.add(key);
1108
- debug(`could not read the ${provider} response in ${site} (${cls}); the span keeps fewer facts`);
1374
+ debug(`could not read the ${provider} ${part} in ${site} (${cls}); the span keeps fewer facts`);
1109
1375
  } catch {
1110
1376
  }
1111
1377
  }
@@ -1116,6 +1382,13 @@ function readResponseSafely(provider, site, span, read) {
1116
1382
  responseReadFailed(provider, site, error, span);
1117
1383
  }
1118
1384
  }
1385
+ function readRequestSafely(provider, site, span, read) {
1386
+ try {
1387
+ read();
1388
+ } catch (error) {
1389
+ responseReadFailed(provider, site, error, span, "request");
1390
+ }
1391
+ }
1119
1392
 
1120
1393
  // src/utils/stream-proxy.ts
1121
1394
  var HANDOFF = /* @__PURE__ */ new Set(["tee", "toReadableStream", "getReader", "pipeTo", "pipeThrough"]);
@@ -1860,7 +2133,39 @@ function calculateCost(model, promptTokens, completionTokens) {
1860
2133
  function tokenCount(value) {
1861
2134
  return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
1862
2135
  }
1863
- function recordTokenUsage(span, counts, model) {
2136
+ var CACHE_READ_TOKENS_KEY = "gen_ai.usage.cache_read.input_tokens";
2137
+ var CACHE_WRITE_TOKENS_KEY = "gen_ai.usage.cache_write.input_tokens";
2138
+ var REASONING_TOKENS_KEY = "gen_ai.usage.reasoning.output_tokens";
2139
+ function providerInteger(value) {
2140
+ return typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : void 0;
2141
+ }
2142
+ function nested(obj, outer, inner) {
2143
+ if (!obj || typeof obj !== "object") return void 0;
2144
+ const details = obj[outer];
2145
+ if (!details || typeof details !== "object") return void 0;
2146
+ return details[inner];
2147
+ }
2148
+ function anthropicUsageDetails(usage) {
2149
+ try {
2150
+ const u = usage && typeof usage === "object" ? usage : {};
2151
+ return {
2152
+ cacheRead: u.cache_read_input_tokens,
2153
+ cacheWrite: u.cache_creation_input_tokens,
2154
+ reasoning: nested(usage, "output_tokens_details", "thinking_tokens")
2155
+ };
2156
+ } catch {
2157
+ return {};
2158
+ }
2159
+ }
2160
+ function recordUsageDetails(span, details) {
2161
+ const cacheRead = providerInteger(details.cacheRead);
2162
+ const cacheWrite = providerInteger(details.cacheWrite);
2163
+ const reasoning = providerInteger(details.reasoning);
2164
+ if (cacheRead !== void 0) span.setAttribute(CACHE_READ_TOKENS_KEY, cacheRead);
2165
+ if (cacheWrite !== void 0) span.setAttribute(CACHE_WRITE_TOKENS_KEY, cacheWrite);
2166
+ if (reasoning !== void 0) span.setAttribute(REASONING_TOKENS_KEY, reasoning);
2167
+ }
2168
+ function recordTokenUsage(span, counts, model, details) {
1864
2169
  const promptTokens = tokenCount(counts.prompt);
1865
2170
  const completionTokens = tokenCount(counts.completion);
1866
2171
  const bothKnown = promptTokens !== void 0 && completionTokens !== void 0;
@@ -1871,6 +2176,7 @@ function recordTokenUsage(span, counts, model) {
1871
2176
  if (completionTokens !== void 0) span.setAttribute("gen_ai.usage.completion_tokens", completionTokens);
1872
2177
  if (totalTokens !== void 0) span.setAttribute("gen_ai.usage.total_tokens", totalTokens);
1873
2178
  if (costUsd !== void 0) span.setAttribute("llm.cost.total_usd", costUsd);
2179
+ if (details) recordUsageDetails(span, details);
1874
2180
  }
1875
2181
 
1876
2182
  // src/utils/assert-client.ts
@@ -1957,7 +2263,7 @@ function enrichSpanFromResponse(span, response) {
1957
2263
  span.setAttribute("gen_ai.response.finish_reasons", [stopReason]);
1958
2264
  }
1959
2265
  if (usage) {
1960
- recordTokenUsage(span, { prompt: usage.input_tokens, completion: usage.output_tokens }, model);
2266
+ recordTokenUsage(span, { prompt: usage.input_tokens, completion: usage.output_tokens }, model, anthropicUsageDetails(usage));
1961
2267
  }
1962
2268
  }
1963
2269
  function createMessagesProxy(originalCreate) {
@@ -1985,9 +2291,23 @@ function createMessagesProxy(originalCreate) {
1985
2291
  const traceContent = tracer.traceContent;
1986
2292
  if (traceContent) {
1987
2293
  captureSystemPrompt(span, params.system);
1988
- capturePromptMessages(span, params.messages, true);
2294
+ readRequestSafely(
2295
+ "anthropic",
2296
+ "capturePromptMessages",
2297
+ span,
2298
+ () => capturePromptMessages(span, params.messages, true)
2299
+ );
1989
2300
  }
1990
2301
  const streamed = {};
2302
+ const noteDetails = (u) => {
2303
+ const d = anthropicUsageDetails(u);
2304
+ const cacheRead = providerInteger(d.cacheRead);
2305
+ const cacheWrite = providerInteger(d.cacheWrite);
2306
+ const reasoning = providerInteger(d.reasoning);
2307
+ if (cacheRead !== void 0) streamed.cacheRead = cacheRead;
2308
+ if (cacheWrite !== void 0) streamed.cacheWrite = cacheWrite;
2309
+ if (reasoning !== void 0) streamed.reasoning = reasoning;
2310
+ };
1991
2311
  return observeProviderCall(
1992
2312
  span,
1993
2313
  {
@@ -2008,19 +2328,29 @@ function createMessagesProxy(originalCreate) {
2008
2328
  streamed.id = e.message.id;
2009
2329
  const u = e.message.usage;
2010
2330
  if (typeof u?.input_tokens === "number") streamed.input = u.input_tokens;
2331
+ noteDetails(u);
2011
2332
  } else if (e?.type === "message_delta") {
2012
2333
  if (e.delta?.stop_reason) streamed.stop_reason = e.delta.stop_reason;
2013
2334
  if (typeof e.usage?.output_tokens === "number") streamed.output = e.usage.output_tokens;
2335
+ noteDetails(e.usage);
2014
2336
  }
2015
2337
  },
2016
2338
  beforeEnd: () => {
2017
- if (streamed.model === void 0 && streamed.input === void 0 && streamed.output === void 0) return;
2339
+ if (streamed.model === void 0 && streamed.input === void 0 && streamed.output === void 0 && streamed.cacheRead === void 0 && streamed.cacheWrite === void 0 && streamed.reasoning === void 0) {
2340
+ return;
2341
+ }
2018
2342
  enrichSpanFromResponse(span, {
2019
2343
  model: streamed.model,
2020
2344
  id: streamed.id,
2021
2345
  stop_reason: streamed.stop_reason,
2022
2346
  // A count the stream did not give stays unknown (token-usage.ts).
2023
- usage: { input_tokens: streamed.input, output_tokens: streamed.output }
2347
+ usage: {
2348
+ input_tokens: streamed.input,
2349
+ output_tokens: streamed.output,
2350
+ cache_read_input_tokens: streamed.cacheRead,
2351
+ cache_creation_input_tokens: streamed.cacheWrite,
2352
+ output_tokens_details: streamed.reasoning === void 0 ? void 0 : { thinking_tokens: streamed.reasoning }
2353
+ }
2024
2354
  });
2025
2355
  }
2026
2356
  },