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

# AI SDK

> Tracing Vercel's AI SDK with Gentrace

## Overview

The Vercel AI SDK provides a consistent API for interacting with various AI providers.

Gentrace integrates seamlessly to provide automatic tracing and error analysis for your AI application.

## Prerequisites

This guide assumes you have already setup an interaction as shown in our [quickstart](/getting-started/quickstart).

## Instrumentation

The AI SDK natively exports to OpenTelemetry, and Gentrace can consume this telemetry automatically.

To enable telemetry, set the `experimental_telemetry` flag to `true` in your AI SDK calls.

For example:

<Note>
  The AI SDK call must be inside the callstack of an
  [interaction](/tracing/interactions) for Gentrace to capture the
  trace.
</Note>

```typescript theme={null}
const { text } = await generateText({
  // Enable telemetry
  experimental_telemetry: {
    isEnabled: true,
  },
  model: openai('gpt-4.1-nano'),
  prompt: `Write a haiku about ${topic}`,
});
```

## Full example

```bash .env theme={null}
GENTRACE_API_KEY=your-gentrace-api-key
GENTRACE_PIPELINE_ID=your-pipeline-id
OPENAI_API_KEY=your-openai-api-key
```

```typescript theme={null}
import { init, interaction } from 'gentrace';
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';

init({
  apiKey: process.env.GENTRACE_API_KEY,
});

const writePoem = interaction('write-poem', async (topic: string) => {
  const { text } = await generateText({
    model: openai('gpt-4.1-nano'),
    prompt: `Write a haiku about ${topic}`,
    experimental_telemetry: {
      isEnabled: true,
    },
  });
  return text;
});

const main = async () => {
  const result = await writePoem('quantum computing');
  console.log(result);
};

main();
```

<Info>
  For an example in our SDK, see the [OpenAI AI SDK
  example](https://github.com/gentrace/gentrace-node/blob/main/examples/openai-ai-sdk.ts)
  on GitHub.
</Info>

## Related Resources

* [Vercel AI SDK Documentation](https://sdk.vercel.ai/docs)
* [Gentrace SDK Reference](/tracing/interactions)
