@anvia/langfuse 0.3.9 → 0.5.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.
package/README.md CHANGED
@@ -63,6 +63,8 @@ explicit options always win.
63
63
  | `environment` | `LANGFUSE_TRACING_ENVIRONMENT`| Tag attached to every trace. |
64
64
  | `release` | `LANGFUSE_RELEASE` | Tag attached to every trace. |
65
65
  | `serviceName` | `LANGFUSE_SERVICE_NAME` | Recorded on the root observation and as the OTel `service.name` resource attribute. |
66
+ | `captureMode` | — | `"safe"` (default) records instructions/messages plus request summaries; `"full"` includes documents, tool definitions, schemas, and additional parameters. |
67
+ | `captureMaxBytes` | — | Maximum encoded size per captured value; defaults to 262,144 bytes and must be at least 96. |
66
68
 
67
69
  ```ts
68
70
  const tracing = langfuse.create({
@@ -73,24 +75,26 @@ const tracing = langfuse.create({
73
75
 
74
76
  ## Observation metadata
75
77
 
76
- The adapter records extra data on Langfuse observations so the UI
77
- shows everything the agent runtime emits:
78
+ The adapter records structured model and runtime data while keeping
79
+ the default capture surface bounded:
78
80
 
79
- - **Generation observations** carry `providerRequest` and `modelInfo`
80
- (with `provider`, `defaultModel`, and `capabilities`) on start, and
81
- `firstDeltaMs` on end. `usageDetails` always includes
82
- `cachedInputTokens` and `cacheCreationInputTokens`.
81
+ - **Generation observations** carry system instructions, model messages,
82
+ the resolved model, and `modelInfo`. Full capture also includes the
83
+ sanitized `providerRequest`.
84
+ `completionStartTime` and `firstDeltaMs` provide native time-to-first-token data.
85
+ - **Usage details** use mutually exclusive `input`, `output`, cache, and
86
+ reasoning buckets so Langfuse can infer cost without double counting.
83
87
  - **Generation observations** receive a `generation.update({ output: { delta } })`
84
88
  call for every streaming delta (`text_delta`, `reasoning_delta`,
85
89
  `tool_call`), so the Langfuse UI reflects partial output as the
86
90
  model produces it.
87
- - **Tool observations** carry `toolDefinition` and `toolMetadata` on
88
- start, and `structuredResult` on end.
91
+ - **Tool observations** carry arguments on start and structured results on
92
+ end. Definitions and tool metadata are included when `captureMode: "full"`.
89
93
  - **Root run observation** carries `serviceName` and the configured
90
94
  `metadata` from `AgentRunStartArgs`.
91
95
 
92
- User-supplied `trace.metadata` always wins over the built-in fields
93
- above (the spread order preserves it).
96
+ Binary and base64 bodies are replaced with omission markers. Oversized
97
+ values are replaced with deterministic bounded previews.
94
98
 
95
99
  ## Eval Scores
96
100
 
@@ -110,6 +114,7 @@ const reporter = createLangfuseEvalReporter(tracing, {
110
114
  onMissingTrace: "ignore", // "ignore" | "warn" | "throw"
111
115
  truncateInputAt: 2048, // max bytes for case input/expected summaries
112
116
  includeMessages: true, // include output.messages in score metadata
117
+ includeContext: false, // opt in to context/retrievalContext score metadata
113
118
  });
114
119
  ```
115
120
 
