docs/src/content/en/models/providers/perplexity.mdx
Access 4 Perplexity models through Mastra's model router. Authentication is handled automatically using the PERPLEXITY_API_KEY environment variable.
Learn more in the Perplexity documentation.
PERPLEXITY_API_KEY=your-api-key
import { Agent } from "@mastra/core/agent";
const agent = new Agent({
id: "my-agent",
name: "My Agent",
instructions: "You are a helpful assistant",
model: "perplexity/sonar"
});
// 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);
}
<ProviderModelsTable models={[ { "model": "perplexity/sonar", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": false, "reasoning": false, "contextWindow": 128000, "maxOutput": 4096, "inputCost": 1, "outputCost": 1 }, { "model": "perplexity/sonar-deep-research", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": false, "reasoning": true, "contextWindow": 128000, "maxOutput": 32768, "inputCost": 2, "outputCost": 8 }, { "model": "perplexity/sonar-pro", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": false, "reasoning": false, "contextWindow": 200000, "maxOutput": 8192, "inputCost": 3, "outputCost": 15 }, { "model": "perplexity/sonar-reasoning-pro", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": false, "reasoning": true, "contextWindow": 128000, "maxOutput": 4096, "inputCost": 2, "outputCost": 8 } ]} />
const agent = new Agent({
id: "custom-agent",
name: "custom-agent",
model: {
id: "perplexity/sonar",
apiKey: process.env.PERPLEXITY_API_KEY,
headers: {
"X-Custom-Header": "value"
}
}
});
const agent = new Agent({
id: "dynamic-agent",
name: "Dynamic Agent",
model: ({ requestContext }) => {
const useAdvanced = requestContext.task === "complex";
return useAdvanced
? "perplexity/sonar-reasoning-pro"
: "perplexity/sonar";
}
});
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/perplexity
For detailed provider-specific documentation, see the AI SDK Perplexity provider docs.