Skip to main content

Together AI logoTogether AI

Access 6 Together AI models through Mastra's model router. Authentication is handled automatically using the TOGETHER_API_KEY environment variable.

Learn more in the Together AI documentation.

TOGETHER_API_KEY=your-api-key
import { Agent } from "@mastra/core";

const agent = new Agent({
name: "my-agent",
instructions: "You are a helpful assistant",
model: "togetherai/Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8"
});

// Generate a response
const response = await agent.generate("Hello!");

// Stream a response
const stream = await agent.stream("Tell me a story");
for await (const chunk of stream) {
console.log(chunk);
}
info

Mastra uses the OpenAI-compatible /chat/completions endpoint. Some provider-specific features may not be available. Check the Together AI documentation for details.

Models

ModelImageAudioVideoToolsStreamingContext Window
togetherai/deepseek-ai/DeepSeek-R1163,839
togetherai/deepseek-ai/DeepSeek-V3131,072
togetherai/meta-llama/Llama-3.3-70B-Instruct-Turbo131,072
togetherai/moonshotai/Kimi-K2-Instruct131,072
togetherai/openai/gpt-oss-120b131,072
togetherai/Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8262,144

Advanced Configuration

Custom Headers

const agent = new Agent({
name: "custom-agent",
model: {
url: "https://api.together.xyz/v1",
id: "togetherai/Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8",
apiKey: process.env.TOGETHER_API_KEY,
headers: {
"X-Custom-Header": "value"
}
}
});

Dynamic Model Selection

const agent = new Agent({
name: "dynamic-agent",
model: ({ runtimeContext }) => {
const useAdvanced = runtimeContext.task === "complex";
return useAdvanced
? "togetherai/openai/gpt-oss-120b"
: "togetherai/Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8";
}
});

Direct Provider Installation

This provider can also be installed directly as a standalone package, which can be used instead of the Mastra model router string. View the package documentation for more details.

npm install @ai-sdk/togetherai

For detailed provider-specific documentation, see the AI SDK Together AI provider docs.