@@ -127,6 +132,10 @@ const reporter = createLangfuseEvalReporter(tracing, {
127
132
  - `includeMessages` controls whether `output.messages` (if present)
128
133
  is included in score metadata.
129
134
 
135
+ - `includeContext` controls whether case `context` and
136
+ `retrievalContext` are included in score metadata. It defaults to
137
+ `false` because retrieved documents may contain sensitive data.
138
+
130
139
  ### Trace resolution
131
140
 
132
141
  The reporter resolves a trace ID for each case in three tiers:
@@ -231,8 +240,8 @@ trace?.addEvent("validation.passed");
231
240
  trace?.addAttributes({ quality: "high" });
232
241
  ```
233
242
 
234
- `addEvent` creates a Langfuse `event` observation under the active
235
- root and ends it immediately. `addAttributes` updates the root
243
+ `addEvent` creates an instantaneous Langfuse `event` observation under the active
244
+ root. `addAttributes` updates the root
236
245
  observation's metadata. Both calls bubble up to Langfuse via the
237
246
  existing OpenTelemetry span processor.
238
247
 
@@ -306,6 +315,7 @@ For eval suites, `runEvalAsExperiment` runs the suite and posts a
306
315
  dataset run alongside the metric scores:
307
316
 
308
317
  ```ts
318
+ import { agentEvalTarget } from "@anvia/core/evals";
309
319
  import { runEvalAsExperiment } from "@anvia/langfuse";
310
320
 
311
321
  const { suite, datasetRun } = await runEvalAsExperiment(
@@ -315,7 +325,7 @@ const { suite, datasetRun } = await runEvalAsExperiment(
315
325
  { id: "c-1", input: "a", expected: "A" },
316
326
  { id: "c-2", input: "b", expected: "B" },
317
327
  ],
318
- target: async (input) => input.toUpperCase(),
328
+ target: agentEvalTarget(supportAgent),
319
329
  metrics: [/* ... */],
320
330
  reporters: [/* existing reporters still score */],
321
331
  },
@@ -323,12 +333,19 @@ const { suite, datasetRun } = await runEvalAsExperiment(
323
333
  tracing,
324
334
  datasetName: "smoke-set",
325
335
  runName: "smoke-run",
336
+ publishScores: true,
337
+ reporterOptions: { onMissingTrace: "warn" },
326
338
  },
327
339
  );
328
340
 
329
341
  console.log(suite.passed, datasetRun.posted);
330
342
  ```
331
343
 
344
+ `publishScores` adds a Langfuse eval reporter without replacing reporters
345
+ already configured on the suite. `agentEvalTarget(...)` returns the response trace used to attach
346
+ each score. Set `includeContexts: true` only when case context may be persisted in Langfuse dataset
347
+ metadata.
348
+
332
349
  ### Options
333
350
 
334
351
  - `createLangfuseDatasetClient(tracing, options)`:
@@ -369,13 +386,12 @@ const prompts = createLangfusePromptClient(tracing);
369
386
  const prompt = await prompts.getPrompt("support.system");
370
387
  console.log(prompt.prompt, prompt.version);
371
388
 
372
- await tracing.startRun({
373
- agentName: "support",
374
- prompt: { role: "user", content: [{ type: "text", text: "hi" }] },
375
- history: [],
376
- maxTurns: 3,
377
- promptRef: { name: "support.system", version: prompt.version },
378
- });
389
+ await agent
390
+ .prompt("hi")
391
+ .withTrace({
392
+ promptRef: { name: "support.system", version: prompt.version },
393
+ })
394
+ .send();
379
395
  ```
380
396
 
381
397
  `promptRef` is also accepted on `trace.metadata` (keys
@@ -424,9 +440,10 @@ const tracing = langfuse.create({
424
440
  });
425
441
  ```
426
442
 
427
- - `redactInputs` redacts text on root inputs, chat history, and tool
428
- arguments.
429
- - `redactOutputs` redacts generation output text and tool results.
443
+ - `redactInputs` redacts system instructions, root inputs, chat history,
444
+ tool arguments, request metadata, and nested-agent inputs.
445
+ - `redactOutputs` redacts streaming/final generation output, errors,
446
+ tool results, transcripts, and nested-agent outputs.
430
447
  - `"deep"` recurses into nested objects and arrays (in addition to
431
448
  top-level strings).
432
449
 
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ import { AgentObserver } from '@anvia/core/observability';
3
3
  import { EvalReporter, EvalSuiteResult, RunEvalSuiteOptions } from '@anvia/core/evals';
4
4
 
5
5
  type LangfuseRedactionMode = boolean | "deep";
6
+ type LangfuseCaptureMode = "safe" | "full";
6
7
  type LangfuseRedactionOptions$1 = {
7
8
  patterns?: RedactorPattern$1[];
8
9
  replacement?: string;
@@ -22,6 +23,8 @@ type LangfuseTracingOptions = {
22
23
  scoreBatchSize?: number | undefined;
23
24
  scoreFlushIntervalMs?: number | undefined;
24
25
  scoreMaxRetries?: number | undefined;
26
+ captureMode?: LangfuseCaptureMode | undefined;
27
+ captureMaxBytes?: number | undefined;
25
28
  redactInputs?: LangfuseRedactionMode | undefined;
26
29
  redactOutputs?: LangfuseRedactionMode | undefined;
27
30
  redaction?: LangfuseRedactionOptions$1 | undefined;
@@ -60,6 +63,7 @@ type LangfuseEvalReporterOptions = {
60
63
  onMissingTrace?: "ignore" | "warn" | "throw" | undefined;
61
64
  truncateInputAt?: number | undefined;
62
65
  includeMessages?: boolean | undefined;
66
+ includeContext?: boolean | undefined;
63
67
  };
64
68
  type LangfuseDatasetClientOptions = {
65
69
  baseUrl?: string | undefined;
@@ -157,6 +161,9 @@ type RunEvalAsExperimentOptions<Input, Output, Expected = unknown> = Omit<Langfu
157
161
  client?: LangfuseDatasetClient;
158
162
  pageSize?: number | undefined;
159
163
  timeoutMs?: number | undefined;
164
+ publishScores?: boolean | undefined;
165
+ reporterOptions?: LangfuseEvalReporterOptions | undefined;
166
+ includeContexts?: boolean | undefined;
160
167
  };
161
168
  type RunEvalAsExperimentResult<Input, Output, Expected = unknown> = {
162
169
  suite: EvalSuiteResult<Input, Output, Expected>;
@@ -193,4 +200,4 @@ declare const langfuse: {
193
200
  create(options?: LangfuseTracingOptions): LangfuseTracing;
194
201
  };
195
202
 
196
- export { DEFAULT_PATTERNS, type LangfuseChatMessage, type LangfuseDataset, type LangfuseDatasetClient, type LangfuseDatasetClientOptions, type LangfuseDatasetItem, type LangfuseEvalReporterOptions, type LangfusePrompt, type LangfusePromptClient, type LangfusePromptClientOptions, type LangfusePromptGetOptions, type LangfuseRedactionMode, type LangfuseRedactionOptions, type LangfuseRunExperimentOptions, type LangfuseRunExperimentResult, type LangfuseRunItemError, type LangfuseRunItemResult, type LangfuseScoreArgs, type LangfuseScoreDataType, LangfuseScoreError, type LangfuseTraceHandle, type LangfuseTracing, type LangfuseTracingOptions, type PiiRedactor, type RedactorPattern, type RunEvalAsExperimentOptions, type RunEvalAsExperimentResult, createLangfuseDatasetClient, createLangfuseEvalReporter, createLangfusePromptClient, createPiiRedactor, langfuse, runEvalAsExperiment };
203
+ export { DEFAULT_PATTERNS, type LangfuseCaptureMode, type LangfuseChatMessage, type LangfuseDataset, type LangfuseDatasetClient, type LangfuseDatasetClientOptions, type LangfuseDatasetItem, type LangfuseEvalReporterOptions, type LangfusePrompt, type LangfusePromptClient, type LangfusePromptClientOptions, type LangfusePromptGetOptions, type LangfuseRedactionMode, type LangfuseRedactionOptions, type LangfuseRunExperimentOptions, type LangfuseRunExperimentResult, type LangfuseRunItemError, type LangfuseRunItemResult, type LangfuseScoreArgs, type LangfuseScoreDataType, LangfuseScoreError, type LangfuseTraceHandle, type LangfuseTracing, type LangfuseTracingOptions, type PiiRedactor, type RedactorPattern, type RunEvalAsExperimentOptions, type RunEvalAsExperimentResult, createLangfuseDatasetClient, createLangfuseEvalReporter, createLangfusePromptClient, createPiiRedactor, langfuse, runEvalAsExperiment };