Skip to main content
您可以使用 LangSmith 来追踪来自 Vercel AI SDK 的运行。本指南将通过一个示例进行说明。

安装

此封装器需要 AI SDK v5 和 langsmith>=0.3.63。如果您使用的是旧版本的 AI SDK 或 langsmith,请参阅基于 OpenTelemetry (OTEL) 的方法在此页面
安装 Vercel AI SDK。本指南在下面的代码片段中使用 Vercel 的 OpenAI 集成,但您也可以使用他们的任何其他选项。
npm install ai @ai-sdk/openai zod

环境配置

export LANGSMITH_TRACING=true
export LANGSMITH_API_KEY=<your-api-key>

# 示例使用 OpenAI,但您可以使用任何选择的 LLM 提供商
export OPENAI_API_KEY=<your-openai-api-key>

# 对于链接到多个工作区的 LangSmith API 密钥,请设置 LANGSMITH_WORKSPACE_ID 环境变量以指定要使用的工作区。
export LANGSMITH_WORKSPACE_ID=<your-workspace-id>

基础设置

导入并封装 AI SDK 方法,然后像往常一样使用它们:
import { openai } from "@ai-sdk/openai";
import * as ai from "ai";

import { wrapAISDK } from "langsmith/experimental/vercel";

const { generateText, streamText, generateObject, streamObject } =
  wrapAISDK(ai);

await generateText({
  model: openai("gpt-5-nano"),
  prompt: "Write a vegetarian lasagna recipe for 4 people.",
});
您应该会在 LangSmith 仪表板中看到一个像这样的追踪 您也可以追踪带有工具调用的运行:
import * as ai from "ai";
import { tool, stepCountIs } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";

import { wrapAISDK } from "langsmith/experimental/vercel";

const { generateText, streamText, generateObject, streamObject } =
  wrapAISDK(ai);

await generateText({
  model: openai("gpt-5-nano"),
  messages: [
    {
      role: "user",
      content: "What are my orders and where are they? My user ID is 123",
    },
  ],
  tools: {
    listOrders: tool({
      description: "list all orders",
      inputSchema: z.object({ userId: z.string() }),
      execute: async ({ userId }) =>
        `User ${userId} has the following orders: 1`,
    }),
    viewTrackingInformation: tool({
      description: "view tracking information for a specific order",
      inputSchema: z.object({ orderId: z.string() }),
      execute: async ({ orderId }) =>
        `Here is the tracking information for ${orderId}`,
    }),
  },
  stopWhen: stepCountIs(5),
});
这将产生一个像这样的追踪 您可以像往常一样使用其他 AI SDK 方法。

使用 traceable

您可以在 AI SDK 调用周围或 AI SDK 工具调用内部封装 traceable 调用。如果您想在 LangSmith 中将运行分组在一起,这很有用:
import * as ai from "ai";
import { tool, stepCountIs } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";

import { traceable } from "langsmith/traceable";
import { wrapAISDK } from "langsmith/experimental/vercel";

const { generateText, streamText, generateObject, streamObject } =
  wrapAISDK(ai);

const wrapper = traceable(async (input: string) => {
  const { text } = await generateText({
    model: openai("gpt-5-nano"),
    messages: [
      {
        role: "user",
        content: input,
      },
    ],
    tools: {
      listOrders: tool({
        description: "list all orders",
        inputSchema: z.object({ userId: z.string() }),
        execute: async ({ userId }) =>
          `User ${userId} has the following orders: 1`,
      }),
      viewTrackingInformation: tool({
        description: "view tracking information for a specific order",
        inputSchema: z.object({ orderId: z.string() }),
        execute: async ({ orderId }) =>
          `Here is the tracking information for ${orderId}`,
      }),
    },
    stopWhen: stepCountIs(5),
  });
  return text;
}, {
  name: "wrapper",
});

await wrapper("What are my orders and where are they? My user ID is 123.");
生成的追踪将像这样

在无服务器环境中追踪

