# Vercel AI SDK

> Give an AI SDK model Corfedo's code tools with createCorfedoTools, or use Corfedo AI as a language model that keeps the conversation.

Source: https://docs.corfedo.ai/connect/frameworks/vercel-ai-sdk/

`@corfedo/ai-sdk` brings Corfedo to the Vercel AI SDK 7 on Node.js 22.12 or later. It gives your model Corfedo's read tools, or makes Corfedo AI a language model of its own.

## Before you begin

- An API key file with **Search and read code**. Add **Ask Corfedo AI** to use Corfedo AI as the model. See [API keys](https://docs.corfedo.ai/connect/authentication/#api-keys).
- The packages:

```sh
npm install @corfedo/client @corfedo/ai-sdk ai
```

## Give your model Corfedo's tools

```ts
import { CorfedoClient } from '@corfedo/client';
import { createCorfedoTools } from '@corfedo/ai-sdk';
import { generateText, isStepCount } from 'ai';

const client = CorfedoClient.fromApiKeyFile(process.env.CORFEDO_API_KEY_FILE!);
const result = await generateText({
  model,
  tools: createCorfedoTools(client),
  stopWhen: isStepCount(10),
  prompt: 'Where is the retry policy configured?',
});
```

`model` is your own AI SDK model. You own its instructions and loop. Each tool returns Corfedo's result as JSON text. A failing tool becomes an error result the model sees, and the loop continues. `createCorfedoTools(client, { groups: [], tools: ['search_code', 'read_file'] })` selects single tools.

**Check:** `result.steps` contains calls to `search_code` or `read_file`, and the text cites files from your repositories.

## Use Corfedo AI as the model

```ts
import { createCorfedoLanguageModel } from '@corfedo/ai-sdk';
import { generateText } from 'ai';

const model = createCorfedoLanguageModel(client);
const first = await generateText({
  model,
  prompt: 'How are search requests routed?',
  providerOptions: {
    corfedo: { sources: { mode: 'only', repositories: [{ kind: 'name', name: 'acme/search' }] } },
  },
});
const next = await generateText({
  model,
  messages: [
    { role: 'user', content: 'How are search requests routed?' },
    ...first.response.messages,
    { role: 'user', content: 'Which modules call that code?' },
  ],
});
```

The conversation lives in Corfedo. Each call sends one new user message. The model finds the conversation in `providerOptions.corfedo.conversationId`, or in the `providerMetadata.corfedo` it attached to the previous response's messages. Earlier messages are not sent again. Without a conversation, the prompt must be exactly one user message.

| `providerOptions.corfedo` | Effect |
| --- | --- |
| `conversationId` | Continues a conversation |
| `sources`, `references`, `limits`, `tools` | Repositories to read, context to start from, run limits and Corfedo's own tools, for a new message |

The [profile chosen for the API](https://docs.corfedo.ai/connect/api/ai/profiles/) answers.

Function tools you pass are client tools the AI SDK runs. The step ends with their calls, and the next step sends only their results. Corfedo's own tool calls appear as provider-executed dynamic tool calls. Use `streamText` for progress and `Output.object` for an answer that follows a JSON schema.

The finish metadata carries the conversation and run IDs, the outcome and the citations. Aborting a call ends its subscription, not the run. `client.ai.cancel(runId)` stops the run.

**Check:** the second answer refers to the first, which shows the conversation continued.

## Troubleshooting

| What you see | Why | What to do |
| --- | --- | --- |
| `CorfedoUnsupportedInputError` | The call has a system message, a file, a sampling setting, stop sequences or a tool choice other than `auto`. The profile owns instructions and settings. | Remove it, or change the profile |
| `CorfedoRunError` | The run ended without an answer | Read its outcome |
| `CorfedoOutputDiscardedError` | Text already streamed was replaced after the run recovered | Read the answer with `client.ai.getRun(runId)` |

## Related

- [TypeScript SDK](https://docs.corfedo.ai/connect/sdks/typescript/) for the core client.
- [Client tools](https://docs.corfedo.ai/connect/api/ai/client-tools/) covers tools your application runs.
