You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/frameworks/vercel-ai.md
+69-21Lines changed: 69 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,10 +7,12 @@ description: Capture Vercel AI SDK OpenTelemetry spans in Logfire from Node.js a
7
7
8
8
The Vercel AI SDK can emit OpenTelemetry spans for model calls, tools, token usage, and streaming operations. Logfire can receive those spans through either `@pydantic/logfire-node` in Node.js scripts or `@vercel/otel` in Next.js applications.
9
9
10
-
## Node.js Scripts
10
+
Since **AI SDK v7**, the recommended telemetry path is the [`@ai-sdk/otel`](https://ai-sdk.dev/docs/ai-sdk-core/telemetry) package, which emits spans that follow the OpenTelemetry GenAI semantic conventions (`gen_ai.*`). **AI SDK v6 and earlier** used the per-call `experimental_telemetry` option and the legacy `ai.*` attribute shape. Logfire recognizes both, so existing instrumentation keeps working — the sections below cover each path.
11
+
12
+
## Node.js Scripts (AI SDK v7)
11
13
12
14
```bash
13
-
npm install @pydantic/logfire-node ai @ai-sdk/openai
15
+
npm install @pydantic/logfire-node ai @ai-sdk/otel @ai-sdk/openai
14
16
```
15
17
16
18
Replace `@ai-sdk/openai` with the provider package you use, such as `@ai-sdk/anthropic` or `@ai-sdk/google`.
@@ -25,33 +27,41 @@ logfire.configure({
25
27
})
26
28
```
27
29
28
-
Enable telemetry on AI SDK calls:
30
+
Register the `@ai-sdk/otel` integration once at startup. After that, every AI SDK call emits telemetry — you do not set `experimental_telemetry` per call:
-`OpenTelemetry` — emits GenAI semantic-convention spans (`gen_ai.*`). Recommended, and what Logfire renders and prices best.
52
+
-`LegacyOpenTelemetry` — emits the older AI SDK (`ai.*`) span shape for tools that have not migrated.
53
+
44
54
## Next.js
45
55
46
-
In Next.js, configure `@vercel/otel` as shown in [Next.js](nextjs.md), then enable the same `experimental_telemetry` option on AI SDK calls. The `instrumentation.ts` file must live in the project root, or in `src` if your Next.js app uses `src`.
56
+
In Next.js, configure `@vercel/otel` as shown in [Next.js](nextjs.md), then register `@ai-sdk/otel` in the same `instrumentation.ts`. The `instrumentation.ts` file must live in the project root, or in `src` if your Next.js app uses `src`.
47
57
48
58
```bash
49
-
npm install @vercel/otel @opentelemetry/api ai @ai-sdk/openai
59
+
npm install @vercel/otel @opentelemetry/api ai @ai-sdk/otel @ai-sdk/openai
50
60
```
51
61
52
-
## Enabling Telemetry
62
+
## Legacy Telemetry (AI SDK v6 and earlier)
53
63
54
-
The Vercel AI SDK emits OpenTelemetry spans when `experimental_telemetry.isEnabled`is set:
64
+
Before v7, the Vercel AI SDK emitted spans only when `experimental_telemetry.isEnabled`was set on each call:
55
65
56
66
```ts
57
67
const result =awaitgenerateText({
@@ -61,22 +71,27 @@ const result = await generateText({
61
71
})
62
72
```
63
73
64
-
This works with the AI SDK core functions that emit telemetry, including:
74
+
This still works and covers the AI SDK core functions that emit telemetry, including:
65
75
66
76
-`generateText` and `streamText`
67
77
-`generateObject` and `streamObject`
68
78
-`embed` and `embedMany`
69
79
80
+
These calls produce the legacy `ai.*` attribute shape (for example `ai.model.provider`, `ai.response.model`, `ai.usage.promptTokens`). Logfire maps both the legacy `ai.*` attributes and the v7 `gen_ai.*` attributes, so spans from either version are recognized as LLM spans.
@@ -99,20 +114,55 @@ For Node.js scripts, import your Logfire instrumentation file before importing o
99
114
100
115
## What You Will See
101
116
102
-
When telemetry is enabled, Logfire captures a trace for each AI operation. Depending on the AI SDK provider and call type, traces can include:
117
+
When telemetry is enabled, Logfire captures a trace for each AI operation. With `@ai-sdk/otel` (v7), **span names include the model name**, so a single `generateText`call with a tool produces spans such as:
103
118
104
-
- parent spans such as `ai.generateText`
105
-
- provider call spans for the model request
106
-
- tool call spans
107
-
- model and provider details
119
+
-`invoke_agent gpt-4.1-mini` — the root agent span
120
+
-`chat gpt-4.1-mini` — the provider/model call
121
+
-`execute_tool weather` — a tool call
122
+
123
+
Depending on the AI SDK provider and call type, traces can also include:
124
+
125
+
- model and provider details (`gen_ai.provider.name`, `gen_ai.request.model` / `gen_ai.response.model`)
108
126
- input and output token usage
109
127
- timing information
110
128
- tool call arguments and results
111
-
- prompts and responses when the AI SDK emits them
129
+
- prompts and responses (`gen_ai.input.messages` / `gen_ai.output.messages`) when the AI SDK emits them
130
+
131
+
Prompts and responses may contain sensitive data. To emit telemetry without recording inputs or outputs for a v7 call, set both options to `false`:
132
+
133
+
```ts
134
+
awaitgenerateText({
135
+
model,
136
+
prompt,
137
+
telemetry: {
138
+
recordInputs: false,
139
+
recordOutputs: false,
140
+
},
141
+
})
142
+
```
143
+
144
+
Set `telemetry.isEnabled` to `false` to disable telemetry entirely for an individual call.
112
145
113
146
## Metadata
114
147
115
-
Use `functionId` and `metadata` to make traces easier to query:
148
+
Use `functionId` and `metadata` to make traces easier to query. In v7, pass them through the per-call `telemetry` option:
149
+
150
+
```ts
151
+
awaitgenerateText({
152
+
model,
153
+
prompt,
154
+
telemetry: {
155
+
functionId: 'support-reply',
156
+
metadata: {
157
+
tenant: 'acme',
158
+
},
159
+
},
160
+
})
161
+
```
162
+
163
+
`functionId` identifies the agent or use case behind a call. Logfire uses it as the agent identity when grouping runs (for example on the AI Engineering agent pages), rather than it only appearing in span names — in v7 the span name carries the model, not the `functionId`. `metadata` attaches custom key-value pairs to the emitted telemetry spans.
164
+
165
+
For AI SDK v6 and earlier, pass the same fields inside `experimental_telemetry`:
116
166
117
167
```ts
118
168
awaitgenerateText({
@@ -127,5 +177,3 @@ await generateText({
127
177
},
128
178
})
129
179
```
130
-
131
-
`functionId` appears in span names and helps distinguish use cases. `metadata` attaches custom key-value pairs to the emitted telemetry spans.
0 commit comments