> ## Documentation Index
> Fetch the complete documentation index at: https://docs.uselemma.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Vercel AI SDK

> Record Vercel AI SDK model calls and tool executions with Lemma

Use `vercelAI()` when your app calls `generateText`, `streamText`, or an AI SDK agent. The integration supports AI SDK v7 and v6. It records model calls as Lemma generations and tool executions as Lemma tool calls.

## Install

```bash theme={null}
npm install @uselemma/tracing ai zod
```

## AI SDK v7

Wrap one agent execution in `lemma.trace()`, then pass `vercelAI()` to the AI SDK `telemetry.integrations` option.

```typescript theme={null}
import { generateText, tool } from "ai";
import { z } from "zod";
import { Lemma, vercelAI } from "@uselemma/tracing";

const lemma = new Lemma();

const answer = await lemma.trace(
  {
    name: "support-agent",
    input: userMessage,
    threadId: conversationId,
    userId: user.id,
  },
  async () => {
    const result = await generateText({
      model,
      prompt: userMessage,
      tools: {
        searchDocs: tool({
          inputSchema: z.object({
            query: z.string(),
          }),
          execute: async ({ query }) => searchDocs(query),
        }),
      },
      telemetry: {
        integrations: [vercelAI()],
      },
    });

    return result.text;
  },
);
```

## AI SDK v6

For AI SDK v6, pass the same helper through `experimental_telemetry.integrations`.

```typescript theme={null}
import { generateText, tool } from "ai";
import { z } from "zod";
import { Lemma, vercelAI } from "@uselemma/tracing";

const lemma = new Lemma();

const answer = await lemma.trace(
  {
    name: "support-agent",
    input: userMessage,
    threadId: conversationId,
    userId: user.id,
  },
  async () => {
    const result = await generateText({
      model,
      prompt: userMessage,
      tools: {
        searchDocs: tool({
          inputSchema: z.object({
            query: z.string(),
          }),
          execute: async ({ query }) => searchDocs(query),
        }),
      },
      experimental_telemetry: {
        integrations: [vercelAI()],
      },
    });

    return result.text;
  },
);
```

## Streaming

Use the same integration with `streamText`. When you pass a trace handle, `vercelAI({ trace })` ends it from the AI SDK terminal callback: `onEnd` in AI SDK v7 and `onFinish` in AI SDK v6.

```typescript theme={null}
import { streamText } from "ai";
import { Lemma, vercelAI } from "@uselemma/tracing";

const lemma = new Lemma();

const trace = lemma.trace({
  name: "support-agent",
  input: userMessage,
  threadId: conversationId,
  userId: user.id,
});

const result = streamText({
  model,
  prompt: userMessage,
  telemetry: {
    integrations: [vercelAI({ trace })],
  },
});

for await (const _part of result.fullStream) {
  // stream to your response
}
```

## What Lemma records

| AI SDK event   | Lemma record                                                                      |
| -------------- | --------------------------------------------------------------------------------- |
| Model call     | Generation with model, provider, messages, output text, token usage, and duration |
| Tool execution | Tool call with name, input, output or error, and duration                         |

AI SDK v7 provides model and tool durations directly. AI SDK v6 provides tool execution durations; model-call durations are inferred by Lemma from the surrounding trace when the AI SDK does not provide an explicit duration.

If you use the callback form of `lemma.trace()`, the callback owns trace closure. If you pass a trace handle to `vercelAI({ trace })`, the integration closes it automatically at the end of the AI SDK operation.

Use `recordInputs: false` or `recordOutputs: false` to avoid sending prompts, tool inputs, tool outputs, or model output text.

```typescript theme={null}
telemetry: {
  integrations: [
    vercelAI({
      recordInputs: false,
      recordOutputs: false,
    }),
  ],
}
```
