agent-lattice 0.18.0 → 0.20.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
@@ -86,7 +86,7 @@ them, so keep expensive handling off the loop itself.
86
86
  | `stream_event` | While the model is responding | Raw provider stream event for incremental rendering. |
87
87
  | `assistant` | After each model turn is assembled | The `AssistantModelMessage` with text / `tool_use` blocks and provider metadata. |
88
88
  | `user` | After a whole tool batch finishes | Tool results as `ToolResultBlock[]`; the prompt is never echoed. |
89
- | `result` | Once, at the end of the query | Final text, `subtype` (`"success"`, `"interrupted"`, or an error variant), and token usage. |
89
+ | `result` | Once, at the end of the query | Final text, `subtype` (`"success"`, `"interrupted"`, or an error variant), optional `structuredResult`, and token usage. |
90
90
 
91
91
  For the exact per-event guarantees see
92
92
  [Streaming Events](https://docs.claude-code-sdk.com/concepts/streaming-events/).
@@ -150,6 +150,14 @@ Anthropic-compatible providers (for example DeepSeek's
150
150
  `https://api.deepseek.com/anthropic` endpoint) default thinking to on —
151
151
  omitting the field would leave it enabled.
152
152
 
153
+ On DeepSeek, the on/off switch is the only thinking control that works:
154
+ DeepSeek accepts `budget_tokens` but ignores the value, treats `adaptive` as
155
+ plain enabled thinking, and does not support `reasoning_effort` at all. Its own
156
+ thinking-strength knob is `output_config.effort`, which the SDK does not expose
157
+ yet. See
158
+ [Provider Compatibility](https://docs.claude-code-sdk.com/reference/provider-compatibility/)
159
+ for the full matrix.
160
+
153
161
  For Kimi K3 through an Anthropic-compatible endpoint or gateway, use
154
162
  `reasoningEffort` to send the provider's top-level `reasoning_effort` parameter:
155
163
 
@@ -308,6 +316,82 @@ root and share one trace session. Each Agent keeps its own SDK session identity,
308
316
  recorded as `agent_session_id` metadata, so tracing does not change Agent state
309
317
  or returned SDK messages.
310
318
 
319
+ ## Langfuse Context Tracing
320
+
321
+ *Requires 0.19.0 or later.*
322
+
323
+ The Langfuse adapter targets the current Langfuse JS SDK generation
324
+ (`@langfuse/tracing` v5), which is OpenTelemetry-based. Register the
325
+ `LangfuseSpanProcessor` once at process startup, then create the tracer —
326
+ no other wiring needed.
327
+
328
+ Configure Langfuse with its standard environment variables:
329
+
330
+ ```bash
331
+ LANGFUSE_PUBLIC_KEY=<your-langfuse-public-key>
332
+ LANGFUSE_SECRET_KEY=<your-langfuse-secret-key>
333
+ LANGFUSE_BASE_URL=https://us.cloud.langfuse.com # or your self-hosted host
334
+ ```
335
+
336
+ ```bash
337
+ npm install @langfuse/otel @opentelemetry/sdk-trace-node
338
+ ```
339
+
340
+ ```ts
341
+ // instrumentation: register the span processor before agents run.
342
+ import { LangfuseSpanProcessor } from "@langfuse/otel";
343
+ import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
344
+
345
+ export const langfuseSpanProcessor = new LangfuseSpanProcessor();
346
+
347
+ const tracerProvider = new NodeTracerProvider({
348
+ spanProcessors: [langfuseSpanProcessor],
349
+ });
350
+ tracerProvider.register();
351
+ ```
352
+
353
+ ```ts
354
+ import {
355
+ createAgent,
356
+ createCompositeContextTracer,
357
+ createJsonlContextTracer,
358
+ createLangfuseContextTracer,
359
+ } from "agent-lattice";
360
+ import { langfuseSpanProcessor } from "./instrumentation";
361
+
362
+ const tracer = createCompositeContextTracer([
363
+ createJsonlContextTracer({ path: ".agent-runs/session.jsonl" }),
364
+ createLangfuseContextTracer({
365
+ // Drained by tracer.flush()/close() so spans reach Langfuse before a
366
+ // short-lived process exits.
367
+ spanProcessor: langfuseSpanProcessor,
368
+ tags: ["local-debug"],
369
+ }),
370
+ ]);
371
+
372
+ const agent = createAgent({
373
+ apiKey: process.env.DEEPSEEK_API_KEY,
374
+ baseURL: "https://api.deepseek.com/anthropic",
375
+ model: "deepseek-v4-flash",
376
+ tracer,
377
+ });
378
+
379
+ try {
380
+ await agent.prompt("Trace this run.", { stream: false });
381
+ } finally {
382
+ await tracer.close?.();
383
+ }
384
+ ```
385
+
386
+ Langfuse receives one trace per SDK query: the agent run is a root `chain`
387
+ observation carrying the trace name, session id, and tags; model turns appear
388
+ as child `generation` observations and SDK tool calls as child `tool`
389
+ observations. For a `Team` query, delegated runs nest as child `chain`
390
+ observations under the team root, so one handoff invocation stays one trace.
391
+
392
+ `startObservation` defaults to the bundled `@langfuse/tracing` function; pass
393
+ `startObservation` only to inject a custom runtime or a test fake.
394
+
311
395
  Custom sinks can implement the same interface for SQLite, OpenTelemetry, object
312
396
  storage, or host-specific observability. A `ContextTracer` port object exposes
313
397
  methods only — `failOnError` is bound when the factory creates the tracer, not
@@ -339,6 +423,12 @@ const agent = createAgent({
339
423
  });
340
424
  ```
341
425
 
426
+ Pass an explicit `deepseek-*` model name — unknown names are silently mapped to
427
+ `deepseek-v4-flash`. For which SDK options DeepSeek actually honors (thinking
428
+ budgets are ignored; `reasoningEffort` does not apply; structured output is not
429
+ supported), see
430
+ [Provider Compatibility](https://docs.claude-code-sdk.com/reference/provider-compatibility/).
431
+
342
432
  ## Custom Tool
343
433
 
344
434
  ```ts
@@ -386,6 +476,92 @@ started concurrently, their `tool_result` blocks still enter the history, and
386
476
  model call is skipped. When several tools in a batch set `endTurn`, the first
387
477
  one's content becomes the result text.
388
478
 
479
+ A tool can also return `structuredResult` next to `endTurn: true` to carry a
480
+ structured payload to `SDKResultMessage.structuredResult`
481
+ (*requires 0.20.0 or later*). Without `endTurn`, `structuredResult` is
482
+ ignored.
483
+
484
+ ## Structured Output Via submit_output
485
+
486
+ *Requires 0.20.0 or later.*
487
+
488
+ Set `AgentOptions.outputSchema` when a run must deliver a typed result rather
489
+ than free text. The SDK injects a built-in `submit_output` tool (exported as
490
+ `SUBMIT_OUTPUT_TOOL_NAME`) whose input schema is your schema converted to JSON
491
+ Schema — a zod schema works directly, since `OutputSchema<T>` is just
492
+ `{ parse(input: unknown): T }`:
493
+
494
+ ```ts
495
+ import { createAgent } from "agent-lattice";
496
+ import { z } from "zod/v4";
497
+
498
+ const reviewSchema = z.object({
499
+ approved: z.boolean(),
500
+ issues: z.array(z.string()),
501
+ });
502
+
503
+ const reviewer = createAgent({
504
+ model: "claude-sonnet-4-6",
505
+ systemPrompt: "Review the change and submit your verdict.",
506
+ outputSchema: reviewSchema,
507
+ });
508
+
509
+ const result = await reviewer.prompt("Review the patch in this workspace.");
510
+ if (result.subtype === "success") {
511
+ console.log(result.structuredResult); // { approved: false, issues: [...] }
512
+ }
513
+ ```
514
+
515
+ The structure is enforced by the harness, not by prompt discipline:
516
+
517
+ - The model submits its answer by calling `submit_output`. The payload is
518
+ validated against the schema first; a validation failure goes back into the
519
+ loop as an error `tool_result`, so the model can fix it and retry. A valid
520
+ submission ends the run with `subtype: "success"` and the payload on
521
+ `SDKResultMessage.structuredResult`.
522
+ - If the model ends its turn without calling `submit_output`, the run fails
523
+ with `subtype: "error_missing_output"` and a `MissingOutputError`. There is
524
+ no fallback that parses the final text as JSON.
525
+ - `submit_output` must be the only tool call in its batch. A batch that mixes
526
+ it with other calls — or contains two submissions — is rejected with code
527
+ `submit_output_exclusive_batch` and the loop continues.
528
+ - The name is reserved: registering your own `submit_output` tool while
529
+ `outputSchema` is set throws from `createAgent`/`addTools`.
530
+
531
+ Unlike `outputFormat` (which relies on the provider's
532
+ `response_format`/`json_schema` support — DeepSeek ignores it, see
533
+ [Provider Compatibility](https://docs.claude-code-sdk.com/reference/provider-compatibility/)),
534
+ `submit_output` only requires a model that can call tools, so it ports to any
535
+ tool-capable provider.
536
+
537
+ The same schema composes with `agentTool()` for parent/child delegation: pass
538
+ it to the child agent and to `AgentToolOptions.outputSchema` on the parent
539
+ side, and an `ask` call returns the child's validated output as a JSON string:
540
+
541
+ ```ts
542
+ import { agentTool, createAgent } from "agent-lattice";
543
+
544
+ const child = createAgent({
545
+ model: "claude-sonnet-4-5",
546
+ systemPrompt: "You review code and submit a structured verdict.",
547
+ outputSchema: reviewSchema, // child submits via submit_output
548
+ });
549
+
550
+ const parent = createAgent({
551
+ model: "claude-sonnet-4-6",
552
+ tools: [
553
+ agentTool("review", child, {
554
+ description: "Ask the reviewer to audit a change.",
555
+ outputSchema: reviewSchema, // validates the child's submission
556
+ }),
557
+ ],
558
+ });
559
+ ```
560
+
561
+ If the child ends without submitting or submits a payload that fails the
562
+ parent's schema, the tool returns an `is_error` `tool_result` starting with
563
+ `child_output_invalid:`, so the parent model sees the failure and can retry.
564
+
389
565
  ## Concurrent Tool Calls
390
566
 
391
567
  The model requests concurrency by returning multiple `tool_use` blocks in one
package/dist/index.d.ts CHANGED
@@ -4,6 +4,8 @@ import type { OAuthClientProvider } from "@modelcontextprotocol/sdk/client/auth.
4
4
  import { type StdioServerParameters } from "@modelcontextprotocol/sdk/client/stdio.js";
5
5
  import { type StreamableHTTPClientTransportOptions } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
6
6
  import type { RunEvent, RunTree, RunTreeConfig } from "langsmith/run_trees";
7
+ import { startObservation as bundledLangfuseStartObservation } from "@langfuse/tracing";
8
+ import type { LangfuseChain, LangfuseGeneration, LangfuseObservation, LangfuseTool } from "@langfuse/tracing";
7
9
  import { z } from "zod/v4";
8
10
  export type TextBlock = TextBlockParam;
9
11
  export type ImageBlock = ImageBlockParam;
@@ -68,7 +70,7 @@ export type AgentRuntimeDelegateResult = {
68
70
  workspaceGrants?: WorkspaceGrant[];
69
71
  };
70
72
  export type AgentRuntimeFailure = {
71
- code: "max_turns_exceeded" | "api_error" | "tool_execution_error" | "permission_denied" | "agent_error";
73
+ code: "max_turns_exceeded" | "api_error" | "tool_execution_error" | "permission_denied" | "missing_output" | "agent_error";
72
74
  message: string;
73
75
  name: string;
74
76
  };
@@ -153,6 +155,27 @@ export type LangSmithContextTracerOptions = {
153
155
  redact?: (event: ContextTraceEvent) => ContextTraceEvent | undefined;
154
156
  failOnError?: boolean;
155
157
  };
158
+ export type LangfuseKVMap = Record<string, unknown>;
159
+ export type LangfuseObservationLike = LangfuseObservation;
160
+ export type LangfuseChainLike = LangfuseChain;
161
+ export type LangfuseGenerationLike = LangfuseGeneration;
162
+ export type LangfuseToolLike = LangfuseTool;
163
+ export type LangfuseStartObservation = typeof bundledLangfuseStartObservation;
164
+ /** Drained by the Langfuse tracer's flush()/close(); a LangfuseSpanProcessor satisfies this. */
165
+ export type LangfuseFlushableSpanProcessor = {
166
+ forceFlush(): Promise<void>;
167
+ };
168
+ export type LangfuseContextTracerOptions = {
169
+ /** Defaults to the bundled @langfuse/tracing startObservation; inject a compatible function for custom runtimes or tests. */
170
+ startObservation?: LangfuseStartObservation;
171
+ /** Span processor drained on flush()/close() — pass the LangfuseSpanProcessor registered with your OpenTelemetry setup. */
172
+ spanProcessor?: LangfuseFlushableSpanProcessor;
173
+ name?: string;
174
+ tags?: string[];
175
+ metadata?: LangfuseKVMap;
176
+ redact?: (event: ContextTraceEvent) => ContextTraceEvent | undefined;
177
+ failOnError?: boolean;
178
+ };
156
179
  export type ModelMessage = {
157
180
  role: "user" | "assistant";
158
181
  content: string | ContentBlock[];
@@ -240,6 +263,11 @@ export type ToolResult = {
240
263
  content: string | ContentBlock[];
241
264
  /** End the run after this tool batch: finish with subtype "success" instead of calling the model again. */
242
265
  endTurn?: boolean;
266
+ /**
267
+ * Structured payload carried to `SDKResultMessage.structuredResult` when this
268
+ * tool also ends the run with `endTurn`. Ignored otherwise.
269
+ */
270
+ structuredResult?: unknown;
243
271
  };
244
272
  export type ToolKind = "tool" | "agent_tool";
245
273
  export type ToolBatchCall = {
@@ -558,6 +586,16 @@ export type AgentOptions<TContext = unknown> = {
558
586
  requestTimeoutMs?: number;
559
587
  tools?: Array<ToolDefinition<any, TContext>>;
560
588
  toolBatchPolicy?: ToolBatchPolicy<TContext>;
589
+ /**
590
+ * Declares the run's structured output contract. When set, the SDK injects a
591
+ * `submit_output` tool with this schema: the run only ends successfully once
592
+ * the model submits a payload through it, and the validated payload lands on
593
+ * `SDKResultMessage.structuredResult`. Ending the turn without submitting
594
+ * fails the run with subtype `error_missing_output`. The tool must be the
595
+ * only call in its batch, and its name is reserved (registering a user tool
596
+ * with the same name throws).
597
+ */
598
+ outputSchema?: OutputSchema;
561
599
  /** Lifecycle callbacks that rewrite tool results and outgoing model requests. */
562
600
  hooks?: AgentHooks<TContext>;
563
601
  /**
@@ -659,12 +697,18 @@ export type SDKResultMessage = {
659
697
  * way, keeping completed turns in history so a follow-up query can continue
660
698
  * the conversation. `is_error` stays `false` for it.
661
699
  */
662
- subtype: "success" | "interrupted" | "error" | "error_max_turns" | "error_abort" | "error_timeout";
700
+ subtype: "success" | "interrupted" | "error" | "error_max_turns" | "error_abort" | "error_timeout" | "error_missing_output";
663
701
  is_error: boolean;
664
702
  result: string;
665
703
  session_id: string;
666
704
  num_turns: number;
667
705
  error?: Error;
706
+ /**
707
+ * Validated structured payload submitted via the `submit_output` tool (see
708
+ * `AgentOptions.outputSchema`), or by any tool that ended the run with both
709
+ * `endTurn` and `ToolResult.structuredResult`. Absent otherwise.
710
+ */
711
+ structuredResult?: unknown;
668
712
  /** Summed over every model request in the query. Zeroed when unreported. */
669
713
  usage: TokenUsage;
670
714
  /**
@@ -711,6 +755,12 @@ export declare class ToolExecutionError extends AgentSDKError {
711
755
  }
712
756
  export declare class MaxTurnsError extends AgentSDKError {
713
757
  }
758
+ /**
759
+ * A run with `AgentOptions.outputSchema` ended without the model calling the
760
+ * `submit_output` tool, so no structured output was produced.
761
+ */
762
+ export declare class MissingOutputError extends AgentSDKError {
763
+ }
714
764
  export declare class AbortError extends AgentSDKError {
715
765
  }
716
766
  /** A second query was started on an Agent that was still running one. */
@@ -729,6 +779,20 @@ export declare class ToolPermissionDeniedError extends AgentSDKError {
729
779
  }
730
780
  export declare function tool<TContext = unknown>(): <TSchema>(name: string, description: string, inputSchema: TSchema, handler: ToolHandler<InferInput<TSchema>, TContext>, options?: ToolOptions<InferInput<TSchema>>) => ToolDefinition<InferInput<TSchema>, TContext>;
731
781
  export declare function tool<TSchema, TContext = unknown>(name: string, description: string, inputSchema: TSchema, handler: ToolHandler<InferInput<TSchema>, TContext>, options?: ToolOptions<InferInput<TSchema>>): ToolDefinition<InferInput<TSchema>, TContext>;
782
+ /**
783
+ * A schema describing structured output: a zod schema, or any object with a
784
+ * compatible `parse` method. The SDK validates submitted payloads with it and
785
+ * converts it to the JSON schema shown to the model.
786
+ */
787
+ export type OutputSchema<TOutput = unknown> = {
788
+ parse(input: unknown): TOutput;
789
+ };
790
+ /**
791
+ * Name of the tool the SDK injects when `AgentOptions.outputSchema` is set.
792
+ * Reserved in that configuration: registering a user tool with the same name
793
+ * throws at `createAgent`/`addTools` time.
794
+ */
795
+ export declare const SUBMIT_OUTPUT_TOOL_NAME = "submit_output";
732
796
  export type DelegateToolOptions = {
733
797
  wait?: DelegateWaitMode;
734
798
  targetMailboxId?: string;
@@ -757,6 +821,14 @@ export type AgentToolInput = z.infer<typeof agentToolInputSchema>;
757
821
  export type AgentToolOptions = {
758
822
  description: string;
759
823
  targetMailboxId?: string;
824
+ /**
825
+ * Expected structured output of the target, declared with the same schema
826
+ * the target uses for its own `AgentOptions.outputSchema`. With mode "ask"
827
+ * the tool result is the target's validated structured output as JSON; a
828
+ * target that ends without submitting — or submits a payload that fails this
829
+ * schema — produces a `child_output_invalid` tool error the parent can retry.
830
+ */
831
+ outputSchema?: OutputSchema;
760
832
  };
761
833
  /**
762
834
  * An AgentLike is a live session: it keeps its conversation history across
@@ -804,6 +876,16 @@ export declare function createJsonlContextTracer(options: JsonlContextTracerOpti
804
876
  export declare function createCompositeAgentHooks<TContext = unknown>(hooks: Array<AgentHooks<TContext> | undefined | null>): AgentHooks<TContext>;
805
877
  export declare function createCompositeContextTracer(tracers: Array<ContextTracer | undefined | null>): ContextTracer;
806
878
  export declare function createLangSmithContextTracer(options?: LangSmithContextTracerOptions): ContextTracer;
879
+ /**
880
+ * Langfuse trace sink built on the current @langfuse/tracing (v5) SDK. The SDK
881
+ * is OpenTelemetry-based: the host registers a LangfuseSpanProcessor (from
882
+ * @langfuse/otel) with a tracer provider, and this adapter maps context trace
883
+ * events onto observations — a chain per agent run, a generation per model
884
+ * turn, a tool observation per tool call, and event observations for
885
+ * everything else. Pass the registered span processor so flush()/close() can
886
+ * drain pending spans before a short-lived process exits.
887
+ */
888
+ export declare function createLangfuseContextTracer(options?: LangfuseContextTracerOptions): ContextTracer;
807
889
  export declare function skill(input: SkillInput): SkillDefinition;
808
890
  export declare function loadSkill(path: string): Promise<SkillDefinition>;
809
891
  export declare function createMCPTools(client: MCPClient, options?: MCPToolsOptions): Promise<Array<ToolDefinition<Record<string, unknown>>>>;