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

# Vercel AI SDK quickstart

> Call Together models from TypeScript, React, or Next.js with the Vercel AI SDK.

The [Vercel AI SDK](https://ai-sdk.dev/) is a TypeScript library for building AI-powered applications in TypeScript, React, or Next.js. The `@ai-sdk/togetherai` provider gives it native access to Together AI models.

## Quickstart

1. Install the Vercel AI SDK and the Together AI provider package.

<CodeGroup>
  ```bash npm theme={null}
  npm i ai @ai-sdk/togetherai
  ```

  ```bash yarn theme={null}
  yarn add ai @ai-sdk/togetherai
  ```

  ```bash pnpm theme={null}
  pnpm add ai @ai-sdk/togetherai
  ```
</CodeGroup>

2. Import the Together AI provider and call `generateText` with Kimi K3.

```typescript TypeScript theme={null}
import { generateText } from "ai";
import { createTogetherAI } from "@ai-sdk/togetherai";

const together = createTogetherAI({
  apiKey: process.env.TOGETHER_API_KEY ?? "",
});

async function main() {
  const { text } = await generateText({
    model: together("moonshotai/Kimi-K3"),
    prompt: "Write a vegetarian lasagna recipe for 4 people.",
    providerOptions: {
      togetherai: { reasoning: { enabled: false } },
    },
  });

  console.log(text);
}

main();
```

Anything inside `providerOptions.togetherai` is passed through to the Together API as request body fields, so you can set Together-specific parameters the AI SDK doesn't model itself. Kimi K3 reasons by default, and `reasoning: { enabled: false }` turns that off for tasks that don't need it.

## Streaming

To stream responses, use `streamText`:

```typescript TypeScript theme={null}
import { streamText } from "ai";
import { createTogetherAI } from "@ai-sdk/togetherai";

const together = createTogetherAI({
  apiKey: process.env.TOGETHER_API_KEY ?? "",
});

async function main() {
  const result = streamText({
    model: together("moonshotai/Kimi-K3"),
    prompt: "Invent a new holiday and describe its traditions.",
    providerOptions: {
      togetherai: { reasoning: { enabled: false } },
    },
  });

  for await (const textPart of result.textStream) {
    process.stdout.write(textPart);
  }
}

main();
```

## Tool use

Define tools with the `tool` helper and a [Zod](https://zod.dev/) schema. The SDK calls your `execute` function and feeds the result back to the model. Use `stopWhen` to let the model take the follow-up step after the tool result arrives:

```typescript TypeScript theme={null}
import { generateText, tool, stepCountIs } from "ai";
import { createTogetherAI } from "@ai-sdk/togetherai";
import { z } from "zod";

const together = createTogetherAI({
  apiKey: process.env.TOGETHER_API_KEY ?? "",
});

async function main() {
  const { text } = await generateText({
    model: together("moonshotai/Kimi-K3"),
    tools: {
      getWeather: tool({
        description: "Get the current weather for a city.",
        inputSchema: z.object({ city: z.string() }),
        execute: async ({ city }) => ({
          city,
          temperature: 18,
          condition: "cloudy",
        }),
      }),
    },
    stopWhen: stepCountIs(3),
    prompt: "What is the weather in Paris right now?",
    providerOptions: {
      togetherai: { reasoning: { enabled: false } },
    },
  });

  console.log(text);
}

main();
```

For patterns beyond a single tool call, see [function calling](/docs/inference/function-calling/overview).

## Structured outputs

To get a typed object back instead of free text, pass an `Output.object` with a Zod schema to `generateText`:

```typescript TypeScript theme={null}
import { generateText, Output } from "ai";
import { createTogetherAI } from "@ai-sdk/togetherai";
import { z } from "zod";

const together = createTogetherAI({
  apiKey: process.env.TOGETHER_API_KEY ?? "",
});

async function main() {
  const { output } = await generateText({
    model: together("moonshotai/Kimi-K3"),
    output: Output.object({
      schema: z.object({
        name: z.string(),
        cuisine: z.string(),
      }),
    }),
    prompt: "Invent a vegetarian dish. Reply with its name and cuisine.",
    providerOptions: {
      togetherai: { reasoning: { enabled: false } },
    },
  });

  console.log(output);
}

main();
```

<Note>
  Use `generateText` with `Output.object` rather than `generateObject`. The Together provider doesn't support the AI SDK's JSON schema response format, so `generateObject` fails with a schema validation error.
</Note>

For schema details and model support, see [structured outputs](/docs/inference/chat/structured-outputs).

## Image generation

To generate images with Together AI models, use the `.image()` factory method. For more on image generation with the AI SDK see [generateImage()](https://ai-sdk.dev/docs/reference/ai-sdk-core/generate-image).

```typescript TypeScript theme={null}
import { createTogetherAI } from "@ai-sdk/togetherai";
import { generateImage } from "ai";

const togetherai = createTogetherAI({
  apiKey: process.env.TOGETHER_API_KEY ?? "",
});

const { images } = await generateImage({
  model: togetherai.image("black-forest-labs/FLUX.2-dev"),
  prompt: "A delighted resplendent quetzal mid flight amidst raindrops",
});

// The images array contains base64-encoded image data by default
```

You can pass optional provider-specific request parameters using the `providerOptions` argument.

```typescript TypeScript theme={null}
import { createTogetherAI } from "@ai-sdk/togetherai";
import { generateImage } from "ai";

const togetherai = createTogetherAI({
  apiKey: process.env.TOGETHER_API_KEY ?? "",
});

const { images } = await generateImage({
  model: togetherai.image("black-forest-labs/FLUX.2-dev"),
  prompt: "A delighted resplendent quetzal mid flight amidst raindrops",
  size: "512x512",
  // Optional additional provider-specific request parameters
  providerOptions: {
    togetherai: {
      steps: 40,
    },
  },
});
```

Supported image dimensions vary by model. Common sizes include 512x512, 768x768, and 1024x1024, with some models supporting up to 1792x1792. The default size is 1024x1024. See the [Together AI models page](/docs/serverless/models#image-models) for the full list of image models and their capabilities.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.