在无服务器环境中追踪时,您必须在环境关闭之前等待所有运行刷新完成。为此,您可以在封装 AI SDK 方法时传递一个 LangSmith Client 实例,然后调用 await client.awaitPendingTraceBatches()。 确保也将其传递给您创建的任何 traceable 封装器:
import * as ai from "ai";
import { tool, stepCountIs } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";

import { Client } from "langsmith";
import { traceable } from "langsmith/traceable";
import { wrapAISDK } from "langsmith/experimental/vercel";

const client = new Client();

const { generateText, streamText, generateObject, streamObject } =
  wrapAISDK(ai, { client });

const wrapper = traceable(async (input: string) => {
  const { text } = await generateText({
    model: openai("gpt-5-nano"),
    messages: [
      {
        role: "user",
        content: input,
      },
    ],
    tools: {
      listOrders: tool({
        description: "list all orders",
        inputSchema: z.object({ userId: z.string() }),
        execute: async ({ userId }) =>
          `User ${userId} has the following orders: 1`,
      }),
      viewTrackingInformation: tool({
        description: "view tracking information for a specific order",
        inputSchema: z.object({ orderId: z.string() }),
        execute: async ({ orderId }) =>
          `Here is the tracking information for ${orderId}`,
      }),
    },
    stopWhen: stepCountIs(5),
  });
  return text;
}, {
  name: "wrapper",
  client,
});

try {
  await wrapper("What are my orders and where are they? My user ID is 123.");
} finally {
  await client.awaitPendingTraceBatches();
}
如果您使用的是 Next.js,有一个方便的 after 钩子,您可以在此处放置此逻辑:
import { after } from "next/server"
import { Client } from "langsmith";


export async function POST(request: Request) {
  const client = new Client();

  ...

  after(async () => {
    await client.awaitPendingTraceBatches();
  });

  return new Response(JSON.stringify({ ... }), {
    status: 200,
    headers: { "Content-Type": "application/json" },
  });
};
有关更多详细信息,包括在无服务器环境中管理速率限制的信息,请参阅在无服务器环境中追踪 JS 函数

传递 LangSmith 配置

您可以在初始封装 AI SDK 方法时以及通过 providerOptions.langsmith 运行它们时,将 LangSmith 特定的配置传递给您的封装器。 这包括元数据(您稍后可以使用它在 LangSmith 中过滤运行)、顶级运行名称、标签、自定义客户端实例等。 在封装时传递的配置将应用于您使用封装方法进行的所有未来调用:
import { openai } from "@ai-sdk/openai";
import * as ai from "ai";

import { wrapAISDK } from "langsmith/experimental/vercel";

const { generateText, streamText, generateObject, streamObject } =
  wrapAISDK(ai, {
    metadata: {
      key_for_all_runs: "value",
    },
    tags: ["myrun"],
  });

await generateText({
  model: openai("gpt-5-nano"),
  prompt: "Write a vegetarian lasagna recipe for 4 people.",
});
而通过 providerOptions.langsmith 在运行时传递的配置仅适用于该运行。 我们建议导入您的配置并将其封装在 createLangSmithProviderOptions 中,以确保正确的类型:
import { openai } from "@ai-sdk/openai";
import * as ai from "ai";

import {
  wrapAISDK,
  createLangSmithProviderOptions,
} from "langsmith/experimental/vercel";

const { generateText, streamText, generateObject, streamObject } =
  wrapAISDK(ai);

const lsConfig = createLangSmithProviderOptions({
  metadata: {
    individual_key: "value",
  },
  name: "my_individual_run",
});

await generateText({
  model: openai("gpt-5-nano"),
  prompt: "Write a vegetarian lasagna recipe for 4 people.",
  providerOptions: {
    langsmith: lsConfig,
  },
});

指定自定义运行 ID

您可以使用 createLangSmithProviderOptions 通过 providerOptions 为每次调用预先指定一个运行 ID。使用 LangSmith SDK 中的 uuid7() 生成一个有效的 ID:
import * as ai from "ai";
import { openai } from "@ai-sdk/openai";
import { wrapAISDK, createLangSmithProviderOptions } from "langsmith/experimental/vercel";
import { uuid7 } from "langsmith";

const { generateText } = wrapAISDK(ai);

const runId = uuid7();
const lsConfig = createLangSmithProviderOptions({ id: runId });

await generateText({
  model: openai("gpt-5.4-mini"),
  prompt: "What is the capital of France?",
  providerOptions: {
    langsmith: lsConfig,
  },
});

// runId 现在可用于附加反馈、查询运行等。
有关手动指定运行 ID 的更多详细信息,请参阅指定自定义运行 ID

数据脱敏

您可以通过指定自定义输入/输出处理函数来定制 AI SDK 发送给 LangSmith 的输入和输出。如果您处理的是希望避免发送给 LangSmith 的敏感数据,这很有用。 因为输出格式因您使用的 AI SDK 方法而异,我们建议将配置单独定义并传递给封装的方法。您还需要为 AI SDK 调用中的子 LLM 运行提供单独的函数,因为在顶级调用 generateText 会在内部调用 LLM,并且可能多次调用。 我们还建议将一个泛型参数传递给 createLangSmithProviderOptions,以获得输入和输出的正确类型。 以下是 generateText 的一个示例:
import {
  wrapAISDK,
  createLangSmithProviderOptions,
} from "langsmith/experimental/vercel";
import * as ai from "ai";
import { openai } from "@ai-sdk/openai";

const { generateText } = wrapAISDK(ai);

const lsConfig = createLangSmithProviderOptions<typeof generateText>({
  processInputs: (inputs) => {
    const { messages } = inputs;
    return {
      messages: messages?.map((message) => ({
        providerMetadata: message.providerOptions,
        role: "assistant",
        content: "REDACTED",
      })),
      prompt: "REDACTED",
    };
  },
  processOutputs: (outputs) => {
    return {
      providerMetadata: outputs.providerMetadata,
      role: "assistant",
      content: "REDACTED",
    };
  },
  processChildLLMRunInputs: (inputs) => {
    const { prompt } = inputs;
    return {
      messages: prompt.map((message) => ({
        ...message,
        content: "REDACTED CHILD INPUTS",
      })),
    };
  },
  processChildLLMRunOutputs: (outputs) => {
    return {
      providerMetadata: outputs.providerMetadata,
      content: "REDACTED CHILD OUTPUTS",
      role: "assistant",
    };
  },
});

const { text } = await generateText({
  model: openai("gpt-5-nano"),
  prompt: "What is the capital of France?",
  providerOptions: {
    langsmith: lsConfig,
  },
});

// Paris.
console.log(text);
实际的返回值将包含原始的、未脱敏的结果,但 LangSmith 中的追踪将被脱敏。这是一个示例 要脱敏工具输入/输出,请像这样将您的 execute 方法封装在 traceable 中:
import * as ai from "ai";
import { tool, stepCountIs } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";

import { Client } from "langsmith";
import { traceable } from "langsmith/traceable";
import { wrapAISDK } from "langsmith/experimental/vercel";

const client = new Client();

const { generateText, streamText, generateObject, streamObject } =
  wrapAISDK(ai, { client });

const { text } = await generateText({
  model: openai("gpt-5-nano"),
  messages: [
    {
      role: "user",
      content: "What are my orders? My user ID is 123.",
    },
  ],
  tools: {
    listOrders: tool({
      description: "list all orders",
      inputSchema: z.object({ userId: z.string() }),
      execute: traceable(
        async ({ userId }) => {
          return `User ${userId} has the following orders: 1`;
        },
        {
          processInputs: (input) => ({ text: "REDACTED" }),
          processOutputs: (outputs) => ({ text: "REDACTED" }),
          run_type: "tool",
          name: "listOrders",
        }
      ) as (input: { userId: string }) => Promise<string>,
    }),
  },
  stopWhen: stepCountIs(5),
});
traceable 的返回类型很复杂,这使得类型转换成为必要。如果您希望避免类型转换,也可以省略 AI SDK 的 tool 封装函数。